Module: MultiLogger

Defined in:
lib/multi_logger.rb,
lib/multi_logger/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.add_logger(name, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/multi_logger.rb', line 5

def add_logger(name, options={})
  name = name.to_s
  rails_logger_class = get_rails_logger_class()

  if rails_logger_class.method_defined?(name)
    raise "'#{name}' is reserved in #{rails_logger_class} and can not be used as a log accessor name."
  else
    logger = Logger.new(*extract_options(name, options))
    rails_logger_class.class_eval do
      define_method name.to_sym do
        logger
      end
    end
    if options[:formatter]
      logger.formatter = options[:formatter]
    end
    logger
  end
end

.extract_options(name, options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/multi_logger.rb', line 49

def extract_options(name, options)
  if options[:shift_age] && options[:shift_size]
    [get_path(name, options[:path]), options[:shift_age], options[:shift_size]]
  elsif options[:shift_age]
    # options[:shift_age] => 'daily', 'weekly'
    [get_path(name, options[:path]), options[:shift_age]]
  else
    [get_path(name, options[:path])]
  end
end

.get_path(name, path = nil) ⇒ Object

Computes log file path



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/multi_logger.rb', line 26

def get_path(name, path=nil)
  if path.nil?
    path = name.underscore
  end
  if !path.include?('/')
    path = Rails.root.join('log',path).to_s
  end
  if !path.end_with?('.log')
    path += '.log'
  end
  path
end

.get_rails_logger_classObject



39
40
41
42
43
44
45
46
47
# File 'lib/multi_logger.rb', line 39

def get_rails_logger_class
  if defined?(ActiveSupport::BufferedLogger)
    ActiveSupport::BufferedLogger
  elsif defined?(ActiveSupport::Logger)
    ActiveSupport::Logger
  else
    raise 'Rails logger not found'
  end
end