Module: MultiLogger

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

Constant Summary collapse

VERSION =
"0.2.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
# File 'lib/multi_logger.rb', line 5

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

  raise "'#{name}' is reserved in #{rails_logger_class} and can not be used as a log accessor name." if rails_logger_class.method_defined?(name)

  logger = Logger.new(*extract_options(name, options))
  rails_logger_class.class_eval do
    define_method name.to_sym do
      logger
    end
  end

  logger.formatter = options[:formatter] if options[:formatter]
  logger
end

.extract_options(name, options) ⇒ Object



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

def extract_options(name, options)
  path = get_path(name, options[:path])

  if options[:shift_age] && options[:shift_size]
    [path, options[:shift_age], options[:shift_size]]
  elsif options[:shift_age]
    # options[:shift_age] => 'daily', 'weekly'
    [path, options[:shift_age]]
  else
    [path]
  end
end

.get_path(name, path = nil) ⇒ Object

Computes log file path



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

def get_path(name, path=nil)
  return path if path == STDOUT
  
  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



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

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