-
Notifications
You must be signed in to change notification settings - Fork 23
/
alogr.rb
76 lines (61 loc) · 1.69 KB
/
alogr.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
require "aio_logger"
module AlogR
Levels = { :emergency => 0, :alert => 1, :critical => 2, :error => 3, :warning => 4, :notice => 5, :info => 6, :debug => 7 }
class Logger
class << self
attr_accessor :default_log_level
end
def initialize(options = {})
puts "In Ruby initialize method"
case options
when String:
options = {:log => options}
when Hash:
options[:log] ||= "log/default.log"
else
raise "AlogR: Invalid configuration"
end
@log_interval = options[:log_interval] || 0.01
default_log_level = options[:default_log_level] || :info
AlogR::Levels.keys.each do | key |
file_name = options[key] || options[:log]
$alogr_log_files[AlogR::Levels[key]] = file_name
system("mkdir -p #{File.dirname(file_name)} ; touch #{file_name}")
end
Thread.abort_on_exception = true
Thread.new do
loop do
sleep( @log_interval )
flush_log_buffer
end
Thread.exit
end
end
def log(string, level = default_log_level)
$alogr_buffer << [level, string]
end
def method_missing(meth, *options)
puts "method: #{meth.inspect}, options: #{options.inspect}"
if AlogR::Levels.include?( meth )
log(meth, options.first)
else
super.method_missing(meth, *options)
end
end
end
end
require "alogr/version"
class Object
def log(level = :info)
if self.class == String
if AlogR::Levels.has_key?(level)
$logger.log(self, AlogR::Levels[level])
else
raise "Error: No such log level #{level}"
end
else
self.to_s.log
end
self
end
end