sirupsen / logrus

Structured, pluggable logging for Go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Idea: Customize JSONFormatter's JSON encoder?

harrisonhjones opened this issue · comments

I have a use case for being able to customize the json encoder that the JSONFormatter uses. I hoped that the code would support a pluggable Encoder interface but unfortunately it does not. I know I could write my own Formatter but I'd like to re-use the logic already in JSONFormatter. A few ideas:

Idea 1 : Add a new optional Encoder field to JSONFormatter. The code would go from:

encoder := json.NewEncoder(b)
encoder.SetEscapeHTML(!f.DisableHTMLEscape)
if f.PrettyPrint {
	encoder.SetIndent("", "  ")
}

to

encoder := f.Encoder
if encoder == nil {
	encoder = json.NewEncoder(b)
	encoder.SetEscapeHTML(!f.DisableHTMLEscape)
	if f.PrettyPrint {
		encoder.SetIndent("", "  ")
	}
}

The doc for the JSONFormatter struct would be updated to call out that if Encoder is set then DisableHTMLEscape and PrettyPrint are ignored (set them on the encoder itself).

Idea 2 : We break out the logic in JSONFormatter and move it to a reusable NormalizeEntryForFormat function in formatter.go and then introduce a new formatter, the EncodingFormatter that takes the Encoder from Idea 1 but does include the ignore struct values mentioned. JSONFormatter remains the same (without the Encoder field). Both new formatters use the new NormalizeEntryForFormat helper. We could choose to export NormalizeEntryForFormat or not.

Thoughts?

This issue is stale because it has been open for 30 days with no activity.

This issue is stale because it has been open for 30 days with no activity.

This issue was closed because it has been inactive for 14 days since being marked as stale.