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
-
#closed? ⇒ TrueClass|FalseClass
Is “@out“ closed?.
-
#initialize(base_dir, time_format = '%Y-%m-%d.log', log_levels = [:debug, :error, :info, :warn]) ⇒ RotatingInformer
constructor
Create a new instance of RotatingInformer.
-
#log(tag, *messages) ⇒ Object
Integration to Logging.
-
#log_interpolate(prefix, text, time = timestamp) ⇒ Object
Takes the prefix (tag), text and timestamp and applies it to the :format trait.
-
#shutdown ⇒ Object
Close the file we log to if it isn’t closed already.
-
#timestamp ⇒ String
This uses timestamp trait or a date in the format of “%Y-%m-%d %H:%M:%S“.
-
#write(message) ⇒ Object
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
Constructor Details
#initialize(base_dir, time_format = '%Y-%m-%d.log', log_levels = [:debug, :error, :info, :warn]) ⇒ RotatingInformer
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
#base_dir ⇒ Object
49 50 51 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 49 def base_dir @base_dir end |
#log_levels ⇒ Object
48 49 50 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 48 def log_levels @log_levels end |
#time_format ⇒ Object
48 49 50 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 48 def time_format @time_format end |
Instance Method Details
#closed? ⇒ TrueClass|FalseClass
Is “@out“ closed?
204 205 206 |
# File 'lib/ramaze/log/rotatinginformer.rb', line 204 def closed? @out.respond_to?(:closed?) && @out.closed? end |
#log(tag, *messages) ⇒ Object
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 |
#log_interpolate(prefix, text, time = timestamp) ⇒ Object
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 |
#shutdown ⇒ Object
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 |
#timestamp ⇒ String
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 |
#write(message) ⇒ Object
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 |