Class: Kafo::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/logger.rb

Constant Summary collapse

PATTERN =
"[%5l %d %c] %m\n"
COLOR_LAYOUT =
Logging::Layouts::Pattern.new(:pattern => PATTERN, :color_scheme => 'bright')
NOCOLOR_LAYOUT =
Logging::Layouts::Pattern.new(:pattern => PATTERN, :color_scheme => nil)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.loggersObject



10
11
12
# File 'lib/kafo/logger.rb', line 10

def loggers
  @loggers ||= []
end

Class Method Details

.bufferObject



14
15
16
# File 'lib/kafo/logger.rb', line 14

def buffer
  @buffer ||= []
end

.buffering?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/kafo/logger.rb', line 82

def self.buffering?
  KafoConfigure.verbose.nil? || ((KafoConfigure.verbose && !loggers.detect {|l| l.name == 'verbose'}) || self.loggers.empty?)
end

.color_layoutObject



113
114
115
# File 'lib/kafo/logger.rb', line 113

def self.color_layout
  KafoConfigure.use_colors? ? COLOR_LAYOUT : NOCOLOR_LAYOUT
end

.dump_buffer(buffer) ⇒ Object



106
107
108
109
110
111
# File 'lib/kafo/logger.rb', line 106

def self.dump_buffer(buffer)
  buffer.each do |log|
    self.loggers.each { |logger| logger.send log[0], *([log[1]].flatten(1)), &log[2] }
  end
  buffer.clear
end

.dump_errorsObject



94
95
96
97
98
99
100
# File 'lib/kafo/logger.rb', line 94

def self.dump_errors
  setup_fatal_logger(color_layout) if loggers.empty?
  unless self.error_buffer.empty?
    loggers.each { |logger| logger.error 'Errors encountered during run:' }
    self.dump_buffer(self.error_buffer)
  end
end

.dump_needed?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/kafo/logger.rb', line 86

def self.dump_needed?
  !self.buffer.empty?
end

.error_bufferObject



18
19
20
# File 'lib/kafo/logger.rb', line 18

def error_buffer
  @error_buffer ||= []
end

.setupObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kafo/logger.rb', line 47

def self.setup
  begin
    FileUtils.mkdir_p(KafoConfigure.config.app[:log_dir], :mode => 0750)
  rescue Errno::EACCES
    puts "No permissions to create log dir #{KafoConfigure.config.app[:log_dir]}"
  end

  logger   = Logging.logger['main']
  filename = KafoConfigure.config.log_file
  begin
    logger.appenders = ::Logging.appenders.rolling_file('configure',
                                                        :filename => filename,
                                                        :layout   => NOCOLOR_LAYOUT,
                                                        :truncate => true
    )
    # set owner and group (it's ignored if attribute is nil)
    FileUtils.chown KafoConfigure.config.app[:log_owner], KafoConfigure.config.app[:log_group], filename
  rescue ArgumentError
    puts "File #{filename} not writeable, won't log anything to file!"
  end

  logger.level = KafoConfigure.config.app[:log_level]
  self.loggers << logger

  setup_fatal_logger(color_layout) unless loggers.detect {|l| l.name == 'verbose'}
end

.setup_fatal_logger(layout) ⇒ Object



40
41
42
43
44
45
# File 'lib/kafo/logger.rb', line 40

def self.setup_fatal_logger(layout)
  fatal_logger           = Logging.logger['fatal']
  fatal_logger.level     = 'fatal'
  fatal_logger.appenders = [::Logging.appenders.stderr(:layout => layout)]
  self.loggers << fatal_logger
end

.setup_verboseObject



74
75
76
77
78
79
80
# File 'lib/kafo/logger.rb', line 74

def self.setup_verbose
  logger           = Logging.logger['verbose']
  logger.level     = (KafoConfigure.config && KafoConfigure.config.app[:verbose_log_level]) || :info
  layout           = color_layout
  logger.appenders = [::Logging.appenders.stdout(:layout => layout)]
  self.loggers<< logger
end

.to_buffer(buffer, *args) ⇒ Object



90
91
92
# File 'lib/kafo/logger.rb', line 90

def self.to_buffer(buffer, *args)
  buffer << args
end

Instance Method Details

#dump_errorsObject



102
103
104
# File 'lib/kafo/logger.rb', line 102

def dump_errors
  self.class.dump_errors
end

#log(name, *args, &block) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/kafo/logger.rb', line 117

def log(name, *args, &block)
  if self.class.buffering?
    self.class.to_buffer(self.class.buffer, name, args, &block)
  else
    self.class.dump_buffer(self.class.buffer) if self.class.dump_needed?
    self.class.loggers.each { |logger| logger.send name, *args, &block }
  end
end