Class: OpenC3::Logger
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_LEVEL =
'DEBUG'
- INFO_LEVEL =
'INFO'
- WARN_LEVEL =
'WARN'
- ERROR_LEVEL =
'ERROR'
- FATAL_LEVEL =
'FATAL'
- LOG =
Types
'log'
- NOTIFICATION =
'notification'
- ALERT =
'alert'
- EPHEMERAL =
'ephemeral'
- @@mutex =
Mutex.new
- @@instance =
nil
- @@scope =
ENV['OPENC3_SCOPE']
Instance Attribute Summary collapse
-
#microservice_name ⇒ String
Microservice name.
Class Method Summary collapse
- .build_log_data(log_level, message, user: nil, type: nil, url: nil, other: nil) ⇒ Object
- .debug(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
- .error(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
- .fatal(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
- .info(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
-
.instance ⇒ Logger
The logger instance.
- .microservice_name ⇒ Object
- .microservice_name=(name) ⇒ Object
- .scope ⇒ Object
- .scope=(scope) ⇒ Object
- .warn(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
Instance Method Summary collapse
- #build_log_data(log_level, message, user: nil, type: nil, url: nil, other: nil, &block) ⇒ Object
- #debug(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
-
#detail_string ⇒ String
Additional detail to add to messages.
- #error(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
- #fatal(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
- #info(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
-
#initialize(level = Logger::INFO) ⇒ Logger
constructor
A new instance of Logger.
-
#level ⇒ Integer
The logging level.
- #scope ⇒ Object
- #scope=(scope) ⇒ Object
-
#stdout ⇒ Boolean
Whether to output the message to stdout.
- #warn(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
Constructor Details
#initialize(level = Logger::INFO) ⇒ Logger
Returns a new instance of Logger.
75 76 77 78 79 80 81 82 |
# File 'lib/openc3/utilities/logger.rb', line 75 def initialize(level = Logger::INFO) @stdout = true @level = level @detail_string = nil @container_name = Socket.gethostname @microservice_name = nil @no_store = ENV['OPENC3_NO_STORE'] end |
Instance Attribute Details
#microservice_name ⇒ String
Returns Microservice name.
45 46 47 |
# File 'lib/openc3/utilities/logger.rb', line 45 def microservice_name @microservice_name end |
Class Method Details
.build_log_data(log_level, message, user: nil, type: nil, url: nil, other: nil) ⇒ Object
194 195 196 |
# File 'lib/openc3/utilities/logger.rb', line 194 def self.build_log_data(log_level, , user: nil, type: nil, url: nil, other: nil) self.instance.build_log_data(log_level, , user: user, type: type, url: url, other: other) end |
.debug(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
126 127 128 |
# File 'lib/openc3/utilities/logger.rb', line 126 def self.debug( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) self.instance.debug(, scope: scope, user: user, type: type, url: url, other: other, &block) end |
.error(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
141 142 143 |
# File 'lib/openc3/utilities/logger.rb', line 141 def self.error( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) self.instance.error(, 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
146 147 148 |
# File 'lib/openc3/utilities/logger.rb', line 146 def self.fatal( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) self.instance.fatal(, 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
131 132 133 |
# File 'lib/openc3/utilities/logger.rb', line 131 def self.info( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) self.instance.info(, scope: scope, user: user, type: type, url: url, other: other, &block) end |
.instance ⇒ Logger
Returns The logger instance.
151 152 153 154 155 156 157 158 |
# File 'lib/openc3/utilities/logger.rb', line 151 def self.instance return @@instance if @@instance @@mutex.synchronize do @@instance ||= self.new end @@instance end |
.microservice_name ⇒ Object
89 90 91 |
# File 'lib/openc3/utilities/logger.rb', line 89 def self.microservice_name self.instance.microservice_name end |
.microservice_name=(name) ⇒ Object
93 94 95 |
# File 'lib/openc3/utilities/logger.rb', line 93 def self.microservice_name=(name) self.instance.microservice_name = name end |
.scope ⇒ Object
160 161 162 |
# File 'lib/openc3/utilities/logger.rb', line 160 def self.scope return @@scope end |
.scope=(scope) ⇒ Object
164 165 166 |
# File 'lib/openc3/utilities/logger.rb', line 164 def self.scope=(scope) @@scope = scope end |
.warn(message = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) ⇒ Object
136 137 138 |
# File 'lib/openc3/utilities/logger.rb', line 136 def self.warn( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) self.instance.warn(, 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
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/openc3/utilities/logger.rb', line 176 def build_log_data(log_level, , 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 # EE: If a user is passed, put its name. Don't include user data if no user was passed. if block_given? = yield end data[:container_name] = @container_name data[:message] = if 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
101 102 103 |
# File 'lib/openc3/utilities/logger.rb', line 101 def debug( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) (DEBUG_LEVEL, , scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= DEBUG end |
#detail_string ⇒ String
Returns 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, type: LOG, url: nil, other: nil, &block) ⇒ Object
116 117 118 |
# File 'lib/openc3/utilities/logger.rb', line 116 def error( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) (ERROR_LEVEL, , 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
121 122 123 |
# File 'lib/openc3/utilities/logger.rb', line 121 def fatal( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) (FATAL_LEVEL, , 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
106 107 108 |
# File 'lib/openc3/utilities/logger.rb', line 106 def info( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) (INFO_LEVEL, , scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= INFO end |
#level ⇒ Integer
Returns The logging level.
39 |
# File 'lib/openc3/utilities/logger.rb', line 39 instance_attr_accessor :level |
#scope ⇒ Object
168 169 170 |
# File 'lib/openc3/utilities/logger.rb', line 168 def scope return @@scope end |
#scope=(scope) ⇒ Object
172 173 174 |
# File 'lib/openc3/utilities/logger.rb', line 172 def scope=(scope) @@scope = scope end |
#stdout ⇒ Boolean
Returns 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, type: LOG, url: nil, other: nil, &block) ⇒ Object
111 112 113 |
# File 'lib/openc3/utilities/logger.rb', line 111 def warn( = nil, scope: @@scope, user: nil, type: LOG, url: nil, other: nil, &block) (WARN_LEVEL, , scope: scope, user: user, type: type, url: url, other: other, &block) if @level <= WARN end |