Module: Logging

Included in:
Class, Module, Object
Defined in:
lib/runtime/logging.rb,
lib/runtime/logging.rb

Overview

The Logging module

Constant Summary collapse

ForwardSlashPattern =
%r{/}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configObject

rubocop: disable Metrics/MethodLength



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/runtime/logging.rb', line 31

def config
  @config ||= begin
    support_dir_path = File.expand_path(__dir__)
    lib_dir_path = File.expand_path(File.dirname(support_dir_path))
    project_dir_path = File.expand_path(File.dirname(lib_dir_path))
    app_name = File.basename(project_dir_path)
    logs_dir_path = File.expand_path(File.join(project_dir_path, 'logs'))
    FileUtils.mkdir_p(logs_dir_path)
    log_file = File.expand_path(File.join(logs_dir_path, "#{app_name}.log"))
    {
      level: :info,
      name: app_name,
      lib_dir_path: lib_dir_path,
      project_dir_path: project_dir_path,
      logs_dir_path: logs_dir_path,
      log_file: log_file,
      rolling_log_file_name_template: "#{app_name}-%d{yyyy-MM-dd}.log.gz",
      logging_timestamp_format: '%Y-%m-%d %H:%M:%S',
      logging_pattern_template: {
        java: '%d{ABSOLUTE} %-5p [%c{1}] %m%n',
        ruby: "%<timestamp>s %-5<severity>s [%<progname>s] %<msg>s\n"
      },
      schedule: '0 0 0 * * ?',
      size: '100M'
    }
  end
end

.log_levelObject



169
170
171
# File 'lib/runtime/logging.rb', line 169

def log_level
  Logging.config[:level]
end

.log_level=(level) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity



164
165
166
# File 'lib/runtime/logging.rb', line 164

def log_level=(level)
  Logging.config[:level] = symbolize_numeric_log_level(level)
end

Instance Method Details

#get_formatted_logger_name(logger_name = nil) ⇒ Object



134
135
136
137
138
# File 'lib/runtime/logging.rb', line 134

def get_formatted_logger_name(logger_name = nil)
  return logger_name.to_s[/\w+$/] unless logger_name.nil?
  return name[/\w+$/] if is_a?(Class) || is_a?(Module)
  self.class.name[/\w+$/]
end

#init_java_logger(level = nil, logger_name = nil, source_location = nil) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/runtime/logging.rb', line 126

def init_java_logger(level = nil, logger_name = nil, source_location = nil)
  logger_name = get_formatted_logger_name(logger_name)
  logger_name = source_location.split(ForwardSlashPattern).last if logger_name.empty?
  log = LogManager.getLogger(logger_name)
  log.level = Level.to_level(level.to_s.upcase) unless level.nil?
  log
end

#init_logger(level = :info, logger_name = nil) ⇒ Object



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

def init_logger(level = :info, logger_name = nil)
  return init_java_logger(level, logger_name, caller[2]) if defined?(Java)
  init_ruby_logger(level, logger_name, caller[2])
end

#init_ruby_logger(level = nil, logger_name = nil, source_location = nil) ⇒ Object



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

def init_ruby_logger(level = nil, logger_name = nil, source_location = nil)
  logger_name = get_formatted_logger_name(logger_name)
  logger_name = source_location.split(ForwardSlashPattern).last if logger_name.empty?
  log = Logger.new($stdout, progname: logger_name)
  log.level = level.to_s unless level.nil?
  log.formatter = method(:ruby_log_formatter)
  log
end

#log(level = Logging.log_level, log_name = ) ⇒ Object Also known as: logger



174
175
176
# File 'lib/runtime/logging.rb', line 174

def log(level = Logging.log_level, log_name = Logging.config[:app_name])
  @log ||= init_logger(level, log_name)
end

#ruby_log_formatter(severity, datetime, progname, message) ⇒ Object



108
109
110
111
112
113
# File 'lib/runtime/logging.rb', line 108

def ruby_log_formatter(severity, datetime, progname, message)
  format(
    Logging.config[:logging_pattern_template][:ruby],
    timestamp: datetime.strftime(Logging.config[:logging_timestamp_format]),
    progname: progname, severity: severity, msg: message)
end

#symbolize_numeric_log_level(level) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity OFF: 0 FATAL: 100 ERROR: 200 WARN: 300 INFO: 400 DEBUG: 500 TRACE: 600 ALL: 2147483647 See: logging.apache.org/log4j/2.x/log4j-api/apidocs/org/apache/logging/log4j/Level.html



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/runtime/logging.rb', line 150

def symbolize_numeric_log_level(level)
  case level
  when 5..Float::INFINITY then :off
  when 4 then :fatal
  when 3 then :error
  when 2 then :warn
  when 1 then :info
  when 0 then :debug
  when -1 then :trace
  when -Float::INFINITY..-2 then :all
  end
end