Cysharp / ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add native CLEF format json logging support

Reag opened this issue · comments

It would be very useful to have a built in formatter to create the correct message templates for the CLEF format of JSON messages. Several logging applications expect to get CLEF format logs, and I think it would be a nice addition to prevent users from having to implement their own formatters to create the correct message templates.

The specific part that would need attention is the pseudo string interpolation used by the CLEF message template format, specified here. Currently, the default behavior for a JSON log is fairly similar to this:

{"@t":"TIME","@l":"Information","Category":"MyWebProvider","@m":"Loaded 21 items out of 26 from remote server!","loaded":21,"total":26}

while the desired output would be this:

{"@t":"TIME","@l":"Information","Category":"MyWebProvider","@mt":"Loaded {loaded} items out of {total} from remote server!","loaded":21,"total":26}

Could you modify JsonPropertyNames configuration?
https://github.com/Cysharp/ZLogger?tab=readme-ov-file#json

That works for the basic case, but CLEF logging can use either A) Message (m) or B) Message Templates (mt). Editting the JsonPropertyNames will allow you to log with the message, but not the message template. Specifically, to use the message templates correctly, and thus the log viewers that support CLEF, the output has to be different that what is provided by default with the JsonLogger. You can see this subtle, but important, difference in what I described above, where to use MessageTemplates, the outgoing string needs to include the literals that was generated.

Thank you, I understand.
I think it's a good suggestion. We have the information internally, so I'll try to prepare a way to extract it!

Thank you! That would be most helpful, as I'm required to log to CLEF for the project I'm in charge of

v2.4.0 supports Write/GetOriginalFormat feature.
This is the sample of custom formatter of CLEF.
https://github.com/Cysharp/ZLogger/blob/master/sandbox/ConsoleApp/SampleCustomFormatter.cs

-[edit]: Ignore this, I'm a dummy. Thanks for helping out!

Thanks, but good point.
Perhaps ResetWrittenCount will not work in Unity and you can use Clear instead.