Class: Logz::MultiLogger

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/logz/multi_logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder = Logz.config.folder, loggers: []) ⇒ MultiLogger

Returns a new instance of MultiLogger.



6
7
8
9
10
# File 'lib/logz/multi_logger.rb', line 6

def initialize(folder = Logz.config.folder, loggers: [])
  @loggers = {}
  @folder = set_and_create_folder(folder)
  Logz.config.loggers.dup.push(*loggers).each { |name| add(name) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/logz/multi_logger.rb', line 58

def method_missing(m, *args, &block)
  if loggers.has_key?(m)
    if args.empty?
      loggers[m]
    else
      puts "Invalid method for logger '#{m}': #{args.join(", ")}"
    end
  elsif default_logger.has_level?(m)
    default_logger.logger.send(m, *args, &block)
  elsif default_logger.respond_to?(m)
    default_logger.send(m, *args, &block)
  else
    puts "Logger '#{m}' not found. Current loggers: #{loggers.keys.join(", ")}"
  end
end

Instance Attribute Details

#folderObject

Returns the value of attribute folder.



4
5
6
# File 'lib/logz/multi_logger.rb', line 4

def folder
  @folder
end

#loggersObject

Returns the value of attribute loggers.



4
5
6
# File 'lib/logz/multi_logger.rb', line 4

def loggers
  @loggers
end

Instance Method Details

#<<(name, path = "", to_stdout: nil, to_file: nil) ⇒ Object



23
24
25
# File 'lib/logz/multi_logger.rb', line 23

def <<(name, path = "", to_stdout: nil, to_file: nil)
  add(name, path, to_stdout: to_stdout, to_file: to_file)
end

#[](name) ⇒ Object



40
41
42
# File 'lib/logz/multi_logger.rb', line 40

def [](name)
  loggers[name.to_sym]
end

#add(name, path = "", to_stdout: Logz.config.output_to_stdout, to_file: Logz.config.output_to_file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/logz/multi_logger.rb', line 12

def add(name, path = "", to_stdout: Logz.config.output_to_stdout, to_file: Logz.config.output_to_file)
  if name.is_a?(Array)
    name.each { |log| add(log, path, to_stdout: to_stdout, to_file: to_file) }
  elsif name == STDOUT || name.to_s == "stdout"
    @loggers[:stdout] = LoggerWrapper.new(STDOUT)
  else
    output_stream = set_output_stream(name, path, to_stdout, to_file)
    @loggers[name.to_sym] = LoggerWrapper.new(output_stream)
  end
end

#default_loggerObject



54
55
56
# File 'lib/logz/multi_logger.rb', line 54

def default_logger
  loggers[:stdout]
end

#eachObject



34
35
36
37
38
# File 'lib/logz/multi_logger.rb', line 34

def each
  loggers.each do |name, logger|
    yield(logger)
  end
end

#global_levelObject Also known as: level



48
49
50
# File 'lib/logz/multi_logger.rb', line 48

def global_level
  default_logger.level
end

#global_level=(level) ⇒ Object



44
45
46
# File 'lib/logz/multi_logger.rb', line 44

def global_level=(level)
  loggers.each { |name, logger| logger.level = level }
end

#remove(name) ⇒ Object Also known as: delete



27
28
29
30
# File 'lib/logz/multi_logger.rb', line 27

def remove(name)
  @loggers[name.to_sym].close
  @loggers.delete(name.to_sym)
end