Class: Ramaze::Logger::RotatingInformer

Inherits:
Object
  • Object
show all
Includes:
Innate::Traited, Ramaze::Logging
Defined in:
lib/ramaze/log/rotatinginformer.rb

Overview

A customized logger (based on Informer) that creates multiple log files based on current date.

Since:

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Ramaze::Logging

#debug, #debug?, #dev, #error, #info, #tag_log, #warn

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

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



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ramaze/log/rotatinginformer.rb', line 54

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

Returns the value of attribute base_dir

Since:

  • 11-08-2009



14
15
16
# File 'lib/ramaze/log/rotatinginformer.rb', line 14

def base_dir
  @base_dir
end

- (Object) log_levels

Returns the value of attribute log_levels

Since:

  • 11-08-2009



13
14
15
# File 'lib/ramaze/log/rotatinginformer.rb', line 13

def log_levels
  @log_levels
end

- (Object) time_format

Returns the value of attribute time_format

Since:

  • 11-08-2009



13
14
15
# File 'lib/ramaze/log/rotatinginformer.rb', line 13

def time_format
  @time_format
end

Instance Method Details

- (Boolean) closed?

Is @out closed?

Returns:

  • (Boolean)

Since:

  • 11-08-2009



169
170
171
# File 'lib/ramaze/log/rotatinginformer.rb', line 169

def closed?
  @out.respond_to?(:closed?) && @out.closed?
end

- (Object) log(tag, *messages)

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/ramaze/log/rotatinginformer.rb', line 122

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

- (Object) log_interpolate(prefix, text, time = timestamp)

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



147
148
149
150
151
152
153
154
# File 'lib/ramaze/log/rotatinginformer.rb', line 147

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

- (Object) shutdown

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

Since:

  • 11-08-2009



105
106
107
108
109
110
111
112
113
114
# File 'lib/ramaze/log/rotatinginformer.rb', line 105

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

- (Object) timestamp

This uses timestamp trait or a date in the format of

%Y-%m-%d %H:%M:%S
# => "2007-01-19 21:09:32"

Since:

  • 11-08-2009



161
162
163
164
# File 'lib/ramaze/log/rotatinginformer.rb', line 161

def timestamp
  mask = class_trait[:timestamp]
  Time.now.strftime(mask || "%Y-%m-%d %H:%M:%S")
end

- (Object) write(message)

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



179
180
181
# File 'lib/ramaze/log/rotatinginformer.rb', line 179

def write message
  log(:info, message)
end