log4rs icon indicating copy to clipboard operation
log4rs copied to clipboard

Support formatting JSON

Open calvinbrown085 opened this issue 5 years ago • 11 comments

Currently, I don't believe if you use the JSON encoder, you can format it, it would be awesome if this was the case.

calvinbrown085 avatar Dec 23 '19 19:12 calvinbrown085

It could possibly look something like this

refresh_rate: 30 seconds
appenders:
  stdout:
    kind: console
    encoder:
      kind: json
      pattern: "{d(%Y-%m-%d %H:%M:%S)} - {m}{t}{T}{n}"
root:
  level: info
  appenders:
    - stdout

calvinbrown085 avatar Dec 23 '19 19:12 calvinbrown085

@calvinbrown085 still interested in this?

estk avatar Jan 28 '20 17:01 estk

I am, but my time may be odd the next couple week so if someone wants to pick it up that's fine.

calvinbrown085 avatar Jan 28 '20 17:01 calvinbrown085

It will be better to support add custom key when using JSON encoder, it will be able to link Filebeat and ELK Stack directly for analysis.

  • Thanks. Alex

caoruijuan13 avatar Jun 05 '20 06:06 caoruijuan13

@Ruijuan-Cao can you give an example?

estk avatar Jun 05 '20 17:06 estk

Firstly, thanks for your reply, and I'm very sorry to see this so late. For example, I need a new customized key-value "type-xxx“ in log for analysis, I didn't find any guidance document to add a new key, so I tried to pretend like that by this

let str = "hello,test\",\"type\":\"xxx";
info!(target:"app", "{}", str);

but all msg including double quotation marks is treated as message by escape character. And the output is shown below:

{
    "time":"2020-06-04T20:59:19.946833+08:00",
    "message":"hello, test\",\"type\":\"xxx",
    "module_path":"spot::statics::clog",
    ...
}

but what I want is like that:

{
    "time":"2020-06-04T20:59:19.946833+08:00",
    "message":"hello, test",
    "type":"xxx",
    "module_path":"spot::statics::clog",
    ...
}

So, I want to know if there is any way to do this.(Maybe this is a bit stupid requirement🤦‍♀️) Thank you very much.

caoruijuan13 avatar Jun 10 '20 06:06 caoruijuan13

Sorry, been away for a while. I still think this would be a great thing to have. Everyone else still interested @estk @Ruijuan-Cao ?

calvinbrown085 avatar Sep 21 '20 15:09 calvinbrown085

Any update on this feature?

sw-dev-code avatar Apr 20 '22 10:04 sw-dev-code

@caoruijuan13 the log crate does not have a notion of structured logging, I would suggest that you use an empty formatter and serialize the json output yourself. Using a separate log target you can filter out your custom serialized logs to a separate appender.

estk avatar Apr 20 '22 14:04 estk

Hi! I'm finding evidence that json formatting is supported in closed issues and here, yet there is no support for that in docs and code from stackoverflow itself isn't working.

Dagunov avatar Feb 22 '24 07:02 Dagunov