Class: OpenC3::Logger

Inherits:
Object show all
Defined in:
lib/openc3/utilities/logger.rb

Overview

Supports different levels of logging and only writes if the level is exceeded.

Constant Summary collapse

DEBUG =

Levels in increasing severity. Logging at a level prints that level and everything above it, e.g. WARN prints WARN, ERROR and FATAL.

::Logger::DEBUG
INFO =
::Logger::INFO
WARN =
::Logger::WARN
ERROR =
::Logger::ERROR
FATAL =
::Logger::FATAL
DEBUG_LEVEL =

Level names as they appear in log messages and OPENC3_LOG_LEVEL

'DEBUG'
INFO_LEVEL =
'INFO'
WARN_LEVEL =
'WARN'
ERROR_LEVEL =
'ERROR'
FATAL_LEVEL =
'FATAL'
LEVELS =
{
  DEBUG_LEVEL => DEBUG,
  INFO_LEVEL => INFO,
  WARN_LEVEL => WARN,
  ERROR_LEVEL => ERROR,
  FATAL_LEVEL => FATAL
}.freeze
LOG =

Types

'log'
NOTIFICATION =
'notification'
ALERT =
'alert'
EPHEMERAL =
'ephemeral'
@@mutex =
Mutex.new
@@instance =
nil
@@scope =
ENV.fetch('OPENC3_SCOPE', 'DEFAULT')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = Logger.default_level) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • level (Integer) (defaults to: Logger.default_level)

    The initial logging level



93
94
95
96
97
98
99
100
# File 'lib/openc3/utilities/logger.rb', line 93

def initialize(level = Logger.default_level)
  @stdout = true
  @level = level
  @detail_string = nil
  @container_name = Socket.gethostname
  @microservice_name = nil
  @no_store = ENV.fetch('OPENC3_NO_STORE', nil)
end

Instance Attribute Details

#microservice_nameString

Returns Microservice name.

Returns:

  • (String)

    Microservice name



41
42
43
# File 'lib/openc3/utilities/logger.rb', line 41

def microservice_name
  @microservice_name
end

Class Method Details

.build_log_data(log_level, message, user: nil, type: nil, url: nil, other: nil) ⇒ Object



212
213
214
# File 'lib/openc3/utilities/logger.rb', line 212

def self.build_log_data(log_level, message, user: nil, type: nil, url: nil, other: nil)
  self.instance.build_log_data(log_level, message, user: user, type: type, url: url, other: other)
end

.debug(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



144
145
146
# File 'lib/openc3/utilities/logger.rb', line 144

def self.debug(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  self.instance.debug(message, scope: scope, user: user, type: type, url: url, other: other, &block)
end

.default_levelInteger

Returns Level named by the OPENC3_LOG_LEVEL env var. INFO if the var is unset or isn't a level name.

Returns:

  • (Integer)

    Level named by the OPENC3_LOG_LEVEL env var. INFO if the var is unset or isn't a level name.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/openc3/utilities/logger.rb', line 78

def self.default_level
  level = ENV.fetch('OPENC3_LOG_LEVEL', '')
  name = level.to_s.strip.upcase
  return INFO if name.empty?

  LEVELS.fetch(name) do
    unless @warned_bad_level
      @warned_bad_level = true
      $stderr.puts "OPENC3_LOG_LEVEL '#{level}' is not one of #{LEVELS.keys.join(', ')}, using INFO"
    end
    INFO
  end
end

.error(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



159
160
161
# File 'lib/openc3/utilities/logger.rb', line 159

def self.error(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  self.instance.error(message, scope: scope, user: user, type: type, url: url, other: other, &block)
end

.fatal(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



164
165
166
# File 'lib/openc3/utilities/logger.rb', line 164

def self.fatal(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  self.instance.fatal(message, scope: scope, user: user, type: type, url: url, other: other, &block)
end

.info(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



149
150
151
# File 'lib/openc3/utilities/logger.rb', line 149

def self.info(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  self.instance.info(message, scope: scope, user: user, type: type, url: url, other: other, &block)
end

.instanceLogger

Returns The logger instance.

Returns:

  • (Logger)

    The logger instance



169
170
171
172
173
174
175
176
# File 'lib/openc3/utilities/logger.rb', line 169

def self.instance
  return @@instance if @@instance

  @@mutex.synchronize do
    @@instance ||= self.new
  end
  @@instance
end

.microservice_nameObject



107
108
109
# File 'lib/openc3/utilities/logger.rb', line 107

def self.microservice_name
  self.instance.microservice_name
end

.microservice_name=(name) ⇒ Object



111
112
113
# File 'lib/openc3/utilities/logger.rb', line 111

def self.microservice_name=(name)
  self.instance.microservice_name = name
end

.scopeObject



178
179
180
# File 'lib/openc3/utilities/logger.rb', line 178

def self.scope
  return @@scope
end

.scope=(scope) ⇒ Object



182
183
184
# File 'lib/openc3/utilities/logger.rb', line 182

def self.scope=(scope)
  @@scope = scope
end

.warn(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



154
155
156
# File 'lib/openc3/utilities/logger.rb', line 154

def self.warn(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  self.instance.warn(message, scope: scope, user: user, type: type, url: url, other: other, &block)
end

Instance Method Details

#build_log_data(log_level, message, user: nil, type: nil, url: nil, other: nil, &block) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/openc3/utilities/logger.rb', line 194

def build_log_data(log_level, message, user: nil, type: nil, url: nil, other: nil, &block)
  time = Time.now.utc
  # timestamp iso8601 with 6 decimal places to match the python output format
  data = { time: time.to_nsec_from_epoch, '@timestamp' => time.iso8601(6), level: log_level }
  data[:microservice_name] = @microservice_name if @microservice_name
  data[:detail] = @detail_string if @detail_string
  data[:user] = user if user # Enterprise: If a user is passed, put its name. Don't include user data if no user was passed.
  if block_given?
    message = yield
  end
  data[:container_name] = @container_name
  data[:message] = message if message
  data[:type] = type if type
  data[:url] = url if url
  data = data.merge(other) if other
  return data
end

#debug(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



119
120
121
# File 'lib/openc3/utilities/logger.rb', line 119

def debug(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  log_message(DEBUG_LEVEL, message, scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= DEBUG
end

#detail_stringString

Returns Additional detail to add to messages.

Returns:

  • (String)

    Additional detail to add to messages



38
# File 'lib/openc3/utilities/logger.rb', line 38

instance_attr_accessor :detail_string

#error(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



134
135
136
# File 'lib/openc3/utilities/logger.rb', line 134

def error(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  log_message(ERROR_LEVEL, message, scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= ERROR
end

#fatal(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



139
140
141
# File 'lib/openc3/utilities/logger.rb', line 139

def fatal(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  log_message(FATAL_LEVEL, message, scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= FATAL
end

#info(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



124
125
126
# File 'lib/openc3/utilities/logger.rb', line 124

def info(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  log_message(INFO_LEVEL, message, scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= INFO
end

#levelInteger

Returns The logging level.

Returns:

  • (Integer)

    The logging level



35
# File 'lib/openc3/utilities/logger.rb', line 35

instance_attr_accessor :level

#scopeObject



186
187
188
# File 'lib/openc3/utilities/logger.rb', line 186

def scope
  return @@scope
end

#scope=(scope) ⇒ Object



190
191
192
# File 'lib/openc3/utilities/logger.rb', line 190

def scope=(scope)
  @@scope = scope
end

#stdoutBoolean

Returns Whether to output the message to stdout.

Returns:

  • (Boolean)

    Whether to output the message to stdout



32
# File 'lib/openc3/utilities/logger.rb', line 32

instance_attr_accessor :stdout

#warn(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object

Parameters:

  • message (String) (defaults to: nil)

    The message to print if the log level is at or below the method name log level.

  • block (Proc)

    Block to call which should return a string to append to the log message



129
130
131
# File 'lib/openc3/utilities/logger.rb', line 129

def warn(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block)
  log_message(WARN_LEVEL, message, scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= WARN
end