TwP / logging

A flexible logging library for use in Ruby programs based on the design of Java's log4j library.
https://rubygems.org/gems/logging
MIT License
529 stars 100 forks source link

ability to add fields to json output #152

Closed athirn closed 7 years ago

athirn commented 7 years ago

Right now when I pass a ruby object to the logger, it gets serialized inside the message field. The result is nested JSON.

 logger = Logging.logger[DATA_LOGGER]
        Logging.logger[DATA_LOGGER].appenders = Logging.appenders.file("data/output.json", :layout => Logging::Layouts.json)
        logger.level = :info

{"timestamp":"2016-12-16T10:55:15.327794-05:00","level":"INFO","logger":"data","message":{"one":1,"two":2}}

there should be a way to add fields into the top level JSON

{"timestamp":"2016-12-16T10:55:15.327794-05:00","level":"INFO","logger":"data","one":1,"two":2}

TwP commented 7 years ago

Flattening the message into the top-level JSON object gives rise to name collisions with the other items already present in the JSON object. For example, if your log message is a Ruby Hash and one of the keys in that hash is named :level, then it will conflict with the log level already present in the JSON object.

Two possible approaches for resolving these conflicts are:

The first solution loses information. That is not an acceptable trade off. The second solution alters the output of the log message in an unexpected fashion. Now you have to "know" that your field is going to be renamed because it conflicts with another field present in the top-level.

I'm good at dichotomies - is there another solution that I'm missing to disambiguate naming collisions without losing information? Thoughts? :thought_balloon: