Module: CemAcpt::Logging

Overview

Logging for CemAcpt

Defined Under Namespace

Classes: MultiLogger

Constant Summary collapse

LEVEL_MAP =
{
  'debug' => Logger::DEBUG,
  'info' => Logger::INFO,
  'warn' => Logger::WARN,
  'error' => Logger::ERROR,
  'fatal' => Logger::FATAL,
  'unknown' => Logger::UNKNOWN,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.current_log_configHash

Returns the current log config if set, or the default if not.

Returns:

  • (Hash)

    the current log config



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cem_acpt/logging.rb', line 134

def current_log_config
  return @log_config if @log_config

  formatter = new_log_formatter(current_log_format)
  @log_config = {
    logdev: $stdout,
    shift_age: 'o',
    shift_size: 1_048_576,
    level: Logger::INFO,
    progname: 'CemAcpt',
    datetime_format: '%Y%m%dT%H%M%S%z',
    formatter: formatter,
  }
  @log_config
end

.current_log_formatSymbol

Shows the current log format style if set, or the default if not.

Returns:

  • (Symbol)

    the current log format style



86
87
88
# File 'lib/cem_acpt/logging.rb', line 86

def current_log_format
  @current_log_format ||= :text
end

.current_log_levelLogger::Severity

Shortcut method for logger.level

Returns:

  • (Logger::Severity)


72
73
74
# File 'lib/cem_acpt/logging.rb', line 72

def current_log_level
  logger.level
end

.included(base) ⇒ Object

Provides class method wrappers for logging methods



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/cem_acpt/logging.rb', line 190

def self.included(base)
  class << base
    def new_logger(*logdevs, **configs)
      CemAcpt::Logging.new_logger(*logdevs, **configs)
    end

    def logger
      CemAcpt::Logging.logger
    end

    def current_log_level
      CemAcpt::Logging.current_log_level
    end

    def new_log_level(level)
      CemAcpt::Logging.new_log_level(level)
    end

    def current_log_format
      CemAcpt::Logging.current_log_format
    end

    def new_log_formatter(f)
      CemAcpt::Logging.new_log_formatter(f)
    end

    def current_log_config
      CemAcpt::Logging.current_log_config
    end

    def new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil)
      CemAcpt::Logging.new_log_config(logdev: logdev, shift_age: shift_age, shift_size: shift_size, level: level, formatter: formatter, datetime_format: datetime_format)
    end
  end
end

.loggerLogger

Exposes a logger instance. Will either use the currently set logger or create a new one.

Returns:

  • (Logger)


61
62
63
64
65
66
67
68
# File 'lib/cem_acpt/logging.rb', line 61

def logger
  @logger ||= Logger.new(
    current_log_config[:logdev],
    current_log_config[:shift_age],
    current_log_config[:shift_size],
    **current_log_config.reject { |k, _| %i[logdev shift_age shift_size].include?(k) }
  )
end

.new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) ⇒ Object

Creates a new log config hash and a new Logger instance using that config and sets the new Logger instance as the current logger. NO DEFAULT VALUES ARE SET.

Parameters:

  • logdev (String, IO) (defaults to: nil)

    the log device to use. If 'stdout' or 'stderr' are passed, the respective IO object will be used. Otherwise, Strings will be treated as file paths. If an IO object is passed, it will be used directly. If no logdev is passed, or an invalid logdev is passed, the default is $stdout.

  • shift_age (String) (defaults to: nil)

    the log rotation age

  • shift_size (Integer) (defaults to: nil)

    the log rotation size

  • level (Logger::Severity) (defaults to: nil)

    the log level

  • formatter (Proc) (defaults to: nil)

    the log formatter

  • datetime_format (String) (defaults to: nil)

    the datetime format



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/cem_acpt/logging.rb', line 161

def new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil)
  @log_config[:shift_age] = shift_age if shift_age
  @log_config[:shift_size] = shift_size if shift_size
  @log_config[:level] = level if level
  @log_config[:formatter] = formatter if formatter
  @log_config[:datetime_format] = datetime_format if datetime_format
  case logdev
  when 'stdout'
    @log_config[:logdev] = $stdout
  when 'stderr'
    @log_config[:logdev] = $stderr
  when String
    @log_config[:logdev] = target
  when IO
    @log_config[:logdev] = logdev
  else
    @log_config[:logdev] = $stdout
    logger.warn("Unknown log target: #{logdev.inspect}, using STDOUT")
  end
  @logger = Logger.new(
    @log_config[:logdev],
    @log_config[:shift_age],
    @log_config[:shift_size],
    **@log_config.reject { |k, _| %i[logdev shift_age shift_size].include?(k) },
  )
end

.new_log_formatter(f) ⇒ Proc

Sets the current log format style and returns a proc to be passed to Logger#formatter=

Parameters:

  • f (Symbol)

    the log format style to set

Returns:

  • (Proc)

    the proc to be passed to Logger#formatter=



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cem_acpt/logging.rb', line 94

def new_log_formatter(f)
  case f.downcase.to_sym
  when :json
    require 'json'
    @current_log_format = :json
    proc do |severity, datetime, progname, msg|
      {
        timestamp: datetime,
        progname: progname,
        severity: severity,
        message: msg,
      }.to_json + "\n"
    end
  when :text
    @current_log_format = :text
    proc do |severity, _datetime, _progname, msg|
      "#{severity} - #{msg}\n"
    end
  when :github_action
    @current_log_format = :github_action
    sev_map = {
      'DEBUG' => '::debug::',
      'INFO' => '::notice::',
      'WARN' => '::warning::',
      'ERROR' => '::error::',
      'FATAL' => '::error::',
    }
    proc do |severity, _datetime, _progname, msg|
      "#{sev_map[severity]}{#{msg}}\n"
    end
  else
    @current_log_format = :file
    proc do |severity, datetime, _progname, msg|
      "[#{datetime}] #{severity}: #{msg}\n"
    end
  end
end

.new_log_level(level) ⇒ Object

Shortcut method to set logger.level

Parameters:

  • level (String)

    the log level to set

Raises:

  • (ArgumentError)


78
79
80
81
82
# File 'lib/cem_acpt/logging.rb', line 78

def new_log_level(level)
  raise ArgumentError, 'Log level not recognized' unless LEVEL_MAP[level.downcase]

  @logger.level = LEVEL_MAP[level.downcase]
end

.new_logger(*logdevs, **configs) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cem_acpt/logging.rb', line 39

def new_logger(*logdevs, **configs)
  new_configs = current_log_config.merge(configs.reject { |_, v| v.nil? })
  if new_configs.key?(:level) && !LEVEL_MAP.values.include?(new_configs[:level])
    new_configs[:level] = LEVEL_MAP[new_configs[:level].downcase]
  end
  loggers = logdevs.map do |dev|
    logdev = dev.is_a?(String) ? $stdout : dev
    logger = Logger.new(
      logdev,
      new_configs[:shift_age],
      new_configs[:shift_size],
      **new_configs.reject { |k, _| %i[logdev shift_age shift_size].include?(k) },
    )
    logger.reopen(dev) if dev.is_a?(String)
    logger
  end
  @logger = CemAcpt::Logging::MultiLogger.new(*loggers)
end

Instance Method Details

#current_log_configObject

Exposes the current log config



254
255
256
# File 'lib/cem_acpt/logging.rb', line 254

def current_log_config
  CemAcpt::Logging.current_log_config
end

#current_log_formatObject



245
246
247
# File 'lib/cem_acpt/logging.rb', line 245

def current_log_format
  CemAcpt::Logging.current_log_format
end

#current_log_levelObject

Exposes the current log level



236
237
238
# File 'lib/cem_acpt/logging.rb', line 236

def current_log_level
  CemAcpt::Logging.current_log_level
end

#loggerObject

Exposes the logger instance



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

def logger
  CemAcpt::Logging.logger
end

#new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil) ⇒ Object

Exposes setting a new log config



259
260
261
# File 'lib/cem_acpt/logging.rb', line 259

def new_log_config(logdev: nil, shift_age: nil, shift_size: nil, level: nil, formatter: nil, datetime_format: nil)
  CemAcpt::Logging.new_log_config(logdev: logdev, shift_age: shift_age, shift_size: shift_size, level: level, formatter: formatter, datetime_format: datetime_format)
end

#new_log_formatter(f) ⇒ Object



249
250
251
# File 'lib/cem_acpt/logging.rb', line 249

def new_log_formatter(f)
  CemAcpt::Logging.new_log_formatter(f)
end

#new_log_level(level) ⇒ Object

Exposes setting the log level



241
242
243
# File 'lib/cem_acpt/logging.rb', line 241

def new_log_level(level)
  CemAcpt::Logging.new_log_level(level)
end

#new_logger(*logdevs, **configs) ⇒ Object



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

def new_logger(*logdevs, **configs)
  CemAcpt::Logging.new_logger(*logdevs, **configs)
end