Class: DaemonKit::AbstractLogger
- Defined in:
- lib/daemon_kit/abstract_logger.rb
Overview
One of the key parts of succesful background processes is adequate logging. The AbstractLogger aims to simplify logging from inside daemon processes by providing additional useful information with each log line, including calling file name and line number and support for cleanly logging exceptions.
The logger can be accessed through #DaemonKit.logger.
AbstractLogger provides an interface that is fully compatible with the Logger class provided by Ruby’s Standard Library, and is extended with some additional conveniences.
The AbstractLogger supports different backends, by default it uses a Logger instance, but can by swapped out for a SysLogLogger logger as well.
Defined Under Namespace
Classes: Formatter
Class Attribute Summary collapse
-
.severities ⇒ Object
readonly
Returns the value of attribute severities.
-
.silencer ⇒ Object
Returns the value of attribute silencer.
Instance Attribute Summary collapse
-
#copy_to_stdout ⇒ Object
Returns the value of attribute copy_to_stdout.
Instance Method Summary collapse
-
#<<(msg) ⇒ Object
Write unformatted message to logging device, mostly useful for Logger interface compatibility and debugging soap4r (possibly others).
- #add(severity, message = nil, skip_caller = false, &block) ⇒ Object
- #clean_trace(trace) ⇒ Object
- #close ⇒ Object
- #debug(msg = nil, &block) ⇒ Object
- #debug? ⇒ Boolean
- #error(msg = nil, &block) ⇒ Object
- #error? ⇒ Boolean
-
#exception(e) ⇒ Object
Conveniently log an exception and the backtrace.
- #fatal(msg = nil, &block) ⇒ Object
- #fatal? ⇒ Boolean
- #info(msg = nil, &block) ⇒ Object
- #info? ⇒ Boolean
-
#initialize(log_path = nil) ⇒ AbstractLogger
constructor
Optional log path, defaults to DAEMON_ROOT/log/DAEMON_ENV.log.
- #level ⇒ Object
- #level=(level) ⇒ Object
- #logger ⇒ Object
- #logger=(logger) ⇒ Object
-
#silence(temporary_level = :error) ⇒ Object
Silence the logger for the duration of the block.
- #unknown(msg = nil, &block) ⇒ Object
- #unknown? ⇒ Boolean
- #warn(msg = nil, &block) ⇒ Object
- #warn? ⇒ Boolean
Constructor Details
#initialize(log_path = nil) ⇒ AbstractLogger
Optional log path, defaults to DAEMON_ROOT/log/DAEMON_ENV.log
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/daemon_kit/abstract_logger.rb', line 41 def initialize( log_path = nil ) if log_path.to_s == "syslog" @backend = :syslog else @logger_file = log_path || "#{DAEMON_ROOT}/log/#{DAEMON_ENV}.log" @backend = :logger end @copy_to_stdout = false end |
Class Attribute Details
.severities ⇒ Object (readonly)
Returns the value of attribute severities.
35 36 37 |
# File 'lib/daemon_kit/abstract_logger.rb', line 35 def severities @severities end |
.silencer ⇒ Object
Returns the value of attribute silencer.
36 37 38 |
# File 'lib/daemon_kit/abstract_logger.rb', line 36 def silencer @silencer end |
Instance Attribute Details
#copy_to_stdout ⇒ Object
Returns the value of attribute copy_to_stdout.
21 22 23 |
# File 'lib/daemon_kit/abstract_logger.rb', line 21 def copy_to_stdout @copy_to_stdout end |
Instance Method Details
#<<(msg) ⇒ Object
Write unformatted message to logging device, mostly useful for Logger interface compatibility and debugging soap4r (possibly others)
68 69 70 |
# File 'lib/daemon_kit/abstract_logger.rb', line 68 def <<( msg ) #:nodoc: self.logger.write( msg ) if self.logger && self.logger.respond_to?( :write ) end |
#add(severity, message = nil, skip_caller = false, &block) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/daemon_kit/abstract_logger.rb', line 126 def add( severity, = nil, skip_caller = false, &block ) = yield if block_given? = "#{called(caller)}: #{}" unless skip_caller self.logger.add( self.class.severities[ severity ] ) { } STDOUT.puts( ) if self.copy_to_stdout end |
#clean_trace(trace) ⇒ Object
160 161 162 163 164 165 |
# File 'lib/daemon_kit/abstract_logger.rb', line 160 def clean_trace( trace ) trace = trace.map { |l| l.gsub(DAEMON_ROOT, '') } trace = trace.reject { |l| l =~ /gems\/daemon[\-_]kit/ } trace = trace.reject { |l| l =~ /vendor\/daemon[\-_]kit/ } trace end |
#close ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/daemon_kit/abstract_logger.rb', line 167 def close case @backend when :logger self.logger.close @logger = nil end end |
#debug(msg = nil, &block) ⇒ Object
72 73 74 |
# File 'lib/daemon_kit/abstract_logger.rb', line 72 def debug( msg = nil, &block ) add( :debug, msg, &block ) end |
#debug? ⇒ Boolean
76 77 78 |
# File 'lib/daemon_kit/abstract_logger.rb', line 76 def debug? self.level == :debug end |
#error(msg = nil, &block) ⇒ Object
96 97 98 |
# File 'lib/daemon_kit/abstract_logger.rb', line 96 def error( msg = nil, &block ) add( :error, msg, &block ) end |
#error? ⇒ Boolean
100 101 102 |
# File 'lib/daemon_kit/abstract_logger.rb', line 100 def error? self.level == :error end |
#exception(e) ⇒ Object
Conveniently log an exception and the backtrace
121 122 123 124 |
# File 'lib/daemon_kit/abstract_logger.rb', line 121 def exception( e ) = "EXCEPTION: #{e.}: #{clean_trace( e.backtrace )}" self.add( :error, , true ) end |
#fatal(msg = nil, &block) ⇒ Object
104 105 106 |
# File 'lib/daemon_kit/abstract_logger.rb', line 104 def fatal( msg = nil, &block ) add( :fatal, msg, &block ) end |
#fatal? ⇒ Boolean
108 109 110 |
# File 'lib/daemon_kit/abstract_logger.rb', line 108 def fatal? self.level == :fatal end |
#info(msg = nil, &block) ⇒ Object
80 81 82 |
# File 'lib/daemon_kit/abstract_logger.rb', line 80 def info( msg = nil, &block ) add( :info, msg, &block ) end |
#info? ⇒ Boolean
84 85 86 |
# File 'lib/daemon_kit/abstract_logger.rb', line 84 def info? self.level == :info end |
#level ⇒ Object
136 137 138 |
# File 'lib/daemon_kit/abstract_logger.rb', line 136 def level self.class.severities.invert[ @logger.level ] end |
#level=(level) ⇒ Object
140 141 142 143 |
# File 'lib/daemon_kit/abstract_logger.rb', line 140 def level=( level ) level = ( Symbol === level ? self.class.severities[ level ] : level ) self.logger.level = level end |
#logger ⇒ Object
145 146 147 |
# File 'lib/daemon_kit/abstract_logger.rb', line 145 def logger @logger ||= create_logger end |
#logger=(logger) ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/daemon_kit/abstract_logger.rb', line 149 def logger=( logger ) if logger.is_a?( Symbol ) @backend = logger @logger.close rescue nil @logger = create_logger else @logger.close rescue nil @logger = logger end end |
#silence(temporary_level = :error) ⇒ Object
Silence the logger for the duration of the block.
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/daemon_kit/abstract_logger.rb', line 53 def silence( temporary_level = :error ) if self.class.silencer begin old_level, self.level = self.level, temporary_level yield self ensure self.level = old_level end else yield self end end |
#unknown(msg = nil, &block) ⇒ Object
112 113 114 |
# File 'lib/daemon_kit/abstract_logger.rb', line 112 def unknown( msg = nil, &block ) add( :unknown, msg, &block ) end |
#unknown? ⇒ Boolean
116 117 118 |
# File 'lib/daemon_kit/abstract_logger.rb', line 116 def unknown? self.level == :unknown end |
#warn(msg = nil, &block) ⇒ Object
88 89 90 |
# File 'lib/daemon_kit/abstract_logger.rb', line 88 def warn( msg = nil, &block ) add( :warn, msg, &block ) end |
#warn? ⇒ Boolean
92 93 94 |
# File 'lib/daemon_kit/abstract_logger.rb', line 92 def warn? self.level == :warn end |