Class: StateFu::Logging

Inherits:
Object show all
Defined in:
lib/support/logging.rb

Overview

TODO - spec coverage

Provide logging facilities, including the ability to use a shared logger.

Use Rails’ log if running as a rails plugin; allow independent control of StateFu log level.

Constant Summary collapse

DEBUG =
0
INFO =
1
WARN =
2
ERROR =
3
FATAL =
4
UNKNOWN =
5
ENV_LOG_LEVEL =
'STATEFU_LOGLEVEL'
DEFAULT_LEVEL =
INFO
DEFAULT_SHARED_LOG_PREFIX =
'[StateFu] '
@@prefix =
DEFAULT_SHARED_LOG_PREFIX
@@logger =
nil
@@suppress =
false
@@shared =
false
@@log_level =
nil

Class Method Summary collapse

Class Method Details

.add(severity, message = nil, progname = nil, &block) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/support/logging.rb', line 80

def self.add(severity, message = nil, progname = nil, &block)
  severity = parse_log_level( severity )
  return if suppressed?( severity )
  message = [prefix, (message || (block && block.call) || progname).to_s].compact.join
  # message = "#{message}\n" unless message[-1] == ?\n
  instance.add( severity, message )
end

.debug(progname = nil, &block) ⇒ Object



88
# File 'lib/support/logging.rb', line 88

def self.debug   progname = nil, █ add DEBUG,   progname, █ end

.error(progname = nil, &block) ⇒ Object



91
# File 'lib/support/logging.rb', line 91

def self.error   progname = nil, █ add ERROR,   progname, █ end

.fatal(progname = nil, &block) ⇒ Object



92
# File 'lib/support/logging.rb', line 92

def self.fatal   progname = nil, █ add FATAL,   progname, █ end

.info(progname = nil, &block) ⇒ Object



89
# File 'lib/support/logging.rb', line 89

def self.info    progname = nil, █ add INFO,    progname, █ end

.initial_log_levelObject



40
41
42
43
44
45
46
# File 'lib/support/logging.rb', line 40

def self.initial_log_level
  if env_level = ENV[ENV_LOG_LEVEL]
    parse_log_level( env_level )
  else
    DEFAULT_LEVEL
  end
end

.instanceObject



68
69
70
# File 'lib/support/logging.rb', line 68

def self.instance
  @@logger ||= default_logger
end

.levelObject



48
49
50
# File 'lib/support/logging.rb', line 48

def self.level
  @@log_level ||= initial_log_level
end

.level=(new_level) ⇒ Object



52
53
54
# File 'lib/support/logging.rb', line 52

def self.level=( new_level )
  @@log_level = parse_log_level(new_level)
end

.logger=(new_logger) ⇒ Object



64
65
66
# File 'lib/support/logging.rb', line 64

def self.logger= new_logger
  set_logger new_logger
end

.new(logger = nil, options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/support/logging.rb', line 34

def self.new( logger=nil, options={} )
  @@suppress = false
  set_logger(logger, options)
  self
end

.prefixObject



60
61
62
# File 'lib/support/logging.rb', line 60

def self.prefix
  shared? ? @@prefix : nil
end

.set_logger(logger, options = { :shared => false }) ⇒ Object

setter for logger instance



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/support/logging.rb', line 100

def self.set_logger( logger, options = { :shared => false } )
  case logger
  when String
    file     = File.open(logger, File::WRONLY | File::APPEND)        
    @@logger = activesupport_logger_available? ? ActiveSupport::BufferedLogger.new(file) : Logger.new(file)
  when Logger
    @@logger = logger
  when activesupport_logger_available? && ActiveSupport::BufferedLogger
    @@logger = logger
  else
    default_logger
  end
  self.shared = !!options.symbolize_keys![:shared]
  if shared?
    @@prefix = options[:prefix] || DEFAULT_SHARED_LOG_PREFIX
    puts "shared :: #{@@prefix} #{prefix}"
  end      
  if lvl = options[:level] || options[:log_level]
    self.level = lvl
  end      
  instance
end

.shared?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/support/logging.rb', line 56

def self.shared?
  !!@@shared
end

.suppress!Object



72
73
74
# File 'lib/support/logging.rb', line 72

def self.suppress!
  self.suppress = true
end

.suppressed?(severity = DEBUG) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/support/logging.rb', line 76

def self.suppressed?(severity = DEBUG)
  suppress == true || severity < level
end

.unknown(progname = nil, &block) ⇒ Object



93
# File 'lib/support/logging.rb', line 93

def self.unknown progname = nil, &block; add UNKNOWN, progname, &block; end

.warn(progname = nil, &block) ⇒ Object



90
# File 'lib/support/logging.rb', line 90

def self.warn    progname = nil, &block; add WARN,    progname, &block; end