Class: Ramaze::Logger::RotatingInformer

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

Overview

A customized logger (based on Informer) that creates multiple log files based on time

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:

RotatingInformer.new('logs')
                                #=> Creates logs in directory called logs.
                                    The generated filenames will be in the
                                    form YYYY-MM-DD.log
RotatingInformer.new('logs', '%Y-%m.txt')
                                #=> Creates logs in directory called logs.
                                    The generated filenames will be in the
                                    form YYYY-MM.txt
RotatingInformer.new('logs', '%Y-%m.txt', [:error])
                                #=> 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.


47
48
49
50
51
52
53
54
55
56
# File 'lib/ramaze/log/rotatinginformer.rb', line 47

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

Returns the value of attribute base_dir.



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

def base_dir
  @base_dir
end

#log_levelsObject

Returns the value of attribute log_levels.



11
12
13
# File 'lib/ramaze/log/rotatinginformer.rb', line 11

def log_levels
  @log_levels
end

#time_formatObject

Returns the value of attribute time_format.



11
12
13
# File 'lib/ramaze/log/rotatinginformer.rb', line 11

def time_format
  @time_format
end

Instance Method Details

#closed?Boolean

is @out closed?

Returns:

  • (Boolean)


145
146
147
# File 'lib/ramaze/log/rotatinginformer.rb', line 145

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

#log(tag, *messages) ⇒ Object

Integration to Logging.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ramaze/log/rotatinginformer.rb', line 104

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.



125
126
127
128
129
130
131
132
# File 'lib/ramaze/log/rotatinginformer.rb', line 125

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.



91
92
93
94
95
96
97
98
99
100
# File 'lib/ramaze/log/rotatinginformer.rb', line 91

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

#timestampObject

This uses Global.inform_timestamp or a date in the format of

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


138
139
140
141
# File 'lib/ramaze/log/rotatinginformer.rb', line 138

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