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
530 stars 101 forks source link

Appending method name? #186

Closed jmt-gh closed 5 years ago

jmt-gh commented 6 years ago

I'm looking to recreate functionality that ruby's built-in Logger has, but I'm struggling to do so.

Logger:

$logger = Logger.new(STDOUT)

def foo
  $logger.info (__method__) { "this is my log" }
end

foo
=> I, [2018-01-10T12:36:42.436592 #57345]  INFO -- foo: this is my log

With Logging, I don't see the ability to pass an appender when calling it. I also can't think of any way to do this when instantiating the Logging instance, since method will evaluate to whatever method is doing the instantiation, and using something like caller_locations will provide the caller locations for the instantiating method, not the method making a logging call.

Any ideas?

TwP commented 6 years ago

You would need to pass the method along as part of the message:

logger.info("#{__method__}: this is my log")