Class: Log
Constant Summary
collapse
- LOG_FORMAT =
"[%l] %d :: %m"
- PRINT_DEBUG =
false
Class Method Summary
collapse
Methods inherited from Base
add_output, change, create, global_variable, global_variable=, load, output, output_endings, #output_types, type_from_ending
Class Method Details
.create_output(type, ending, file) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/libisi/log.rb', line 30
def self.create_output(type, ending, file)
name = file.basename.to_s
case ending
when "err","error"
new_logger(name, file.to_s, :level => Logger::ERROR)
when "debug","dbg"
new_logger(name, file.to_s, :level => Logger::DEBUG)
when "log"
new_logger(name, file.to_s, :level => stdout_log_level)
else
raise "Unexpected ending #{ending}"
end
end
|
.init(options = {}) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/libisi/log.rb', line 66
def self.init(options = {})
$log.debug("Redefine logger #{options.inspect}") if $log
case options[:logging]
when nil, "Log4r", "log4r"
require "log4r"
Log4r::Configurator.custom_levels(*options[:log_levels]) if options[:log_levels] and !options[:redefine]
Log4r::Logger.new("default")
options[:level] ||= (self.log_level_name or
(defined?(LOG_LEVEL) and LOG_LEVEL) or
ENV["LOG_LEVEL"] or "warn")
options[:level] = normalize_level(options[:level])
default_output = :stdout
options[:output] = nil if (options[:output].class == Array and options[:output].length == 0)
options[:output] ||= ((defined?(LOG_OUTPUT) and LOG_OUTPUT) or
ENV["LOG_OUTPUT"] or
default_output)
options[:output] = :stdout if options[:output] == "stdout"
options[:output] = [options[:output]] if options[:output].class != Array
$log.debug{"Create new logger now with Updated options: #{options.inspect}"} if $log
if !options[:redefine] or $log.nil? $log = Log4r::Logger.new((ENV["PROGRAM_IDENT"] or "default"))
else
$log.outputters = []
raise "Still outputters, maybe /usr/lib/ruby/1.8/logger.rb loaded" if $log.outputters.length != 0
@outputs = []
end
options[:output].each_with_index {|output, i|
new_logger("default#{i}", (output or :stdout), options)
}
else
raise "Unexpected logging mode #{options[:logging]}"
end
self.log_level = options[:level]
$log.debug("Logging initialized #{options.inspect}")
$log.outputters.each {|o| $log.debug("Outputter: #{o.inspect}") } if $log.debug?
end
|
.log_level ⇒ Object
199
200
201
202
|
# File 'lib/libisi/log.rb', line 199
def self.log_level
@log_level ||= Log4r::WARN
@log_level
end
|
.log_level=(level) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/libisi/log.rb', line 186
def self.log_level=(level)
$log.debug("Setting log_level #{level} (DEBUG is #{Log4r::DEBUG})")
$log.level = level
$log.outputters.each {|o|
next if o == $stdout_logger or o == $stderr_logger
o.level = level
}
self.stdout_log_level = level if $stdout_logger
@log_level = level
print "Log level is now #{$log.level} (set #{level})\n" if PRINT_DEBUG
$log.debug("Log level is now #{log_level_name}")
end
|
.log_level_name ⇒ Object
203
204
205
206
207
|
# File 'lib/libisi/log.rb', line 203
def self.log_level_name
return nil unless self.log_level
return nil unless defined?(Log4r::LNAMES)
Log4r::LNAMES[self.log_level]
end
|
.new_logger(name, output, options = {}) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
# File 'lib/libisi/log.rb', line 127
def self.new_logger(name, output, options = {})
raise "No outputter given." unless output
new_outputters = []
case output
when String, Pathname
new_outputters << Log4r::FileOutputter.new(name,:filename => output.to_s)
when :stdout
$stdout_logger||= Log4r::Outputter.stdout
$stderr_logger||= Log4r::Outputter.stderr
new_outputters << $stdout_logger
new_outputters << $stderr_logger
when :stderr
$stderr_logger||= Log4r::Outputter.stderr
new_outputters << $stderr_logger
when Hash
raise "unexpected output type #{output.inspect}"
else
new_outputters << Log4r::IOOutputter.new(name, output)
end
pattern = Log4r::PatternFormatter.new(:pattern => (options[:pattern] or LOG_FORMAT))
new_outputters.each_with_index {|out,i|
raise "Outputter #{i} is nil" unless out
old_len = $log.outputters.length
$log.add(out)
raise "No outputter added, maybe /usr/lib/ruby/1.8/logger.rb loaded" unless $log.outputters.length == (old_len + 1)
@outputs ||= []
@outputs << output
out.formatter = pattern
options[:only] = normalize_level(options[:only])
options[:level] = ((normalize_level(options[:level]) or $log.level))
if output == :stdout
next if out == $stderr_logger
raise "Only log level not allowed for :stdout" if options[:only]
self.stdout_log_level = options[:level]
next
end
if options[:only]
out.only_at(options[:only])
$log.level = options[:only] if $log.level > options[:only]
end
if (options[:level])
out.level = options[:level]
$log.level = options[:level] if $log.level > options[:level]
end
}
print "Added new logger at #{output.inspect} (#{options.inspect})\n" if PRINT_DEBUG
$log.debug{"Added new logger at #{output.inspect} (#{options.inspect})"}
end
|
.normalize_level(level) ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/libisi/log.rb', line 50
def self.normalize_level(level)
level = level.to_s if level.class == Symbol
level = case level
when String, Symbol
Log4r.const_get(level.to_s.upcase)
when Fixnum
level
when NilClass
nil
else
raise "Unexpected log level class #{level.class} #{level.inspect}"
end
print "Normalized log level is #{level}\n" if PRINT_DEBUG
level
end
|
.output_types ⇒ Object
26
27
28
|
# File 'lib/libisi/log.rb', line 26
def self.output_types
{"log" => ["err","error","debug","dbg","log"]}
end
|
.outputs ⇒ Object
126
|
# File 'lib/libisi/log.rb', line 126
def self.outputs; @outputs or []; end
|
.redefine_logger(options) ⇒ Object
45
46
47
48
|
# File 'lib/libisi/log.rb', line 45
def self.redefine_logger(options)
options[:redefine] ||= true
init(options)
end
|
.stdout_log_level ⇒ Object
209
210
211
|
# File 'lib/libisi/log.rb', line 209
def self.stdout_log_level
self.log_level
end
|
.stdout_log_level=(level) ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/libisi/log.rb', line 212
def self.stdout_log_level=(level)
return unless outputs.include?(:stdout)
print "set stdout level #{level} debug is: #{Log4r::DEBUG}\n" if PRINT_DEBUG
print "stdout_err_level: #{$stderr_logger.level}\n" if PRINT_DEBUG
print "stdout_log_level: #{$stdout_logger.level}\n" if PRINT_DEBUG
$stderr_logger.level = Log4r::ERROR
print "stderr_level: #{$stderr_logger.level}\n" if PRINT_DEBUG
raise "Level #{level} is too verbose" if level == 0
raise "Level #{level} is too quiet" if level >= Log4r::LEVELS - 2
if level >= (Log4r::LEVELS - 3)
$log.debug("Removing stdout logger")
$log.remove($stdout_logger.name)
print "removed stdout_logger\n" if PRINT_DEBUG
else
unless $log.outputters.include?($stdout_logger)
print "Added stdout_logger\n" if PRINT_DEBUG
$log.add($stdout_logger)
end
levels = (level..Log4r::LEVELS-4).to_a
print "Stdout levels: #{levels.inspect}\n" if PRINT_DEBUG
$stdout_logger.only_at(*levels)
end
end
|