Class: Semlogger
- Inherits:
-
Logger
show all
- Defined in:
- lib/semlogger.rb,
lib/semlogger.rb
Defined Under Namespace
Classes: Base, CustomType, Default, Filter, Formatter, Multiplex, Output, Printer, Rack, Rotate, Tailer, Writer
Constant Summary
collapse
- @@progname =
nil
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(logdev = nil, *a, &e) ⇒ Semlogger
Returns a new instance of Semlogger.
100
101
102
103
104
105
106
107
|
# File 'lib/semlogger.rb', line 100
def initialize logdev = nil, *a, &e
case logdev
when String, nil then logdev = ::Semlogger::Writer.new logdev
end
@progname = a[0] || @@progname
@level, @data, @tags, @logdev = DEBUG, {}, [], logdev
self.class.logger = self if !self.class.logger && self.class.logger.is_a?( Semlogger::Default)
end
|
Class Attribute Details
Returns the value of attribute logger.
73
74
75
|
# File 'lib/semlogger.rb', line 73
def logger
@logger
end
|
Returns the value of attribute progname.
73
74
75
|
# File 'lib/semlogger.rb', line 73
def progname
@progname
end
|
Instance Attribute Details
Returns the value of attribute level.
66
67
68
|
# File 'lib/semlogger.rb', line 66
def level
@level
end
|
Returns the value of attribute logdev.
66
67
68
|
# File 'lib/semlogger.rb', line 66
def logdev
@logdev
end
|
Returns the value of attribute progname.
66
67
68
|
# File 'lib/semlogger.rb', line 66
def progname
@progname
end
|
Class Method Details
.custom(*a) ⇒ Object
94
|
# File 'lib/semlogger.rb', line 94
def custom( *a) CustomType.new( *a).tap {|t| t.logger = self.logger } end
|
.new_rails_logger(config, logdev = nil) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/semlogger.rb', line 75
def new_rails_logger config, logdev = nil
require 'semlogger/rack'
logdev ||= ::Rails.root.join 'log', "#{::Rails.env.to_s.gsub('%', '%%')}.%Y-%m-%d.%$.log"
logdev = logdev.to_s
logger = nil
if Rails.env.production?
logger = new logdev
logger.level = Semlogger::INFO
elsif Rails.env.development?
logger = new Semlogger::Multiplex.new( Semlogger::FInfo.new( Semlogger::Printer.new), Semlogger::Writer.new( logdev))
logger.level = Semlogger::DEBUG
else
logger = new logdev
logger.level = Semlogger::DEBUG
end
config.middleware.swap Rails::Rack::Logger, Semlogger::Rack, [], {reqid: :uuid}
config.logger = logger
end
|
Instance Method Details
#add(severity, message = nil, progname = nil, &block) ⇒ Object
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/semlogger.rb', line 118
def add severity, message = nil, progname = nil, &block
severity ||= UNKNOWN
return true if @logdev.nil? or severity < @level
progname ||= @progname
if message.nil?
if block_given?
message = yield
else
message = progname
progname = @progname
end
end
@logdev.add severity, Time.new, progname, format_data( @data), format_tags( @tags), format_msg( message)
end
|
#caller(&e) ⇒ Object
167
168
169
|
# File 'lib/semlogger.rb', line 167
def caller &e
data caller: Kernel.method(:caller), &e
end
|
#custom(*a) ⇒ Object
96
|
# File 'lib/semlogger.rb', line 96
def custom( *a) CustomType.new( *a).tap {|t| t.logger = self } end
|
#data(data, &e) ⇒ Object
158
159
160
161
162
163
164
165
|
# File 'lib/semlogger.rb', line 158
def data data, &e
return @data unless e
@data.update data
keys = data.keys
yield
ensure
keys.each &data.method( :delete)
end
|
153
154
155
156
|
# File 'lib/semlogger.rb', line 153
def format_data data
data = data ? data.dup : {}
data.each {|k,v| data[k] = format_obj v }
end
|
133
134
135
136
137
138
139
|
# File 'lib/semlogger.rb', line 133
def format_msg msg
msg = msg.to_semlogger
case msg
when Array then msg
else [msg.class.name.to_sym, msg.inspect]
end
end
|
141
142
143
144
145
146
|
# File 'lib/semlogger.rb', line 141
def format_obj obj
case obj
when Proc then obj.call
else obj
end
end
|
148
149
150
151
|
# File 'lib/semlogger.rb', line 148
def format_tags tags
tags = tags ? tags.dup : []
tags.map &method( :format_obj)
end
|
#log_level ⇒ Object
70
|
# File 'lib/semlogger.rb', line 70
def log_level() @level end
|
#log_level=(level) ⇒ Object
69
|
# File 'lib/semlogger.rb', line 69
def log_level=( level) @level = level end
|
#tagged(*tags, &e) ⇒ Object
109
110
111
112
113
114
115
116
|
# File 'lib/semlogger.rb', line 109
def tagged *tags, &e
@tags += tags.flatten.compact
tags = tags.size
yield
ensure
@tags.slice! -tags .. -1
end
|
#thread(&e) ⇒ Object
171
172
173
|
# File 'lib/semlogger.rb', line 171
def thread &e
data thread: Proc.new { Thread.current.object_id }, &e
end
|