Class: OnesnooperServer::Log

Inherits:
Object
  • Object
show all
Includes:
Logger::Severity
Defined in:
lib/onesnooper_server/log.rb

Overview

Wrapper for log subscribers combining the functionality of `Logger` and `ActiveSupport::Notifications`. Allows the use of Singleton-like logging facilities.

Constant Summary collapse

SUBSCRIPTION_HANDLE =

Default subscription handle for notifications

"onesnooper-server.log"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log_dev, log_prefix = '[onesnooper-server]') ⇒ Log

Creates a new logger


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/onesnooper_server/log.rb', line 19

def initialize(log_dev, log_prefix = '[onesnooper-server]')
  if log_dev.kind_of? ::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_prefixObject (readonly)

Returns the value of attribute log_prefix


10
11
12
# File 'lib/onesnooper_server/log.rb', line 10

def log_prefix
  @log_prefix
end

#loggerObject (readonly)

Returns the value of attribute logger


10
11
12
# File 'lib/onesnooper_server/log.rb', line 10

def logger
  @logger
end

Class Method Details

.debug(message) ⇒ Object

See Also:


49
50
51
# File 'lib/onesnooper_server/log.rb', line 49

def self.debug(message)
  ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, :level => ::Logger::DEBUG, :message => message)
end

.error(message) ⇒ Object

See Also:


65
66
67
# File 'lib/onesnooper_server/log.rb', line 65

def self.error(message)
  ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, :level => ::Logger::ERROR, :message => message)
end

.fatal(message) ⇒ Object

See Also:


70
71
72
# File 'lib/onesnooper_server/log.rb', line 70

def self.fatal(message)
  ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, :level => ::Logger::FATAL, :message => message)
end

.info(message) ⇒ Object

Log an INFO message


55
56
57
# File 'lib/onesnooper_server/log.rb', line 55

def self.info(message)
  ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, :level => ::Logger::INFO, :message => message)
end

.warn(message) ⇒ Object

See Also:


60
61
62
# File 'lib/onesnooper_server/log.rb', line 60

def self.warn(message)
  ActiveSupport::Notifications.instrument(self::SUBSCRIPTION_HANDLE, :level => ::Logger::WARN, :message => message)
end

Instance Method Details

#closeObject


34
35
36
# File 'lib/onesnooper_server/log.rb', line 34

def close
  ActiveSupport::Notifications.unsubscribe(@log_subscriber)
end

#level::Logger::Severity


44
45
46
# File 'lib/onesnooper_server/log.rb', line 44

def level
  @logger.level
end

#level=(severity) ⇒ Object


39
40
41
# File 'lib/onesnooper_server/log.rb', line 39

def level=(severity)
  @logger.level = severity
end