mattiasflodin/reckless

Custom fields in JSON logger

Closed this issue · 3 comments

Hi! Thank you for sharing this awesome library!

I need to create a JSON logger that could accept custom JSON fields for context logging. So apart from the message, severity etc there would be other, custom fields.

Is this possible with reckless?

Thanks!

Hi,

It is definitely possible, and an intended use case, but you are going to have to do more work than would normally be needed. At its core, reckless is essentially a queue containing arbitrary items, and a background thread that calls a formatter that serializes those items into a byte stream. So you would need to package the stuff you need into a set of objects that you pass on the queue, and then you would need a formatter to generate the JSON UTF-8 encoding for you.

The key to keeping latency for logging calls at a minimum is to do as little work as possible up front—just make sure you gather the minimum information needed and pass it on the queue. Then you do the bulk work in the formatter, which is called from the background thread.

Have a look at policy_log.hpp. At the bottom is the policy_log class. You'll see that all it does is derive from basic_log and call its write() function with the information that is needed, and provides a formatter as a template argument. basic_log::write() will package all of this on the queue, and make sure your formatter is invoked from the background thread.

Now look at policy_formatter. The only thing required by reckless is the format() function. It is called with the exact same arguments that you passed into basic_log::write, and with an additional output_buffer pointer. Here is where you will take the arguments and convert them into JSON, then write it to the output buffer.

So to accomplish this your job would be to write your own class that derives from basic_log, and another formatter class that implements format() and generates JSON from the arguments.

You'll see in policy_log that not all arguments passed to basic_log::write are taken directly from the arguments to policy_log::write. Rather, some of the fields in the log are template arguments to the class. You could make a similar design with field classes for JSON, but they would have to be implemented to serialize differently. The existing field classes that plug into policy_log are in the same header file.

I hope this is enough to get you going. Let me know if you have any more questions.

Thanks!
Would you accept PR with a JSON logger?

Sure, as long as it doesn't stray too far in style and quality from the rest of the library. In any case I'd be happy to at least add a link in the readme to it.