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

Support for slf4j's Throwable param? #4

Open
LeifWarner opened this issue May 29, 2012 · 2 comments
Open

Support for slf4j's Throwable param? #4

LeifWarner opened this issue May 29, 2012 · 2 comments

Comments

@LeifWarner
Copy link

e.g. http://www.slf4j.org/apidocs/org/slf4j/Logger.html#debug(java.lang.String, java.lang.Throwable)
Would it be possible to add support for passing a caught Exception into the log method, for the stacktrace to be logged through slf4j's mechanism?

@lwe
Copy link
Contributor

lwe commented May 30, 2012

Would be nice, lets see how we could twist the ruby standard logger API to make this work. Basically I think implementations should behave like: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/Logger.html#method-i-add

In some implementations (e.g. Rails' BufferedLogger) though progname is ignored, in the slf4j logger it uses the progname as marker if given. Maybe we can enhance the slf4j logger to accept a hash of options as progname

    logger.error("some failure bla bla: #{err}", exception: err, marker: "FOO")

Problem is change is basically incompatible with the MRI Logger, question how bad is that - considering it's only used in jruby/slf4j projects. An alternative would be to provide a new method like:

  def raised(exception, msg = nil, level = :error)
    msg = yield if block_given?
    msg ||= "#{msg.class}: #{msg}"
    java_logger.send(level, java_marker, msg, exception) # <- maybe need to cast the exception to Throwable...
  end

What do you think? Personally, I'm in favor of a new method, then it's clear it's a new different, non-standard behavior.

@lwe
Copy link
Contributor

lwe commented May 30, 2012

Another issue is that Ruby Exception != Java Throwable, when looking at the ancestors of a Ruby RuntimeError it looks like:

  • [RuntimeError, StandardError, Exception, Object, MiniTest::Expectations, Rake::DeprecatedObjectDSL, Kernel]

while a java.lang.RuntimeException looks like:

  • [Java::JavaLang::RuntimeException, Java::JavaLang::Exception, Java::JavaLang::Throwable, Java::JavaIo::Serializable, Java::JavaLang::Object, ConcreteJavaProxy, JavaProxy, JavaProxyMethods, Object, MiniTest::Expectations, Rake::DeprecatedObjectDSL, Kernel]

Sadly I did not find any useful method to convert a ruby exception to a java.lang.Throwable...

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

2 participants