lib/sinatra/log.rb in sinatra-log-0.0.1 vs lib/sinatra/log.rb in sinatra-log-0.1.1

- old
+ new

@@ -1,42 +1,57 @@ require 'rubygems' require 'log4r' require 'time' module Sinatra - # Logging. Logs to log/<project_name>.log with the format: + # Logs to specified filename with the format: # # [Log Level]: [Timestamp (ISO-8601)]: [File:linenum]: [Log Message] # - # Use through the helper: - # log.warn 'This is my log message' + # Access is usually wrapped in a class helper: # + # class MyProject + # self.log + # @logger ||= Sinatra::Log.new(:logger_name => 'myproject', + # :log_filename => 'myproject/dev.log', + # :loglevel => 'WARN', + # :enabled => true, + # :project_dir => '/var/opt/myproj') + # end + # end class Log include Log4r + REQUIRED_CONFIG_SYMBOLS = [:logger_name, :loglevel, :log_filename, + :enabled].freeze + # Formatter that include the filename and relative path, and line number in # output of the caller. # # Since all callers go through the methods defined in this class to log, we # look at the second line of the tracer output, removing everything but the # directories after the project directory. - # class DefaultFormatter < Log4r::Formatter - # Return the project base directory for filtering to help with - # identifiying the filename and line number when formatting the log - # message - # - # @return [String] Base directory for the project - def basedir - @basedir ||= File.expand_path(File.join(File.dirname(__FILE__), "..")) + attr_reader :basedir + + # @param [String] basedir The base project directory; this directory + # will be filtered out from each log entry if specified. + def initialize(basedir = nil) + super + @basedir = basedir end # Return a trimmed version of the filename from where a LogEvent occurred + # # @param [String] tracer A line from the LogEvent#tracer Array # @return [String] Trimmed and parsed version of the file ane line number def event_filename(tracer) - parts = tracer.match(/#{basedir}\/(.*:[0-9]+).*:/) + if basedir.nil? + parts = tracer.match(/(.*:[0-9]+).*:/) + else + parts = tracer.match(/#{basedir}\/(.*:[0-9]+).*:/) + end # If we get no matches back, we're probably in a jar file in which case # the format of the tracer is going to be abbreviated if parts.nil? parts = tracer.match(/(.*:[0-9]+).*:/) @@ -58,16 +73,16 @@ attr_reader :outputter def initialize(config={}) errors = [] - [:logger_name, :loglevel, :log_filename, :enabled].each do |key| + REQUIRED_CONFIG_SYMBOLS.each do |key| if !config.include?(key) errors << "#{key} required, but not specified in config hash" end - raise ArgumentError, "#{errors}" if errors.count > 0 end + raise ArgumentError, "#{errors}" if errors.count > 0 logger_name = config[:logger_name].to_s.gsub(/\s+/, '_') @logger = Log4r::Logger.new(logger_name) if config[:enabled] @@ -80,10 +95,10 @@ @outputter = FileOutputter.new("#{logger_name}fileoutput", :filename => config[:log_filename], :trunc => false) @logger.trace = true - @outputter.formatter = DefaultFormatter + @outputter.formatter = DefaultFormatter.new(config[:project_dir]) @logger.outputters = @outputter end [:debug, :info, :warn, :error, :fatal, :level].each do |method|