Class: EasyLog4r::Logger
- Inherits:
-
Object
- Object
- EasyLog4r::Logger
- Includes:
- Log4r
- Defined in:
- lib/easy_log4r/logger.rb
Overview
Class that creates Log4r::Loggers with some basic defaults and also modifies base classes to referece said Loggers.
Class Method Summary collapse
-
.add_logger(base, logger) ⇒ Object
Adds log4r class variable, as well as Base::log4r and Base#log4r.
-
.configured_default_outputters ⇒ Object
Gathers all Log4r::Outputters defined in the user defined configuration file.
-
.default_formatter ⇒ Object
Creates or returns the default formatter.
-
.default_logger(name) ⇒ Object
Provides the default Loggers, which are null_loggers with the default outputters.
-
.default_outputter(name, clas) ⇒ Object
- Takes a name and Logger class and gives back a predefined logger, or a new instance of the given Log4r
-
class.
-
.default_outputters ⇒ Object
Creates or returns default outputters.
-
.get_logger(base, null = false) ⇒ Object
Given a Class, this will return either an existing Log4r::Logger, that could be defined via XML or YAML options, or a fresh new logger, that has the name of the class, as represented by Class.to_s, as its name.
-
.null_logger(name) ⇒ Object
Creates a Log4r::Logger with the given name and clears all Outputters.
-
.standard_outputters ⇒ Object
Defines two standard outputters, one which prints to stdout at all levels, and one which prints to stderr for error and fatal levels.
Class Method Details
.add_logger(base, logger) ⇒ Object
Adds log4r class variable, as well as Base::log4r and Base#log4r
11 12 13 14 15 16 17 |
# File 'lib/easy_log4r/logger.rb', line 11 def self.add_logger(base, logger) base.class_variable_set(:@@log4r, logger) base.class_eval do def log4r; self.class.log4r; end def self.log4r; class_variable_get(:@@log4r); end end end |
.configured_default_outputters ⇒ Object
Gathers all Log4r::Outputters defined in the user defined configuration file.
55 56 57 |
# File 'lib/easy_log4r/logger.rb', line 55 def self.configured_default_outputters Log4r::Outputter.default_outputters end |
.default_formatter ⇒ Object
Creates or returns the default formatter.
The formatter is a Log4r::PatternFormatter with the following pattern:
"[%l] %d @@%c :: %m"
For example:
Base.log4r.info("This is only a test")
=> [INFO] 2012-07-30 12:00:00 PM @@Base :: This is only a test
76 77 78 79 80 81 |
# File 'lib/easy_log4r/logger.rb', line 76 def self.default_formatter return @formatter if @formatter @formatter = Log4r::PatternFormatter.new(:pattern => "[%l] %d @@%c :: %m") return @formatter end |
.default_logger(name) ⇒ Object
Provides the default Loggers, which are null_loggers with the default outputters.
21 22 23 24 25 26 |
# File 'lib/easy_log4r/logger.rb', line 21 def self.default_logger(name) logger = null_logger(name) logger.outputters = default_outputters return logger end |
.default_outputter(name, clas) ⇒ Object
- Takes a name and Logger class and gives back a predefined logger, or a new instance of the given Log4r
-
class
45 46 47 48 49 50 51 |
# File 'lib/easy_log4r/logger.rb', line 45 def self.default_outputter(name, clas) clas = Log4r.instance_eval{ clas } out = Log4r::Outputter[name] out = clas.new(name, :formatter => default_formatter) unless out return out end |
.default_outputters ⇒ Object
Creates or returns default outputters.
Those outputters are a Log4r::StdoutOutputter and a Log4r::StderrOutputter, both of which have a default Log4r::Formatter
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/easy_log4r/logger.rb', line 32 def self.default_outputters return @outputters if @outputters @outputters = [] @outputters << configured_default_outputters @outputters << standard_outputters if @outputters.empty? @outputters.flatten! return @outputters end |
.get_logger(base, null = false) ⇒ Object
Given a Class, this will return either an existing Log4r::Logger, that could be defined via XML or YAML options, or a fresh new logger, that has the name of the class, as represented by Class.to_s, as its name.
If the second option is given any value which resolves to true, it will provide a Log4r::Logger with no Outputters.
96 97 98 99 100 |
# File 'lib/easy_log4r/logger.rb', line 96 def self.get_logger(base, null=false) clas = base.to_s logger = null ? null_logger(clas) : Log4r::Logger[clas] return logger || default_logger(clas) end |
.null_logger(name) ⇒ Object
Creates a Log4r::Logger with the given name and clears all Outputters
85 86 87 88 89 90 |
# File 'lib/easy_log4r/logger.rb', line 85 def self.null_logger(name) logger = Log4r::Logger.new(name) logger.outputters = [] return logger end |
.standard_outputters ⇒ Object
Defines two standard outputters, one which prints to stdout at all levels, and one which prints to stderr for error and fatal levels.
61 62 63 64 65 |
# File 'lib/easy_log4r/logger.rb', line 61 def self.standard_outputters @outputters << default_outputter('formatted_stdout', StdoutOutputter) @outputters << default_outputter('formatted_stderr', StderrOutputter) Log4r::Outputter['formatted_stderr'].level = :error end |