Cysharp / ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.
MIT License
1.11k stars 79 forks source link

Add native CLEF format json logging support #149

Closed Reag closed 4 months ago

Reag commented 5 months ago

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}
neuecc commented 5 months ago

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

Reag commented 5 months ago

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.

neuecc commented 5 months ago

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!

Reag commented 4 months ago

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

neuecc commented 4 months ago

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

Reag commented 4 months ago

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

neuecc commented 4 months ago

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