Class: Ramaze::Logger::RotatingInformer

Inherits:
Object
  • Object
show all
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.

Since:

  • 11-08-2009

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Examples:

# 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])

Parameters:

  • base_dir (String)

    The base directory for all the log files.

  • time_format (String) (defaults to: '%Y-%m-%d.log')

    The time format for all log files.

  • log_levels (Array) (defaults to: [:debug, :error, :info, :warn])

    Array containing the type of messages to log.

Since:

  • 11-08-2009



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_dirObject

Since:

  • 11-08-2009



49
50
51
# File 'lib/ramaze/log/rotatinginformer.rb', line 49

def base_dir
  @base_dir
end

#log_levelsObject

Since:

  • 11-08-2009



48
49
50
# File 'lib/ramaze/log/rotatinginformer.rb', line 48

def log_levels
  @log_levels
end

#time_formatObject

Since:

  • 11-08-2009



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?

Returns:

  • (TrueClass|FalseClass)

Since:

  • 11-08-2009



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.

Parameters:

  • tag (String)

    The type of message we’re logging.

  • messages (Array)

    An array of messages to log.

Since:

  • 11-08-2009



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, *messages)
  return unless @log_levels.include?(tag)

  # Update current log
  update_current_log

  messages.flatten!

  prefix = tag.to_s.upcase.ljust(5)

  messages.each do |message|
    @out.puts(log_interpolate(prefix, message))
  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.

Parameters:

  • prefix (String)
  • text (String)
  • time (Integer) (defaults to: timestamp)

Since:

  • 11-08-2009



179
180
181
182
183
184
185
186
# File 'lib/ramaze/log/rotatinginformer.rb', line 179

def log_interpolate(prefix, text, time = timestamp)
  message = class_trait[:format].dup

  vars = { '%time' => time, '%prefix' => prefix, '%text' => text }
  vars.each{|from, to| message.gsub!(from, to) }

  message
end

#shutdownObject

Close the file we log to if it isn’t closed already.

Since:

  • 11-08-2009



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

#timestampString

This uses timestamp trait or a date in the format of “%Y-%m-%d %H:%M:%S“

Returns:

  • (String)

Since:

  • 11-08-2009



194
195
196
197
# File 'lib/ramaze/log/rotatinginformer.rb', line 194

def timestamp
  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.

Parameters:

  • message (String)

    The data that has to be logged.

Author:

  • Yorick Peterse

Since:

  • 11-08-2009



214
215
216
# File 'lib/ramaze/log/rotatinginformer.rb', line 214

def write(message)
  log(:info, message)
end