Class: HighLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/highlogger.rb

Overview

This class implements a simple heirarchical logging system.

Instance Method Summary collapse

Constructor Details

#initialize(*super_args) ⇒ HighLogger

Returns a new instance of HighLogger.



8
9
10
11
12
# File 'lib/highlogger.rb', line 8

def initialize(*super_args)
  super(*super_args)
  @logmap = {}
  @default_level = Logger::WARN
end

Instance Method Details

#add(component, severity, message = nil, progname = nil, &block) ⇒ Object Also known as: log

Adds a message to the log, with the given component name, severity, message (or block if no message), and program name.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/highlogger.rb', line 17

def add(component, severity, message = nil, progname = nil, &block)
  return if severity == :none

  level = level_for_component(component)
  return if level and severity < level

  # Logic copied from Logger class
  if message.nil?
    if block_given?
	message = yield
    else
	message = progname
	progname = @progname
    end
  end

  super(severity, component + ": " + message, progname, &block);
end

#debug(component, progname = nil, &block) ⇒ Object

Logs a DEBUG message for the given component name and progname, using the given block as the output message.



81
82
83
# File 'lib/highlogger.rb', line 81

def debug(component, progname = nil, &block)
  add(component, Logger::DEBUG, nil, progname, &block)
end

#debug?(*args) ⇒ Boolean

Returns whether a DEBUG log message will be visible for the given component, if any, or by default if no arg is given.

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/highlogger.rb', line 41

def debug?(*args)
  return super() if args.empty?
  @logmap[args[0]] <= Logger::DEBUG
end

#error(component, progname = nil, &block) ⇒ Object

Logs an ERROR message for the given component name and progname, using the given block as the output message.



102
103
104
# File 'lib/highlogger.rb', line 102

def error(component, progname = nil, &block)
  add(component, Logger::ERROR, nil, progname, &block)
end

#error?(*args) ⇒ Boolean

Returns whether an ERROR log message will be visible for the given component, if any, or by default if no arg is given.

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/highlogger.rb', line 65

def error?(*args)
  return super() if args.empty?
  @logmap[args[0]] <= Logger::ERROR
end

#fatal(component, progname = nil, &block) ⇒ Object

Logs a FATAL message for the given component name and progname, using the given block as the output message.



109
110
111
# File 'lib/highlogger.rb', line 109

def fatal(component, progname = nil, &block)
  add(component, Logger::FATAL, nil, progname, &block)
end

#fatal?(*args) ⇒ Boolean

Returns whether a FATAL log message will be visible for the given component, if any, or by default if no arg is given.

Returns:

  • (Boolean)


73
74
75
76
# File 'lib/highlogger.rb', line 73

def fatal?(*args)
  return super() if args.empty?
  @logmap[args[0]] <= Logger::FATAL
end

#info(component, progname = nil, &block) ⇒ Object

Logs an INFO message for the given component name and progname, using the given block as the output message.



88
89
90
# File 'lib/highlogger.rb', line 88

def info(component, progname = nil, &block)
  add(component, Logger::INFO, nil, progname, &block)
end

#info?(*args) ⇒ Boolean

Returns whether an INFO log message will be visible for the given component, if any, or by default if no arg is given.

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/highlogger.rb', line 49

def info?(*args)
  return super() if args.empty?
  @logmap[args[0]] <= Logger::INFO
end

#read_config_buffer(buf) ⇒ Object

Have this logger read a configuration buffer to determine what components will log at what levels.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/highlogger.rb', line 134

def read_config_buffer(buf)
  buf.each_line do |line|
    mobj = /^\s*([A-Za-z0-9_:]+)\s*=\s*(.*?)\s*$/.match(line)
    raise "Can't parse config line '#{line}'!" unless mobj

    loglevel = parse_loglevel(mobj[2])
    if mobj[1] =~ /default/i
      @default_level = loglevel
	#parent.level = loglevel
	next
    end

    @logmap[mobj[1]] = loglevel
  end
end

#read_config_file(filename) ⇒ Object

Have this logger read a configuration file to determine what components will log at what levels.



125
126
127
128
129
# File 'lib/highlogger.rb', line 125

def read_config_file(filename)
  f = File.new(filename)
  buf = f.read()
  read_config_buffer(buf)
end

#unknown(component, progname = nil, &block) ⇒ Object

Logs an UNKNOWN message for the given component name and progname, using the given block as the output message. UNKNOWN is more severe than even FATAL.



117
118
119
# File 'lib/highlogger.rb', line 117

def unknown(component, progname = nil, &block)
  add(component, Logger::UNKNOWN, nil, progname, &block)
end

#warn(component, progname = nil, &block) ⇒ Object

Logs a WARN message for the given component name and progname, using the given block as the output message.



95
96
97
# File 'lib/highlogger.rb', line 95

def warn(component, progname = nil, &block)
  add(component, Logger::WARN, nil, progname, &block)
end

#warn?(*args) ⇒ Boolean

Returns whether a WARN log message will be visible for the given component, if any, or by default if no arg is given.

Returns:

  • (Boolean)


57
58
59
60
# File 'lib/highlogger.rb', line 57

def warn?(*args)
  return super() if args.empty?
  @logmap[args[0]] <= Logger::WARN
end