Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizing message keys #99

Open
shouichi opened this issue Jan 31, 2020 · 10 comments
Open

Customizing message keys #99

shouichi opened this issue Jan 31, 2020 · 10 comments

Comments

@shouichi
Copy link

Hi,

Is it possible to customize message key? For example, {"message": "hello"} instead of {"msg": "hello"}. We send log messages to google cloud stackdriver and it requires log messages to be formatted in the certain way to pretty print them in the console.

Thanks!

@tilfin
Copy link
Owner

tilfin commented Jan 31, 2020

Currently, there is no option to change the message field name.
Please change msg field message by creating sub-class at the following code.

class ChangeMsgFormatter < Ougai::Formatters::Bunyan
  def _call(severity, time, progname, data)
    if data.is_a?(Hash)
      msg = data.delete(:msg)
      data[:message] = msg
      super(severity, time, progname, data)
    else
      super({ message: data.to_s })
    end
  end
end

@shouichi
Copy link
Author

Do you want to support customizing message keys? At least adding the above snippet to the README would help future users.

Thanks!

@tilfin
Copy link
Owner

tilfin commented Jan 31, 2020

I think that I should write how to use StackDriver nicely (for example, including the way between logger.child method and the log grouping feature of StackDriver).

@shouichi
Copy link
Author

Awesome 👏 Feel free to close this (not really sure if I should close this but my original question was solved).

@ghost
Copy link

ghost commented Mar 3, 2020

Theoretically couldn't a custom serializer be written to accomplish mapping internal "message" fields to the desired output format? (I'm coming from a Java/logback viewpoint)

{msg: 'blah', this: "that", the: "other}
to JSON output of:
{"m":"blah", "c": {"this":"that", "the":"other"}}

I don't see an immediate way to register a custom serializer but perhaps I've missed something?

@ghost
Copy link

ghost commented Mar 3, 2020

In addition, the ability to create custom serializers (appenders in logback terms) would allow users to write to multiple outputs simultaneously which is very useful when wanting friendly STDOUT logs and detailed json logging to disk which includes more context.

@tilfin
Copy link
Owner

tilfin commented Mar 3, 2020

@andrew-newell Please make your Formatter as well as above one.

class ChangeMsgFormatter < Ougai::Formatters::Bunyan
  def _call(severity, time, progname, data)
    if data.is_a?(Hash)
      msg = data.delete(:msg)
      data[:m] = msg
      super(severity, time, progname, { m: msg, c: data })
    else
      super({ message: data.to_s })
    end
  end
end

You can use broadcast method to multiple outputs.

logger = Ougai::Logger.new(STDOUT)
logger.formatter = Ougai::Formatters::Readable.new
logger.level = Logger::INFO

debug_logger = Ougai::Logger.new('./debug.log')
debug_logger.formatter = ChangeMsgFormatter.new
debug_logger.level = Logger::DEBUG
logger.extend Ougai::Logger.broadcast(debug_logger)

logger.debug('debug!', foo: 'a')
logger.info('info!', bar: 1)

@ghost
Copy link

ghost commented Mar 3, 2020

@tilfin thank you for the examples! I think that should do what I've described without any extra code needing to be added to this library, which is always a great thing. 👍

@ghost
Copy link

ghost commented Mar 3, 2020

@andrew-newell Please make your Formatter as well as above one.

class ChangeMsgFormatter < Ougai::Formatters::Bunyan
  def _call(severity, time, progname, data)
    if data.is_a?(Hash)
      msg = data.delete(:msg)
      data[:m] = msg
      super(severity, time, progname, { m: msg, c: data })
    else
      super({ message: data.to_s })
    end
  end
end

You can use broadcast method to multiple outputs.

logger = Ougai::Logger.new(STDOUT)
logger.formatter = Ougai::Formatters::Readable.new
logger.level = Logger::INFO

debug_logger = Ougai::Logger.new('./debug.log')
debug_logger.formatter = ChangeMsgFormatter.new
debug_logger.level = Logger::DEBUG
logger.extend Ougai::Logger.broadcast(debug_logger)

logger.debug('debug!', foo: 'a')
logger.info('info!', bar: 1)

The method call for super was missing some things, I had to do this:

class ChangeMsgFormatter < Ougai::Formatters::Bunyan
  def _call(severity, time, progname, data)
    if data.is_a?(Hash)
      msg = data.delete(:msg)
      data[:m] = msg
      super(severity, time, progname, { m: msg, c: data })
    else
      super(severity, time, progname, { message: data.to_s })
    end
  end
end

@tilfin
Copy link
Owner

tilfin commented Jan 17, 2021

Pino (JavaScript logger) supports this feature as messageKey option. https://github.com/pinojs/pino/blob/master/docs/api.md#messagekey-string

Pino (JavaScript logger) supports wrapping data as nestedkey option. https://github.com/pinojs/pino/blob/master/docs/api.md#nestedkey-string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants