Module: Logging

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

Overview

The Logging module

Constant Summary collapse

FORWARD_SLASH_PATTERN =
%r{/}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configObject

rubocop: disable Metrics/MethodLength



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/logging.rb', line 22

def config
  @config ||= begin
    lib_dir_path = File.expand_path(__dir__)
    project_dir_path = File.expand_path(File.dirname(lib_dir_path))
    logs_dir_path = File.expand_path(File.join(project_dir_path, 'logs'))
    FileUtils.mkdir_p(logs_dir_path)
    log_file_path = File.expand_path(File.join(logs_dir_path, 'server.log'))
    FileUtils.touch(log_file_path)
    {
      level: Logger::INFO,
      name: 'websocket',
      lib_dir_path: lib_dir_path,
      project_dir_path: project_dir_path,
      logs_dir_path: logs_dir_path,
      log_file_path: log_file_path,
      rolling_log_file_name_template: File.expand_path(
        File.join(logs_dir_path, 'server-%d{yyyy-MM-dd}.log.gz')),
      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

.init_log4j(log_level = org.apache.logging.log4j.Level::INFO) ⇒ Object

rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/logging.rb', line 62

def init_log4j(log_level = org.apache.logging.log4j.Level::INFO)
  java.lang::System.setProperty('log4j.shutdownHookEnabled', java.lang::Boolean.toString(false))
  config = ConfigurationBuilderFactory.newConfigurationBuilder()

  log_level = org.apache.logging.log4j::Level.to_level(log_level.to_s.upcase) if log_level.is_a? Symbol
  config.setStatusLevel(log_level)
  config.setConfigurationName('websocket')

  # create a console appender
  target = ConsoleAppender::Target::SYSTEM_OUT
  pattern = Logging.config[:logging_pattern_template][:java]
  layout = config.newLayout('PatternLayout')
  layout = layout.addAttribute('pattern', pattern)
  appender = config.newAppender('stdout', 'CONSOLE')
  appender = appender.addAttribute('target', target)
  appender = appender.add(layout)
  config.add(appender)

  # create a root logger
  root_logger = config.newRootLogger(log_level)
  root_logger = root_logger.add(config.newAppenderRef('stdout'))

  # create a rolling file appender
  cron = config.newComponent('CronTriggeringPolicy')
  cron = cron.addAttribute('schedule', '0 0 0 * * ?')

  size = config.newComponent('SizeBasedTriggeringPolicy')
  size = size.addAttribute('size', '100M')

  policies = config.newComponent('Policies')
  policies = policies.addComponent(cron)
  policies = policies.addComponent(size)

  appender = config.newAppender('rolling_file', 'RollingFile')
  appender = appender.addAttribute('fileName', Logging.config[:log_file_path])
  appender = appender.addAttribute('filePattern', Logging.config[:rolling_log_file_name_template])
  appender = appender.add(layout)
  appender = appender.addComponent(policies)
  config.add(appender)

  root_logger = root_logger.addAttribute('additivity', false)
  root_logger = root_logger.add(config.newAppenderRef('rolling_file'))
  config.add(root_logger)

  logging_configuration = config.build()
  ctx = Configurator.initialize(logging_configuration)
  ctx.updateLoggers()
end

.log_levelObject



225
226
227
# File 'lib/logging.rb', line 225

def log_level
  Logging.config[:level]
end

.log_level=(level) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity



220
221
222
# File 'lib/logging.rb', line 220

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

Instance Method Details

#get_formatted_logger_name(logger_name = nil) ⇒ Object



190
191
192
193
194
# File 'lib/logging.rb', line 190

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



182
183
184
185
186
187
188
# File 'lib/logging.rb', line 182

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(FORWARD_SLASH_PATTERN).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



161
162
163
164
# File 'lib/logging.rb', line 161

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



173
174
175
176
177
178
179
180
# File 'lib/logging.rb', line 173

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(FORWARD_SLASH_PATTERN).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



230
231
232
# File 'lib/logging.rb', line 230

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

#ruby_log_formatter(severity_level, datetime, program_name, message) ⇒ Object



166
167
168
169
170
171
# File 'lib/logging.rb', line 166

def ruby_log_formatter(severity_level, datetime, program_name, message)
  format(
    Logging.config[:logging_pattern_template][:ruby],
    timestamp: datetime.strftime(Logging.config[:logging_timestamp_format]),
    progname: program_name, severity: severity_level, 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



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/logging.rb', line 206

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