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 =

DEBUG only prints DEBUG messages

::Logger::DEBUG
INFO =

INFO prints INFO, DEBUG messages

::Logger::INFO
WARN =

WARN prints WARN, INFO, DEBUG messages

::Logger::WARN
ERROR =

ERROR prints ERROR, WARN, INFO, DEBUG messages

::Logger::ERROR
FATAL =

FATAL prints FATAL, ERROR, WARN, INFO, DEBUG messages

::Logger::FATAL
DEBUG_SEVERITY_STRING =
'DEBUG'
INFO_SEVERITY_STRING =
'INFO'
WARN_SEVERITY_STRING =
'WARN'
ERROR_SEVERITY_STRING =
'ERROR'
FATAL_SEVERITY_STRING =
'FATAL'
@@mutex =
Mutex.new
@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = Logger::INFO) ⇒ Logger

Returns a new instance of Logger.

Parameters:

  • level (Integer) (defaults to: Logger::INFO)

    The initial logging level



71
72
73
74
75
76
77
78
79
# File 'lib/openc3/utilities/logger.rb', line 71

def initialize(level = Logger::INFO)
  @stdout = true
  @level = level
  @scope = nil
  @detail_string = nil
  @container_name = Socket.gethostname
  @microservice_name = nil
  @no_store = ENV['OPENC3_NO_STORE']
end

Instance Attribute Details

#microservice_nameString

Returns Microservice name.

Returns:

  • (String)

    Microservice name



45
46
47
# File 'lib/openc3/utilities/logger.rb', line 45

def microservice_name
  @microservice_name
end

Class Method Details

.debug(message = nil, scope: nil, user: 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



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

def self.debug(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.debug(message, **args, &block)
end

.error(message = nil, scope: nil, user: 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



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

def self.error(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.error(message, **args, &block)
end

.fatal(message = nil, scope: nil, user: 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



155
156
157
158
159
160
# File 'lib/openc3/utilities/logger.rb', line 155

def self.fatal(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.fatal(message, **args, &block)
end

.info(message = nil, scope: nil, user: 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



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

def self.info(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.info(message, **args, &block)
end

.instanceLogger

Returns The logger instance.

Returns:

  • (Logger)

    The logger instance



163
164
165
166
167
168
169
170
# File 'lib/openc3/utilities/logger.rb', line 163

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

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

.microservice_nameObject



86
87
88
# File 'lib/openc3/utilities/logger.rb', line 86

def self.microservice_name
  self.instance.microservice_name
end

.microservice_name=(name) ⇒ Object



90
91
92
# File 'lib/openc3/utilities/logger.rb', line 90

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

.warn(message = nil, scope: nil, user: 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
142
143
144
# File 'lib/openc3/utilities/logger.rb', line 139

def self.warn(message = nil, scope: nil, user: nil, &block)
  args = {}
  args[:scope] = scope if scope
  args[:user] = user if user
  self.instance.warn(message, **args, &block)
end

Instance Method Details

#debug(message = nil, scope: @scope, user: 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



98
99
100
# File 'lib/openc3/utilities/logger.rb', line 98

def debug(message = nil, scope: @scope, user: nil, &block)
  log_message(DEBUG_SEVERITY_STRING, message, scope: scope, user: user, &block) if @level <= DEBUG
end

#detail_stringString

Returns Additional detail to add to messages.

Returns:

  • (String)

    Additional detail to add to messages



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

instance_attr_accessor :detail_string

#error(message = nil, scope: @scope, user: 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



113
114
115
# File 'lib/openc3/utilities/logger.rb', line 113

def error(message = nil, scope: @scope, user: nil, &block)
  log_message(ERROR_SEVERITY_STRING, message, scope: scope, user: user, &block) if @level <= ERROR
end

#fatal(message = nil, scope: @scope, user: 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



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

def fatal(message = nil, scope: @scope, user: nil, &block)
  log_message(FATAL_SEVERITY_STRING, message, scope: scope, user: user, &block) if @level <= FATAL
end

#info(message = nil, scope: @scope, user: 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



103
104
105
# File 'lib/openc3/utilities/logger.rb', line 103

def info(message = nil, scope: @scope, user: nil, &block)
  log_message(INFO_SEVERITY_STRING, message, scope: scope, user: user, &block) if @level <= INFO
end

#levelInteger

Returns The logging level.

Returns:

  • (Integer)

    The logging level



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

instance_attr_accessor :level

#scopeString

Returns Scope.

Returns:



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

instance_attr_accessor :scope

#stdoutBoolean

Returns Whether to output the message to stdout.

Returns:

  • (Boolean)

    Whether to output the message to stdout



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

instance_attr_accessor :stdout

#warn(message = nil, scope: @scope, user: 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



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

def warn(message = nil, scope: @scope, user: nil, &block)
  log_message(WARN_SEVERITY_STRING, message, scope: scope, user: user, &block) if @level <= WARN
end