Class: Ramaze::Logger::Informer

Inherits:
Object
  • Object
show all
Includes:
Ramaze::Logging, Traited
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

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. You can spcify

Examples:

Informer.new                    #=> logs to stdout with all levels being
                                    shown.
Informer.new($stderr)           #=> same, but to stderr
Informer.new("foo.log")         #=> same, but logs to the file foo.log
                                    (or creates it if it doesn't exist yet)
Informer.new($stdout, [:info])  #=> show only #info messages to stdout.


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ramaze/log/informer.rb', line 45

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

#colorizeObject

Returns the value of attribute colorize.



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

def colorize
  @colorize
end

#log_levelsObject

Returns the value of attribute log_levels.



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

def log_levels
  @log_levels
end

#outObject

Returns the value of attribute out.



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

def out
  @out
end

Instance Method Details

#closed?Boolean

is @out closed?

Returns:

  • (Boolean)


123
124
125
# File 'lib/ramaze/log/informer.rb', line 123

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

#log(tag, *messages) ⇒ Object

Integration to Logging.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ramaze/log/informer.rb', line 82

def log tag, *messages
  return if closed? || !@log_levels.include?(tag)
  messages.flatten!

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

  if @colorize
    color = COLORS[tag] ||= :white
    prefix.replace prefix.send(color)
  end

  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.



103
104
105
106
107
108
109
110
# File 'lib/ramaze/log/informer.rb', line 103

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.



73
74
75
76
77
78
# File 'lib/ramaze/log/informer.rb', line 73

def shutdown
  if @out.respond_to?(:close)
    Log.debug("close, #{@out.inspect}")
    @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"


116
117
118
119
# File 'lib/ramaze/log/informer.rb', line 116

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