Class: Fluent::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/log.rb

Direct Known Subclasses

PluginLogger

Defined Under Namespace

Modules: TTYColor

Constant Summary collapse

LEVEL_TRACE =
0
LEVEL_DEBUG =
1
LEVEL_INFO =
2
LEVEL_WARN =
3
LEVEL_ERROR =
4
LEVEL_FATAL =
5
LEVEL_TEXT =
%w(trace debug info warn error fatal)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, opts = {}) ⇒ Log

Returns a new instance of Log.



56
57
58
59
60
61
62
63
64
65
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
# File 'lib/fluent/log.rb', line 56

def initialize(logger, opts={})
  # overwrites logger.level= so that config reloading resets level of Fluentd::Log
  orig_logger_level_setter = logger.class.public_instance_method(:level=).bind(logger)
  me = self
  # The original ruby logger sets the number as each log level like below.
  # DEBUG = 0
  # INFO  = 1
  # WARN  = 2
  # ERROR = 3
  # FATAL = 4
  # Serverengine use this original log number. In addition to this, serverengine sets -1 as TRACE level.
  # TRACE = -1
  #
  # On the other hand, in fluentd side, it sets the number like below.
  # TRACE = 0
  # DEBUG = 1
  # INFO  = 2
  # WARN  = 3
  # ERROR = 4
  # FATAL = 5
  #
  # Then fluentd's level is set as serverengine's level + 1.
  # So if serverengine's logger level is changed, fluentd's log level will be changed to that + 1.
  logger.define_singleton_method(:level=) {|level| orig_logger_level_setter.call(level); me.level = self.level + 1 }

  @logger = logger
  @out = logger.instance_variable_get(:@logdev)
  @level = logger.level + 1
  @debug_mode = false
  @self_event = false
  @tag = 'fluent'
  @time_format = '%Y-%m-%d %H:%M:%S %z '
  @depth_offset = 1
  enable_color out.tty?
  # TODO: This variable name is unclear so we should change to better name.
  @threads_exclude_events = []

  # Fluent::Engine requires Fluent::Log, so we must take that object lazily
  @engine = Fluent.const_get('Engine')
  @optional_header = nil
  @optional_attrs = nil

  @suppress_repeated_stacktrace = opts[:suppress_repeated_stacktrace]
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



113
114
115
# File 'lib/fluent/log.rb', line 113

def level
  @level
end

#optional_attrsObject

Returns the value of attribute optional_attrs.



116
117
118
# File 'lib/fluent/log.rb', line 116

def optional_attrs
  @optional_attrs
end

#optional_headerObject

Returns the value of attribute optional_header.



116
117
118
# File 'lib/fluent/log.rb', line 116

def optional_header
  @optional_header
end

#outObject

Returns the value of attribute out.



112
113
114
# File 'lib/fluent/log.rb', line 112

def out
  @out
end

#tagObject

Returns the value of attribute tag.



114
115
116
# File 'lib/fluent/log.rb', line 114

def tag
  @tag
end

#time_formatObject

Returns the value of attribute time_format.



115
116
117
# File 'lib/fluent/log.rb', line 115

def time_format
  @time_format
end

Class Method Details

.str_to_level(log_level_str) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fluent/log.rb', line 44

def self.str_to_level(log_level_str)
  case log_level_str.downcase
  when "trace" then LEVEL_TRACE
  when "debug" then LEVEL_DEBUG
  when "info"  then LEVEL_INFO
  when "warn"  then LEVEL_WARN
  when "error" then LEVEL_ERROR
  when "fatal" then LEVEL_FATAL
  else raise "Unknown log level: level = #{log_level_str}"
  end
end

Instance Method Details

#debug(*args, &block) ⇒ Object Also known as: DEBUG



195
196
197
198
199
200
201
# File 'lib/fluent/log.rb', line 195

def debug(*args, &block)
  return if @level > LEVEL_DEBUG
  args << block.call if block
  time, msg = event(:debug, args)
  puts [@color_debug, caller_line(time, @depth_offset, LEVEL_DEBUG), msg, @color_reset].join
rescue
end

#debug_backtrace(backtrace = $!.backtrace) ⇒ Object



204
205
206
# File 'lib/fluent/log.rb', line 204

def debug_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_DEBUG)
end

#disable_events(thread) ⇒ Object

If you want to suppress event emitting in specific thread, please use this method. Events in passed thread are never emitted.



167
168
169
# File 'lib/fluent/log.rb', line 167

def disable_events(thread)
  @threads_exclude_events.push(thread) unless @threads_exclude_events.include?(thread)
end

#dupObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/fluent/log.rb', line 101

def dup
  dl_opts = {}
  dl_opts[:log_level] = @level - 1
  logger = ServerEngine::DaemonLogger.new(@out, dl_opts)
  clone = self.class.new(logger, suppress_repeated_stacktrace: @suppress_repeated_stacktrace)
  clone.tag = @tag
  clone.time_format = @time_format
  # optional headers/attrs are not copied, because new PluginLogger should have another one of it
  clone
end

#enable_color(b = true) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/fluent/log.rb', line 144

def enable_color(b=true)
  if b
    @color_trace = TTYColor::BLUE
    @color_debug = TTYColor::WHITE
    @color_info  = TTYColor::GREEN
    @color_warn  = TTYColor::YELLOW
    @color_error = TTYColor::MAGENTA
    @color_fatal = TTYColor::RED
    @color_reset = TTYColor::NORMAL
  else
    @color_trace = ''
    @color_debug = ''
    @color_info  = ''
    @color_warn  = ''
    @color_error = ''
    @color_fatal = ''
    @color_reset = ''
  end
  self
end

#enable_color?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/fluent/log.rb', line 140

def enable_color?
  !@color_reset.empty?
end

#enable_debug(b = true) ⇒ Object



130
131
132
133
# File 'lib/fluent/log.rb', line 130

def enable_debug(b=true)
  @debug_mode = b
  self
end

#enable_event(b = true) ⇒ Object



135
136
137
138
# File 'lib/fluent/log.rb', line 135

def enable_event(b=true)
  @self_event = b
  self
end

#error(*args, &block) ⇒ Object Also known as: ERROR



249
250
251
252
253
254
255
# File 'lib/fluent/log.rb', line 249

def error(*args, &block)
  return if @level > LEVEL_ERROR
  args << block.call if block
  time, msg = event(:error, args)
  puts [@color_error, caller_line(time, @depth_offset, LEVEL_ERROR), msg, @color_reset].join
rescue
end

#error_backtrace(backtrace = $!.backtrace) ⇒ Object



258
259
260
# File 'lib/fluent/log.rb', line 258

def error_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_ERROR)
end

#fatal(*args, &block) ⇒ Object Also known as: FATAL



267
268
269
270
271
272
273
# File 'lib/fluent/log.rb', line 267

def fatal(*args, &block)
  return if @level > LEVEL_FATAL
  args << block.call if block
  time, msg = event(:fatal, args)
  puts [@color_fatal, caller_line(time, @depth_offset, LEVEL_FATAL), msg, @color_reset].join
rescue
end

#fatal_backtrace(backtrace = $!.backtrace) ⇒ Object



276
277
278
# File 'lib/fluent/log.rb', line 276

def fatal_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_FATAL)
end

#flushObject



293
294
295
# File 'lib/fluent/log.rb', line 293

def flush
  @out.flush
end

#info(*args, &block) ⇒ Object Also known as: INFO



213
214
215
216
217
218
219
# File 'lib/fluent/log.rb', line 213

def info(*args, &block)
  return if @level > LEVEL_INFO
  args << block.call if block
  time, msg = event(:info, args)
  puts [@color_info, caller_line(time, @depth_offset, LEVEL_INFO), msg, @color_reset].join
rescue
end

#info_backtrace(backtrace = $!.backtrace) ⇒ Object



222
223
224
# File 'lib/fluent/log.rb', line 222

def info_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_INFO)
end

#logdev=(logdev) ⇒ Object



118
119
120
121
122
# File 'lib/fluent/log.rb', line 118

def logdev=(logdev)
  @out = logdev
  @logger.instance_variable_set(:@logdev, logdev)
  nil
end

#on_debug(&block) ⇒ Object



190
191
192
193
# File 'lib/fluent/log.rb', line 190

def on_debug(&block)
  return if @level > LEVEL_DEBUG
  block.call if block
end

#on_error(&block) ⇒ Object



244
245
246
247
# File 'lib/fluent/log.rb', line 244

def on_error(&block)
  return if @level > LEVEL_ERROR
  block.call if block
end

#on_fatal(&block) ⇒ Object



262
263
264
265
# File 'lib/fluent/log.rb', line 262

def on_fatal(&block)
  return if @level > LEVEL_FATAL
  block.call if block
end

#on_info(&block) ⇒ Object



208
209
210
211
# File 'lib/fluent/log.rb', line 208

def on_info(&block)
  return if @level > LEVEL_INFO
  block.call if block
end

#on_trace(&block) ⇒ Object



171
172
173
174
# File 'lib/fluent/log.rb', line 171

def on_trace(&block)
  return if @level > LEVEL_TRACE
  block.call if block
end

#on_warn(&block) ⇒ Object



226
227
228
229
# File 'lib/fluent/log.rb', line 226

def on_warn(&block)
  return if @level > LEVEL_WARN
  block.call if block
end

#puts(msg) ⇒ Object



280
281
282
283
284
285
286
287
# File 'lib/fluent/log.rb', line 280

def puts(msg)
  @logger << msg + "\n"
  @out.flush
  msg
rescue
  # FIXME
  nil
end

#reopen!Object



124
125
126
127
128
# File 'lib/fluent/log.rb', line 124

def reopen!
  # do noting in @logger.reopen! because it's already reopened in Supervisor.load_config
  @logger.reopen! if @logger
  nil
end

#resetObject



297
298
299
# File 'lib/fluent/log.rb', line 297

def reset
  @out.reset if @out.respond_to?(:reset)
end

#trace(*args, &block) ⇒ Object Also known as: TRACE



176
177
178
179
180
181
182
183
# File 'lib/fluent/log.rb', line 176

def trace(*args, &block)
  return if @level > LEVEL_TRACE
  args << block.call if block
  time, msg = event(:trace, args)
  puts [@color_trace, caller_line(time, @depth_offset, LEVEL_TRACE), msg, @color_reset].join
rescue
  # logger should not raise an exception. This rescue prevents unexpected behaviour.
end

#trace_backtrace(backtrace = $!.backtrace) ⇒ Object



186
187
188
# File 'lib/fluent/log.rb', line 186

def trace_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_TRACE)
end

#warn(*args, &block) ⇒ Object Also known as: WARN



231
232
233
234
235
236
237
# File 'lib/fluent/log.rb', line 231

def warn(*args, &block)
  return if @level > LEVEL_WARN
  args << block.call if block
  time, msg = event(:warn, args)
  puts [@color_warn, caller_line(time, @depth_offset, LEVEL_WARN), msg, @color_reset].join
rescue
end

#warn_backtrace(backtrace = $!.backtrace) ⇒ Object



240
241
242
# File 'lib/fluent/log.rb', line 240

def warn_backtrace(backtrace=$!.backtrace)
  dump_stacktrace(backtrace, LEVEL_WARN)
end

#write(data) ⇒ Object



289
290
291
# File 'lib/fluent/log.rb', line 289

def write(data)
  @out.write(data)
end