Proper use of Log4r in Ruby Application

I really need to skip something obvious, but I'm having problems with the general use of Log4r in my Ruby application. I can log in without a problem, but the overhead seems awkward, as I have setup. I basically pass the full path to the file name to enter each class in my application. A ruby ​​script called pulls the log file from one of the arguments in ARGV, which is then passed and installed in each class that I call in ruby. In each class, I use patternFormatter to insert the class / file name into the log statement.

Is there a better way to make this work? It seems to me that no matter what I think, it will require that something be passed to each class in my ruby ​​application. Instead, I could set the log file in the yaml configuration file, but then I would go around and the configuration file for each class.

Any tips? If that doesn't make sense, I could try posting some more specific code examples to further explain what I mean.

Thanks!

+7
ruby logging log4r
source share
2 answers

Hmm, any reason you are not creating an instance of the Log4r::Logger class at the beginning of your script and not passing the instance? You don’t even need to pass it, you can always get it by name from the Logger class:

run.rb:

 require 'log4r' require 'class_a' logger = Log4r::Logger.new('test') logger.outputters << Log4r::Outputter.stdout logger.outputters << Log4r::FileOutputter.new('logtest', :filename => 'logtest.log') logger.info('started script') a = A.new a.do_something logger.info('finishing') 

class_a.rb:

 class A def do_something logger = Log4r::Logger['test'] logger.info('in do_something') puts 'hi!' end end 

and when you run run.rb you will get:

 $ ruby run.rb INFO test: started script INFO test: in do_something hi! INFO test: finishing 

and a log file named logtest.log on disk.

+12
source share

I support log4r,

For individual scripts (different .rb files), you can approach this in several ways (for example, I know), firstly, remember that the functions that I cover here are available in> = 1.1.4.

One way is to set another PatternFormatter line to the script (if you create a yaml or xml configuration file, you can specify different patterns for each class name).

Another way would be to use one of the GDC, NDC or MDC in PatternFormatter.

GDC will set the "Global Diagnostic Context", that is, this value, which is available for all threads running under the script. You can use it by putting% g in the template and setting the value via GDC.set (String) to get more details: http://log4r.rubyforge.org/manual.html

NDC and MDC are nested and associated diagnostic contexts, respectively. The template for them is to use% x and% X {Symbol | Object} and for their installation via NDC.set (String) and MDC.put (Symbol | Object, Object)

Another way would be to use the% t pattern, which prints the file name and line number where the call was made.

The trade-off between each of these methods is that they are gradually becoming more expensive in using CPU resources. I tend to use GDC first for what you ask.

+13
source share

All Articles