Class: Itchy::Log
Overview
Handles static logging from the entire application without the need for a traveling logger instance.
Constant Summary collapse
- SUBSCRIPTION_HANDLE =
'itchy.log'
Instance Attribute Summary collapse
-
#log_prefix ⇒ Object
readonly
Returns the value of attribute log_prefix.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
- .debug(message) ⇒ Object
- .error(message) ⇒ Object
- .fatal(message) ⇒ Object
-
.info(message) ⇒ Object
Log an
INFO
message. - .warn(message) ⇒ Object
Instance Method Summary collapse
-
#close ⇒ Object
Closes active logger subscription.
-
#initialize(log_dev, log_prefix = '[itchy]') ⇒ Log
constructor
Creates a new itchy logger instance.
-
#level ⇒ ::Logger::Severity
Gets current logging level.
-
#level=(severity) ⇒ Object
Sets current logging level.
Constructor Details
#initialize(log_dev, log_prefix = '[itchy]') ⇒ Log
Creates a new itchy logger instance. Subsequent logging can be then performed via static Itchy::Log methods.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/itchy/log.rb', line 17 def initialize(log_dev, log_prefix = '[itchy]') if log_dev.is_a? ::Logger @logger = log_dev else @logger = ::Logger.new(log_dev) end @log_prefix = log_prefix.blank? ? '' : log_prefix.strip # subscribe to log messages and send to logger @log_subscriber = ActiveSupport::Notifications.subscribe(self.class::SUBSCRIPTION_HANDLE) do |_name, _start, _finish, _id, payload| @logger.log(payload[:level], "#{@log_prefix} #{payload[:message]}") if @logger end end |
Instance Attribute Details
#log_prefix ⇒ Object (readonly)
Returns the value of attribute log_prefix.
7 8 9 |
# File 'lib/itchy/log.rb', line 7 def log_prefix @log_prefix end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
7 8 9 |
# File 'lib/itchy/log.rb', line 7 def logger @logger end |
Class Method Details
.debug(message) ⇒ Object
52 53 54 |
# File 'lib/itchy/log.rb', line 52 def self.debug() ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, level: ::Logger::DEBUG, message: ) end |
.error(message) ⇒ Object
69 70 71 |
# File 'lib/itchy/log.rb', line 69 def self.error() ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, level: ::Logger::ERROR, message: ) end |
.fatal(message) ⇒ Object
74 75 76 |
# File 'lib/itchy/log.rb', line 74 def self.fatal() ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, level: ::Logger::FATAL, message: ) end |
.info(message) ⇒ Object
Log an INFO
message.
59 60 61 |
# File 'lib/itchy/log.rb', line 59 def self.info() ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, level: ::Logger::INFO, message: ) end |
.warn(message) ⇒ Object
64 65 66 |
# File 'lib/itchy/log.rb', line 64 def self.warn() ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, level: ::Logger::WARN, message: ) end |
Instance Method Details
#close ⇒ Object
Closes active logger subscription.
33 34 35 |
# File 'lib/itchy/log.rb', line 33 def close ActiveSupport::Notifications.unsubscribe(@log_subscriber) end |
#level ⇒ ::Logger::Severity
Gets current logging level.
47 48 49 |
# File 'lib/itchy/log.rb', line 47 def level @logger.level end |
#level=(severity) ⇒ Object
Sets current logging level.
40 41 42 |
# File 'lib/itchy/log.rb', line 40 def level=(severity) @logger.level = severity end |