Class: Logion::LoggerPatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/logion/logger_patcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logion_instance) ⇒ LoggerPatcher

Returns a new instance of LoggerPatcher.



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

def initialize(logion_instance)
  self.logion_instance = logion_instance
  self.logger          = logion_instance.logger

  additional_logging =  proc do |*args, &block|
    log_to_separate_file *args, &block
  end

  logger.instance_variable_set '@additional_logging', additional_logging

  # we don't want to ruin your pretty logger, so lets extend it
  def logger.add(*args, &block)
    @additional_logging.call(*args, &block)
    super
  end
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/logion/logger_patcher.rb', line 3

def logger
  @logger
end

#logion_instanceObject

Returns the value of attribute logion_instance.



3
4
5
# File 'lib/logion/logger_patcher.rb', line 3

def logion_instance
  @logion_instance
end

Instance Method Details

#format_log_entry(severity, message = nil, progname = nil, *_rest) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/logion/logger_patcher.rb', line 33

def format_log_entry(severity, message = nil, progname = nil, *_rest)
  message ||= progname

  severity_name = Logger::Severity.constants.find do |name|
    Logger::Severity.const_get(name) == severity
  end

  color, background = severity_colors(severity_name)

  prefix = "#{ Process.pid } #{ '%10s' % "(#{ severity_name })" }:"
  prefix = ::Logion::Logion.colorize(prefix, color: color, background: background)

  "#{ prefix } #{ message }"
end

#log_to_separate_file(*args, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/logion/logger_patcher.rb', line 48

def log_to_separate_file(*args, &block)
  info_file = logion_instance.log_path_holder

  if File.exists?(info_file)
    separate_log    = File.read(info_file)
    formatted_entry = format_log_entry(*args, &block)
    open(separate_log, 'a') { |f| f.puts formatted_entry }
  end
end

#severity_colors(severity_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/logion/logger_patcher.rb', line 22

def severity_colors(severity_name)
  {
      WARN:    [:black, :yellow],
      DEBUG:   [:white, :blue],
      INFO:    [:black, :green],
      FATAL:   [:white, :red],
      ERROR:   [:black, :light_red],
      UNKNOWN: [:white, :black]
  }[severity_name]
end