Class: MultiLogger
- Inherits:
-
ActiveSupport::BufferedLogger
- Object
- ActiveSupport::BufferedLogger
- MultiLogger
- Defined in:
- lib/multilogger.rb
Overview
Extend rails’ logger to support multiple targets. Patterns are added with ‘add_log`, then when a message is added to the log, it is checked against the patterns. example:
logger = MultiLogger.new("log.txt")
logger.add_log("Delayed::Job", "dj_log.txt")
Instance Method Summary collapse
- #__old_add ⇒ Object
- #add(severity, message = nil, progname = nil, &block) ⇒ Object
-
#add_log(pattern, target) ⇒ Object
pattern can be either a String or a Regexp.
-
#initialize(target, level = DEBUG) ⇒ MultiLogger
constructor
A new instance of MultiLogger.
Constructor Details
#initialize(target, level = DEBUG) ⇒ MultiLogger
Returns a new instance of MultiLogger.
15 16 17 18 |
# File 'lib/multilogger.rb', line 15 def initialize(target, level=DEBUG) super(target, level) @extra_logs = {} end |
Instance Method Details
#__old_add ⇒ Object
20 |
# File 'lib/multilogger.rb', line 20 alias __old_add add |
#add(severity, message = nil, progname = nil, &block) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/multilogger.rb', line 22 def add(severity, =nil, progname=nil, &block) @extra_logs.keys.each do |pattern| if && .respond_to?(:match) && .match(pattern) return @extra_logs[pattern].add(severity, , progname, &block) end end __old_add(severity, , progname, &block) end |
#add_log(pattern, target) ⇒ Object
pattern can be either a String or a Regexp.
10 11 12 13 |
# File 'lib/multilogger.rb', line 10 def add_log(pattern, target) @extra_logs[pattern] = ActiveSupport::BufferedLogger.new(target) return self # just so we can't accidentally grab the sub-log and do something stupid with it. end |