tinylog icon indicating copy to clipboard operation
tinylog copied to clipboard

JSON Logging to the Console

Open halprin opened this issue 2 years ago • 3 comments

Please add a new writer that supports JSON output to the console instead of a file.

There is an existing json writer, but it currently only supports writing out to a file.

One can simulate JSON output to the console using the console writer, but it is easy to run into some gotchas. Given the below tinylog.properties file...

writer         = console
writer.level   = info
writer.format  = {opening-curly-bracket}"timestamp": "{date}", "level": "{level}", "message": "{message}", "thread": "{thread}"{closing-curly-bracket}
writer.charset = UTF-8
writer.stream  = out

...notice the writer.format line. It simulates JSON, but it becomes invalid JSON if the message has multiple lines (e.g. \n) or a JSON-reserved character (e.g. "). This can easily happen if there is an exception's stacktrace being logged.

So, while one can accomplish a simple simulation of JSON console logging, I believe tinylog will benefit from adding a native JSON writer to the console that fully supports JSON.

halprin avatar Jun 23 '23 16:06 halprin

This feature is already implemented in the branch of tinylog 3.

pmwmedia avatar Jun 26 '23 19:06 pmwmedia

Oh, that's great to hear! I'm looking forward to version 3.

halprin avatar Jun 26 '23 19:06 halprin

Here is a preview of the documentation of tinylog 3: https://github.com/tinylog-org/website/blob/v3/content/documentation/configuration.md#json

pmwmedia avatar Jun 26 '23 20:06 pmwmedia