Class: Ramaze::Logger::Informer
- Inherits:
-
Object
- Object
- Ramaze::Logger::Informer
- Includes:
- Innate::Traited, Ramaze::Logging
- Defined in:
- lib/ramaze/log/informer.rb
Overview
A minimal logger for Ramaze, supports files, CLI, colors and some customization.
Constant Summary collapse
- COLORS =
Which tag should be in what color
{ :dev => :blue, :debug => :yellow, :info => :green, :warn => :red, :error => :red, }
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#closed? ⇒ Boolean
Is @out closed?.
-
#initialize(out = $stdout, log_levels = [:debug, :error, :info, :warn]) ⇒ Informer
constructor
Create a new instance of Informer.
-
#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 ⇒ Object
This uses timestamp trait or a date in the format of %Y-%m-%d %H:%M:%S # => “2007-01-19 21:09:32”.
Methods included from Ramaze::Logging
#debug, #debug?, #dev, #error, #info, #tag_log, #warn
Constructor Details
#initialize(out = $stdout, log_levels = [:debug, :error, :info, :warn]) ⇒ Informer
Create a new instance of Informer.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ramaze/log/informer.rb', line 54 def initialize(out = $stdout, log_levels = [:debug, :error, :info, :warn]) @colorize = false @out = case out when STDOUT, :stdout, 'stdout' $stdout when STDERR, :stderr, 'stderr' $stderr when IO out else if out.respond_to?(:puts) out else File.open(out.to_s, 'ab+') end end if @out.respond_to?(:tty?) and class_trait[:colorize] @colorize = @out.tty? end @log_levels = log_levels end |
Instance Attribute Details
#colorize ⇒ Object
15 16 17 |
# File 'lib/ramaze/log/informer.rb', line 15 def colorize @colorize end |
#log_levels ⇒ Object
15 16 17 |
# File 'lib/ramaze/log/informer.rb', line 15 def log_levels @log_levels end |
#out ⇒ Object
15 16 17 |
# File 'lib/ramaze/log/informer.rb', line 15 def out @out end |
Instance Method Details
#closed? ⇒ Boolean
Is @out closed?
144 145 146 |
# File 'lib/ramaze/log/informer.rb', line 144 def closed? @out.respond_to?(:closed?) and @out.closed? end |
#log(tag, *messages) ⇒ Object
Integration to Logging
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/ramaze/log/informer.rb', line 96 def log tag, * return if closed? || !@log_levels.include?(tag) .flatten! prefix = tag.to_s.upcase.ljust(5) if @colorize color = COLORS[tag] ||= :white prefix.replace prefix.send(color) end .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.
122 123 124 125 126 127 128 129 |
# File 'lib/ramaze/log/informer.rb', line 122 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.
83 84 85 86 87 88 |
# File 'lib/ramaze/log/informer.rb', line 83 def shutdown if @out.respond_to?(:close) Log.debug("close, #{@out.inspect}") @out.close end end |
#timestamp ⇒ Object
This uses timestamp trait or a date in the format of
%Y-%m-%d %H:%M:%S
# => "2007-01-19 21:09:32"
136 137 138 139 |
# File 'lib/ramaze/log/informer.rb', line 136 def mask = class_trait[:timestamp] Time.now.strftime(mask || "%Y-%m-%d %H:%M:%S") end |