Class | Ramaze::Logger::RotatingInformer |
In: |
lib/ramaze/log/rotatinginformer.rb
|
Parent: | Object |
A logger that rotates log files based on the current date. Log files are named after the date on which they were created. If the date changes a new log file is used.
In order to use this logger you‘ll have to specify a base directory for all log files. This directory will not be created for you so make sure it exists. Loading the class can be done as following:
logger = Ramaze::Logger::RotatingInformer.new('./log')
This creates a new instance that uses the directory ``./log`` for all it‘s log files.
The default log format is ``%Y-%m-%d.log``. If you want to change this you can specify an alternative format (including the extension) as the secondary parameter of the ``.new()`` method:
logger = Ramaze::Logger::RotatingInformer.new('./log', '%d-%m-%Y.log')
In this case the instance will use the date format ``dd-mm-yyyy`` along with the ``.log`` extension.
Besides the date format you can also customize the timestamp format as well as the format of each logged messages. Both these are set in a trait. The timestamp format is located in the trait ``:timestamp`` while the message format is stored in the ``:format`` trait. These can be set as following:
logger = Ramaze::Logger::RotatingInformer.new('./log') logger.trait[:timestamp] = '...' logger.trait[:format] = '...'
When setting the ``:format`` trait you can use 3 tags that will be replaced by their corresponding values. These are the following tags:
base_dir | [R] | |
log_levels | [RW] | |
time_format | [RW] |
Create a new instance of RotatingInformer.
base_dir is the directory where all log files will be stored
time_format is the time format used to name the log files. Possible formats are identical to those accepted by Time.strftime
log_levelse is an array describing what kind of messages that the log receives. The array may contain any or all of the symbols :debug, :error, :info and/or :warn
@example
# Creates logs in directory called logs. The generated filenames # will be in the form YYYY-MM-DD.log RotatingInformer.new('logs') # Creates logs in directory called logs. The generated filenames # will be in the form YYYY-MM.txt RotatingInformer.new('logs', '%Y-%m.txt') # Creates logs in directory called logs. The generated filenames # will be in the form YYYY-MM.txt. # Only errors will be logged to the files. RotatingInformer.new('logs', '%Y-%m.txt', [:error])
@param [String] base_dir The base directory for all the log files. @param [String] time_format The time format for all log files. @param [Array] log_levels Array containing the type of messages to log.
Set the base directory for log files
If this method is called with the raise_exception parameter set to true the method will raise an exception if the specified directory does not exist or is unwritable.
If raise_exception is set to false, the method will just silently fail if the specified directory does not exist or is unwritable.
@param [String] directory The base directory specified by the developer. @param [Bool] raise_exception Boolean that indicates if an exception
should be raised if the base directory doesn't exist.
Takes the prefix (tag), text and timestamp and applies it to the :format trait.
@param [String] prefix @param [String] text @param [Integer] time
This uses timestamp trait or a date in the format of ``%Y-%m-%d %H:%M:%S``
@return [String]