Class: StateFu::Logging
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
false
- @@log_level =
nil
Class Method Summary collapse
- .add(severity, message = nil, progname = nil, &block) ⇒ Object
- .debug(progname = nil, &block) ⇒ Object
- .error(progname = nil, &block) ⇒ Object
- .fatal(progname = nil, &block) ⇒ Object
- .info(progname = nil, &block) ⇒ Object
- .initial_log_level ⇒ Object
- .instance ⇒ Object
- .level ⇒ Object
- .level=(new_level) ⇒ Object
- .logger=(new_logger) ⇒ Object
- .new(logger = nil, options = {}) ⇒ Object
- .prefix ⇒ Object
-
.set_logger(logger, options = { :shared => false }) ⇒ Object
setter for logger instance.
- .shared? ⇒ Boolean
- .suppress! ⇒ Object
- .suppressed?(severity = DEBUG) ⇒ Boolean
- .unknown(progname = nil, &block) ⇒ Object
- .warn(progname = nil, &block) ⇒ Object
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, = nil, progname = nil, &block) severity = parse_log_level( severity ) return if suppressed?( severity ) = [prefix, ( || (block && block.call) || progname).to_s].compact.join # message = "#{message}\n" unless message[-1] == ?\n instance.add( severity, ) 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_level ⇒ Object
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 |
.instance ⇒ Object
68 69 70 |
# File 'lib/support/logging.rb', line 68 def self.instance @@logger ||= default_logger end |
.level ⇒ Object
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, ={} ) @@suppress = false set_logger(logger, ) self end |
.prefix ⇒ Object
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, = { :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 = !!.symbolize_keys![:shared] if shared? @@prefix = [:prefix] || DEFAULT_SHARED_LOG_PREFIX puts "shared :: #{@@prefix} #{prefix}" end if lvl = [:level] || [:log_level] self.level = lvl end instance end |
.shared? ⇒ 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
76 77 78 |
# File 'lib/support/logging.rb', line 76 def self.suppressed?(severity = DEBUG) suppress == true || severity < level end |