Class: Kafo::Logging

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

Constant Summary collapse

LOG_LEVELS =
[:debug, :info, :notice, :warn, :error, :fatal]

Class Method Summary collapse

Class Method Details

.add_logger(name) ⇒ Object



89
90
91
# File 'lib/kafo/logging.rb', line 89

def add_logger(name)
  ::Logging.logger[name]
end

.bufferObject



103
104
105
# File 'lib/kafo/logging.rb', line 103

def buffer
  @buffer ||= []
end

.buffering?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/kafo/logging.rb', line 107

def buffering?
  root_logger.appenders.empty?
end

.dump_bufferObject



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

def dump_buffer
  @buffer.each do |log|
    ::Logging.logger[log[0]].send(:log_event, log[1])
  end
  @buffer.clear
end

.dump_needed?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/kafo/logging.rb', line 111

def dump_needed?
  !buffer.empty?
end

.layout(color: false) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/kafo/logging.rb', line 81

def layout(color: false)
  ::Logging::Layouts::Pattern.new(
    pattern: "%d [%-6l] [%c] %m\n",
    color_scheme: color ? 'bright' : nil,
    date_pattern: '%Y-%m-%d %H:%M:%S'
  )
end

.root_loggerObject



16
17
18
# File 'lib/kafo/logging.rb', line 16

def root_logger
  @root_logger ||= ::Logging.logger.root
end

.set_color_schemeObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kafo/logging.rb', line 63

def set_color_scheme
  ::Logging.color_scheme(
    'bright',
    :levels => {
      :info => :cyan,
      :notice => :green,
      :warn  => :yellow,
      :error => :red,
      :fatal => [:white, :on_red]
    },
    :date   => :blue,
    :logger => :cyan,
    :line   => :yellow,
    :file   => :yellow,
    :method => :yellow
  )
end

.setup(verbose: false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kafo/logging.rb', line 20

def setup(verbose: false)
  set_color_scheme

  level = KafoConfigure.config.app[:log_level]

  setup_file_logging(
    level,
    KafoConfigure.config.app[:log_dir],
    KafoConfigure.config.app[:log_owner],
    KafoConfigure.config.app[:log_group]
  )
  setup_verbose(level: KafoConfigure.config.app[:verbose_log_level] || level) if verbose
end

.setup_file_logging(log_level, log_dir, log_owner, log_group) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kafo/logging.rb', line 34

def setup_file_logging(log_level, log_dir, log_owner, log_group)
  filename = KafoConfigure.config.log_file

  begin
    FileUtils.mkdir_p(log_dir, :mode => 0750)
  rescue Errno::EACCES
    puts "No permissions to create log dir #{log_dir}"
  end

  begin
    root_logger.appenders = ::Logging.appenders.rolling_file(
      'configure',
      level: log_level,
      filename: filename,
      layout: layout(color: false),
      truncate: true,
      roll_by: 'date'
    )

    FileUtils.chown(
      log_owner,
      log_group,
      filename
    )
  rescue ArgumentError
    puts "File #{filename} not writeable, won't log anything to file!"
  end
end

.setup_verbose(level: :notice) ⇒ Object



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

def setup_verbose(level: :notice)
  root_logger.add_appenders(
    ::Logging.appenders.stdout(
      'verbose',
      layout: layout(color: KafoConfigure.use_colors?),
      level: level
    )
  )
end

.to_buffer(*args) ⇒ Object



115
116
117
# File 'lib/kafo/logging.rb', line 115

def to_buffer(*args)
  buffer << args
end