| Class | Dsadmin::Logger |
| In: |
lib/dsadmin/logger.rb
|
| Parent: | Object |
Logging class
# (Example) set up: @log = Dsadmin::Logger.instance # Logger is a singleton @log.run # (Example) shutdown: @log.shutdown
Using run and shutdown seems cumbersome, but it (a) ensures proper cleanup and (b) allows for easier testing.
The behavior of this class is controlled via the config file, section "log" (unless run(true) is used). Please see the comments in the config file for details.
Usage Example:
# ... (once!) setup see above
@log = Dsadmin::Logger.instance
@log.debug("Accessing Logger -- Yeehaw")
@log.warning("Current Request smells funny", request)
begin
neverThrowsAnything_Really(42)
rescue StandardError => xcept
@log.bug(xcept)
end
# ... (much later, somewhere) shutdown see above
| See also: | class Dsadmin::SingletonConfigManager |
| See also: | class Dsadmin::LoggerFilter |
| LOG_ERROR | = | 1 |
| LOG_WARNING | = | 2 |
| LOG_NOTICE | = | 4 |
| LOG_DEBUG | = | 8 |
| LOG_BUG | = | 16 |
| LOG_ALL | = | LOG_ERROR | LOG_WARNING | LOG_NOTICE | LOG_DEBUG | LOG_BUG |
| bug_count | [R] | |
| error_count | [R] | |
| warning_count | [R] |
# File lib/dsadmin/logger.rb, line 244
244: def initialize(aConfigMgr = SingletonConfigManager.instance)
245: @cfg = aConfigMgr
246: @sys = System.instance
247: @targets = Array.new
248: @accesslog = AccessLogTarget.new(LogWriters::StreamWriter.new($stdout))
249: @running = false
250:
251: @bug_count = 0
252: @error_count = 0
253: @warning_count = 0
254: @count_mutex = Mutex.new
255: end
Send an access record to all targets.
# File lib/dsadmin/logger.rb, line 149
149: def access(request, response, start_time)
150: begin
151: @accesslog.log(request, response, start_time, System.instance.now - start_time)
152: rescue StandardError => xcept
153: # We discovered a bug, but as this means the logging
154: # module is broken, we can only push it to the console
155: # the old-fashioned way
156: $stderr.puts("BUG: Logger caught #{xcept.class.name}\n")
157: $stderr.puts(" Message: #{xcept.message}\n")
158: $stderr.puts(" Backtrace:\n")
159: $stderr.puts(xcept.backtrace.join("\n "))
160: end
161: end
Add a logging target.
# File lib/dsadmin/logger.rb, line 178
178: def addTarget(kind, format, level, dest = nil)
179: fmt = format.downcase
180: kind = kind.downcase
181:
182: case fmt
183: when "xml" then formatter = LogFormatters::XMLFormatter.new
184: when "line" then formatter = LogFormatters::LineFormatter.new
185: when "plain" then formatter = LogFormatters::PlainFormatter.new
186: else raise(IOError, "Unknown log format '#{fmt}'")
187: end
188:
189: writer = getWriterFor(kind, dest)
190: target = MessageLogTarget.new(level, formatter, writer)
191: @targets << target
192: end
Send a debugging message to all targets.
aMessage may be a String, an Exception or anything that responds to to_s.
# File lib/dsadmin/logger.rb, line 134
134: def debug(aMessage)
135: log(aMessage, LOG_DEBUG)
136: end
Get a stream used for logging with the specified level & format.
This allows to redirect output from a different logger into e.g. files opened by this one.
# File lib/dsadmin/logger.rb, line 215
215: def getStream(aLevel, aFormat)
216: @targets.each { |target|
217: next if((target.log_level & aLevel) != aLevel)
218: next if(target.format != aFormat)
219:
220: return target.stream unless(target.stream.nil?)
221: }
222:
223: nil
224: end
Get information about the active logger targets (Array of Hashes)
# File lib/dsadmin/logger.rb, line 201
201: def getTargetInfo
202: info = Array.new
203: @targets.each { |target|
204: info << target.info
205: }
206:
207: return info
208: end
Send an informational message to all targets.
aMessage may be a String, an Exception or anything that responds to to_s.
# File lib/dsadmin/logger.rb, line 125
125: def notice(aMessage)
126: log(aMessage, LOG_NOTICE)
127: end
Run the logging system.
Only one logger may be run at a time!!
# File lib/dsadmin/logger.rb, line 87
87: def run(anIgnoreConfig = false)
88: requireTrue(! @running)
89:
90: @running = true
91:
92: begin
93: initTargets() unless anIgnoreConfig
94: rescue StandardError => xcept
95: pp(xcept, xcept.backtrace)
96: end
97: end
# File lib/dsadmin/logger.rb, line 195
195: def setAccessTarget(kind, dest)
196: @accesslog = AccessLogTarget.new(getWriterFor(kind, dest))
197: end
Shut the logger down.
# File lib/dsadmin/logger.rb, line 164
164: def shutdown
165: @targets.each { |target|
166: target.shutdown
167: }
168: @targets = Array.new
169:
170: @accesslog.shutdown if(@accesslog)
171:
172: @running = false;
173: end
# File lib/dsadmin/logger.rb, line 283
283: def getWriterFor(kind, dest)
284: case kind
285: when "file"
286: writer = LogWriters::FileWriter.new(dest, @cfg)
287: when "email"
288: writer = LogWriters::EmailWriter.new(dest)
289: when "stdout"
290: writer = LogWriters::StreamWriter.new($stdout)
291: when "stderr"
292: writer = LogWriters::StreamWriter.new($stderr)
293: when "syslog"
294: $stderr.puts "Logging to syslog is not supported yet - ignoring"
295: else
296: raise(IOError, "Unknown log type '#{kind}'")
297: end
298:
299: writer
300: end
Initialize all targets from the specs found in the config file
# File lib/dsadmin/logger.rb, line 259
259: def initTargets()
260: csect = @cfg.log_config
261:
262: csect.target.to_a.each do |entry|
263: type = entry.kind.strip
264: dest = entry.dest ? entry.dest.strip : ''
265: fmt = entry.format.strip
266: lvl = 0
267: lvl |= LOG_ERROR if entry.errors
268: lvl |= LOG_WARNING if entry.warnings
269: lvl |= LOG_NOTICE if entry.notices
270: lvl |= LOG_DEBUG if entry.debug
271: lvl |= LOG_BUG if entry.bug
272:
273: addTarget(type, fmt, lvl, dest)
274: end
275:
276: unless(csect.access.nil?)
277: adest = csect.access.dest && csect.access.dest.strip
278: setAccessTarget(csect.access.kind.strip, adest)
279: end
280: end
Send a log record to all targets
# File lib/dsadmin/logger.rb, line 304
304: def log(aMessage, aLevel)
305: requireTrue(@running)
306:
307: request = Thread.current[:request]
308:
309: begin
310: @targets.each { |current|
311: current.log(aMessage, aLevel, request)
312: }
313: rescue StandardError => xcept
314: # We discovered a bug, but as this means the logging
315: # module is broken, we can only push it to the console
316: # the old-fashioned way
317: $stderr.puts("BUG: Logger caught #{xcept.class.name}\n")
318: $stderr.puts(" Message: #{xcept.message}\n")
319: $stderr.puts(" Backtrace:\n")
320: $stderr.puts(xcept.backtrace.join("\n "))
321: end
322: end