Skip to content

Instantly share code, notes, and snippets.

@ciaranarcher
Created November 3, 2011 09:05
Show Gist options
  • Save ciaranarcher/1336094 to your computer and use it in GitHub Desktop.
Save ciaranarcher/1336094 to your computer and use it in GitHub Desktop.
Logging Helper Example
# Helper class for logging.
# This class will allow the user get a standard Ruby Logger instance.
# Any specific log file configuration (e.g. rollover size etc)
# should be done within this class.
# @todo - should we run all log() calls through this class?
#
# @author Ciaran Archer
class LoggerHelper
@log_dir = "./logs"
@rollover_period = "weekly"
# @author Ciaran Archer
# @param [String] file_name with extension e.g. `bets_trace` or `payments/my_processor` (note forward slash!)
# @return Logger
def self.get_logger(file_name)
# create any directories, if we have any passed in the log name
dirs = File.dirname(file_name)
if (dirs != ".")
self.check_create_dir(dirs)
end
# @todo we may wish to override the rollover_period value for certain log files
Logger.new("#{@log_dir}/#{file_name}.log", @rollover_period)
end
# Checks and creates a directory if it doesn't exist
# @author Ciaran Archer
# @param [String] (see #get_logger)
# @return nil
# @note It might not be necessary to do this step with other logging libs
def self.check_create_dir(dir)
unless (File.directory? @log_dir + "/" + dir)
FileUtils.mkpath(@log_dir + "/" + dir)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment