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

Failure when logging non-string objects #23

Closed
Xylakant opened this issue Oct 5, 2014 · 4 comments
Closed

Failure when logging non-string objects #23

Xylakant opened this issue Oct 5, 2014 · 4 comments

Comments

@Xylakant
Copy link

Xylakant commented Oct 5, 2014

When passing an object to any log method that is not a String, a Hash or a Logstash object, the logger errors with

Puma caught this error: undefined method `[]' for nil:NilClass (NoMethodError)
path/to/gems/logstash-logger-0.6.2/lib/logstash-logger/formatter.rb:34:in `build_event'
path/to/gems/logstash-logger-0.6.2/lib/logstash-logger/formatter.rb:12:in `call'

The culprit is https://github.com/dwbutler/logstash-logger/blob/master/lib/logstash-logger/formatter.rb#L24 where a default case for anything that is not a String, but can be converted is missing. It would be more robust to test if data.responds_to :to_s, so that exception objects or similar can be passed directly.

@dwbutler
Copy link
Owner

dwbutler commented Oct 5, 2014

This certainly sounds like reasonable behavior.

I checked the default stdlib logger, and it does accept any object and formats it as a string. I checked the source code for the default formatter, and found that it calls a private method #msg2str. This method does special formatting on exceptions, and defaults to calling #inspect.

I'll push a fix to call #msg2str in the default case.

@dwbutler
Copy link
Owner

dwbutler commented Oct 5, 2014

Released 0.7.0 to Rubygems.

@rogierslag
Copy link

Still crashes when it receives an int for example (which can be quite useful for taking averages in kibana)

@dwbutler
Copy link
Owner

@rogierslag I cannot reproduce this. Are you using LogStashLogger >= 0.7?

logger = LogStashLogger.new(type: :stdout)
logger.info(1)
{"message":"1","@timestamp":"2015-04-22T07:48:51.934-07:00","@version":"1","severity":"INFO","host":"Davids-MacBook-Pro-2.local"}

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

No branches or pull requests

3 participants