Class: Tracee::Logger
- Includes:
- Benchmarkable
- Defined in:
- lib/tracee/logger.rb
Constant Summary collapse
- LEVELS =
[ 'DEBUG', # Low-level information for developers 'INFO', # Generic (useful) information about system operation 'WARN', ## A warning 'ERROR', # A handleable error condition 'FATAL', # An unhandleable error that results in a program crash 'UNKNOWN' # An unknown message that should always be logged ].freeze
- LEVEL_NAMES =
LEVELS.map(&:downcase).freeze
Instance Attribute Summary collapse
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#level ⇒ Object
(also: #log_level)
Returns the value of attribute level.
-
#preprocessors ⇒ Object
readonly
Returns the value of attribute preprocessors.
-
#streams ⇒ Object
readonly
Returns the value of attribute streams.
Instance Method Summary collapse
- #add_preprocessor(callable_or_symbol = nil, *preprocessor_params) ⇒ Object
- #add_stream(target_or_stream) ⇒ Object
- #default? ⇒ Boolean
-
#initialize(stream: $stdout, streams: nil, formatter: {:default => :plain}, preprocessors: [], level: :info, default: false) ⇒ Logger
constructor
A new instance of Logger.
- #local_level ⇒ Object
- #local_level=(level) ⇒ Object
- #set_formatter(callable_or_symbol = nil, *formatter_params) ⇒ Object (also: #formatter=)
- #should_process_caller? ⇒ Boolean
- #silence(temporary_level = :error) ⇒ Object
- #write(msg, progname, level, level_int, caller_slice = []) ⇒ Object
Methods included from Benchmarkable
Constructor Details
#initialize(stream: $stdout, streams: nil, formatter: {:default => :plain}, preprocessors: [], level: :info, default: false) ⇒ Logger
Returns a new instance of Logger.
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 |
# File 'lib/tracee/logger.rb', line 20 def initialize(stream: $stdout, streams: nil, formatter: {:default => :plain}, preprocessors: [], level: :info, default: false) @streams = [] streams ||= [stream] streams.each {|item| add_stream item} if formatter.is_a? Hash # `formatter=' can't accept *array set_formatter *formatter.to_a.flatten else self.formatter = formatter end @preprocessors = [] preprocessors.each {|item| if item.is_a? Hash add_preprocessor *item.to_a.flatten else add_preprocessor item end } self.level = level read_log_level_from_env if default if Tracee.default_logger warn "Overwriting default logger #{Tracee.default_logger}\nwith the new one: #{self}" end Tracee.default_logger = self end end |
Instance Attribute Details
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
17 18 19 |
# File 'lib/tracee/logger.rb', line 17 def formatter @formatter end |
#level ⇒ Object Also known as: log_level
Returns the value of attribute level.
17 18 19 |
# File 'lib/tracee/logger.rb', line 17 def level @level end |
#preprocessors ⇒ Object (readonly)
Returns the value of attribute preprocessors.
17 18 19 |
# File 'lib/tracee/logger.rb', line 17 def preprocessors @preprocessors end |
#streams ⇒ Object (readonly)
Returns the value of attribute streams.
17 18 19 |
# File 'lib/tracee/logger.rb', line 17 def streams @streams end |
Instance Method Details
#add_preprocessor(callable_or_symbol = nil, *preprocessor_params) ⇒ Object
53 54 55 56 57 58 59 60 61 |
# File 'lib/tracee/logger.rb', line 53 def add_preprocessor(callable_or_symbol=nil, *preprocessor_params) if callable_or_symbol.is_a? Symbol @preprocessors << Tracee::Preprocessors.const_get(callable_or_symbol.to_s.camelize).new(*preprocessor_params) elsif callable_or_symbol.respond_to? :call @preprocessors << callable_or_symbol else raise TypeError, 'A preprocessor must respond to #call' end end |
#add_stream(target_or_stream) ⇒ Object
78 79 80 81 82 83 84 |
# File 'lib/tracee/logger.rb', line 78 def add_stream(target_or_stream) if target_or_stream.is_a? Tracee::Stream @streams << target_or_stream else @streams << Stream.new(target_or_stream) end end |
#default? ⇒ Boolean
185 186 187 |
# File 'lib/tracee/logger.rb', line 185 def default? Tracee.default_logger.eql? self end |
#local_level ⇒ Object
94 95 96 |
# File 'lib/tracee/logger.rb', line 94 def local_level Thread.current["tracee_#{__id__}_level"] ||= @level end |
#local_level=(level) ⇒ Object
90 91 92 |
# File 'lib/tracee/logger.rb', line 90 def local_level=(level) Thread.current["tracee_#{__id__}_level"] = norm_level(level) end |
#set_formatter(callable_or_symbol = nil, *formatter_params) ⇒ Object Also known as: formatter=
63 64 65 66 67 68 69 70 71 |
# File 'lib/tracee/logger.rb', line 63 def set_formatter(callable_or_symbol=nil, *formatter_params) if callable_or_symbol.is_a? Symbol @formatter = Tracee::Preprocessors.const_get(callable_or_symbol.to_s.camelize.sub('Default', 'Formatter')).new(*formatter_params) elsif callable_or_symbol.respond_to? :call @formatters = callable_or_symbol else raise TypeError, 'A formatter must respond to #call' end end |
#should_process_caller? ⇒ Boolean
74 75 76 |
# File 'lib/tracee/logger.rb', line 74 def should_process_caller? formatter.respond_to? :should_process_caller? and formatter.should_process_caller? end |
#silence(temporary_level = :error) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/tracee/logger.rb', line 174 def silence(temporary_level=:error) begin old_local_level = local_level self.local_level = temporary_level yield self ensure self.local_level = old_local_level end end |
#write(msg, progname, level, level_int, caller_slice = []) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/tracee/logger.rb', line 102 def write(msg, progname, level, level_int, caller_slice=[]) now = DateTime.now catch :halt do @preprocessors.each do |preprocessor| msg = preprocessor.(level, now, progname, msg, caller_slice) end msg = @formatter.(level, now, progname, msg, caller_slice) @streams.each do |stream| stream.write msg, level_int, log_level end end nil end |