#!/usr/bin/env ruby #-- # Copyright (c) 2006 Jeff Barczewski # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including # without limitation the rights to use, copy, modify, merge, publish, # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ # # MasterView initialization utility to install a logger in MasterView::Log. # # The MasterView logger is used by the MasterView template engine # and directive implementations to record debug, warning, and error messages. # #-- # Need to do this in separate init file in order to do the # Log4r include for mixing into the MasterView module. # Can't just do +MasterView.include Log4r+ in Initializer#initialize_logger, # unfortunately. #++ # module MasterView #:nodoc: if ConfigSettings.logger == 'log4r' begin require 'log4r' include Log4r # mix in the Log4R Logger facilities Log = Logger.new 'MasterView' Log.outputters = Outputter.stdout LogLevels = {} Log4r::LNAMES.each_index { |lindex| LogLevels[Log4r::LNAMES[lindex]] = lindex } rescue LoadError, NameError # fallback to built-in logger end end if ! defined?(Log) require 'logger' Log = Logger.new STDOUT #:nodoc: LogLevels = {} #:nodoc: LogLevels['ANY'] = nil Logger::SEV_LABEL.each { |lname| LogLevels[lname] = Logger.const_get(lname) unless lname == 'ANY' } end if ConfigSettings.log_level if LogLevels.has_key?(ConfigSettings.log_level) Log.level = LogLevels[ConfigSettings.log_level] else Log.error "Undefined config.log_level='#{ConfigSettings.log_level}'" end end # Answer the current logging severity level of the MasterView::Log. # By default, answers the name of the logging level. # Request :index flavor to get the logger's internal index value def self.log_level( flavor=:name ) lindex = Log.level return lindex if flavor == :index lname = LogLevels.index(lindex) # nil if unknown name - ok or should we complain? lname = lname.to_sym if lname lname end # Set the logging severity level for the MasterView::Log def self.log_level= level set_log_level(level) end # Set the logging severity level for the MasterView::Log def self.set_log_level(level) #TBD: justify use case and test before enabling polymorphic parm cleverness if level.kind_of? Integer #TODO: ensure a valid level for this logger?? #Log.level = level Log.error "Not supported: log_level='#{level}' (use string or symbol form of level name)" return end #assert (level.kind_of? String) || (level.kind_of? Symbol) lname = level.to_s # stringify per internal rep if LogLevels.has_key?(lname) Log.level = LogLevels[lname] else Log.error "Undefined log level='#{level}' for #{Log.class.name} logger" end end end