-
Notifications
You must be signed in to change notification settings - Fork 24
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
Comments
Currently, there is no option to change the message field name. 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 |
Do you want to support customizing message keys? At least adding the above snippet to the README would help future users. Thanks! |
I think that I should write how to use StackDriver nicely (for example, including the way between |
Awesome 👏 Feel free to close this (not really sure if I should close this but my original question was solved). |
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)
I don't see an immediate way to register a custom serializer but perhaps I've missed something? |
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. |
@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 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) |
@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. 👍 |
The method call for 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 |
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 |
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!
The text was updated successfully, but these errors were encountered: