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
-
#initialize(stream: $stdout, streams: nil, formatter: {:default => :plain}, preprocessors: [], level: :info) ⇒ 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) ⇒ 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 |
# File 'lib/tracee/logger.rb', line 20 def initialize(stream: $stdout, streams: nil, formatter: {:default => :plain}, preprocessors: [], level: :info) @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 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
46 47 48 49 50 51 52 53 54 |
# File 'lib/tracee/logger.rb', line 46 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
71 72 73 74 75 76 77 |
# File 'lib/tracee/logger.rb', line 71 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 |
#local_level ⇒ Object
87 88 89 |
# File 'lib/tracee/logger.rb', line 87 def local_level Thread.current["tracee_#{__id__}_level"] ||= @level end |
#local_level=(level) ⇒ Object
83 84 85 |
# File 'lib/tracee/logger.rb', line 83 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=
56 57 58 59 60 61 62 63 64 |
# File 'lib/tracee/logger.rb', line 56 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
67 68 69 |
# File 'lib/tracee/logger.rb', line 67 def should_process_caller? formatter.respond_to? :should_process_caller? and formatter.should_process_caller? end |
#silence(temporary_level = :error) ⇒ Object
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/tracee/logger.rb', line 167 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/tracee/logger.rb', line 95 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 |