Class: Ramaze::Logger::RotatingInformer
- Inherits:
-
Object
- Object
- Ramaze::Logger::RotatingInformer
- Includes:
- Innate::Traited, Ramaze::Logging
- Defined in:
- lib/ramaze/log/rotatinginformer.rb
Overview
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:
%time
: will be replaced by the current time.%prefix
: the log level such as "ERROR" or "INFO".%text
: the actual log message.
Instance Attribute Summary (collapse)
Instance Method Summary (collapse)
-
- (TrueClass|FalseClass) closed?
Is
@out
closed?. -
- (RotatingInformer) initialize(base_dir, time_format = '%Y-%m-%d.log', log_levels = [:debug, :error, :info, :warn])
constructor
Create a new instance of RotatingInformer.
-
- (Object) log(tag, *messages)
Integration to Logging.
-
- (Object) log_interpolate(prefix, text, time = timestamp)
Takes the prefix (tag), text and timestamp and applies it to the :format trait.
-
- (Object) shutdown
Close the file we log to if it isn't closed already.
-
- (String) timestamp
This uses timestamp trait or a date in the format of
%Y-%m-%d %H:%M:%S
. -
- (Object) update_current_log
private
Checks whether current filename is still valid.
-
- (Object) write(message)
Method that is called by Rack::CommonLogger when logging data to a file.
Methods included from Ramaze::Logging
#debug, #debug?, #dev, #error, #info, #tag_log, #warn
Methods included from Innate::Traited
#ancestral_trait, #ancestral_trait_values, #class_trait, #each_ancestral_trait, included, #trait
Constructor Details
- (RotatingInformer) initialize(base_dir, time_format = '%Y-%m-%d.log', log_levels = [:debug, :error, :info, :warn])
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
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 87 def initialize(base_dir, time_format = '%Y-%m-%d.log', log_levels = [:debug, :error, :info, :warn]) # Verify and set base directory send :base_dir=, base_dir, true @time_format = time_format @log_levels = log_levels # Keep track of log shutdown (to prevent StackErrors due to recursion) @in_shutdown = false end |
Instance Attribute Details
- (Object) base_dir
49 50 51 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 49 def base_dir @base_dir end |
- (Object) log_levels
48 49 50 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 48 def log_levels @log_levels end |
- (Object) time_format
48 49 50 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 48 def time_format @time_format end |
Instance Method Details
- (TrueClass|FalseClass) closed?
Is @out
closed?
204 205 206 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 204 def closed? @out.respond_to?(:closed?) && @out.closed? end |
- (Object) log(tag, *messages)
Integration to Logging.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 154 def log(tag, *) return unless @log_levels.include?(tag) # Update current log update_current_log .flatten! prefix = tag.to_s.upcase.ljust(5) .each do || @out.puts(log_interpolate(prefix, )) end @out.flush if @out.respond_to?(:flush) end |
- (Object) log_interpolate(prefix, text, time = timestamp)
Takes the prefix (tag), text and timestamp and applies it to the :format trait.
179 180 181 182 183 184 185 186 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 179 def log_interpolate(prefix, text, time = ) = class_trait[:format].dup vars = { '%time' => time, '%prefix' => prefix, '%text' => text } vars.each{|from, to| .gsub!(from, to) } end |
- (Object) shutdown
Close the file we log to if it isn't closed already.
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 137 def shutdown if @out.respond_to?(:close) unless @in_shutdown @in_shutdown = true Log.debug("close, #{@out.inspect}") @in_shutdown = false end @out.close end end |
- (String) timestamp
This uses timestamp trait or a date in the format of
%Y-%m-%d %H:%M:%S
194 195 196 197 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 194 def mask = class_trait[:timestamp] Time.now.strftime(mask || "%Y-%m-%d %H:%M:%S") end |
- (Object) update_current_log (private)
Checks whether current filename is still valid. If not, update the current log to point at the new filename.
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 224 def update_current_log out = File.join(@base_dir, Time.now.strftime(@time_format)) if @out.nil? || @out.path != out # Close old log if necessary shutdown unless @out.nil? || closed? # Start new log @out = File.open(out, 'ab+') end end |
- (Object) write(message)
Method that is called by Rack::CommonLogger when logging data to a file.
214 215 216 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 214 def write() log(:info, ) end |