Class: SemanticLogger::Appender::Wrapper

Inherits:
Subscriber
  • Object
show all
Defined in:
lib/semantic_logger/appender/wrapper.rb

Instance Attribute Summary collapse

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #metrics

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_by_default?, #console_output?, #console_stream, #default_formatter, #level, #should_log?

Constructor Details

#initialize(logger:, **args) ⇒ Wrapper

Forward all logging calls to the supplied logging instance.

Parameters logger: [Object] Instance of an existing logger conforming to the Ruby Logger methods.

level: [:trace | :debug | :info | :warn | :error | :fatal]
Override the log level for this appender.
Default: SemanticLogger.default_level

formatter: [Object|Proc]
An instance of a class that implements #call, or a Proc to be used to format
the output from this appender
Default: Use the built-in formatter (See: #call)

filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied.
regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true
      The Proc must return true or false.

Ruby Logger require 'logger' require 'semantic_logger'

ruby_logger = Logger.new($stdout)
SemanticLogger.add_appender(logger: ruby_logger)

logger =  SemanticLogger['test']
logger.info('Hello World', some: :payload)

Install the rails_semantic_logger gem to replace the Rails logger with Semantic Logger.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/semantic_logger/appender/wrapper.rb', line 41

def initialize(logger:, **args, &)
  @logger = logger

  # Check if the custom appender responds to all the log levels. For example Ruby ::Logger
  does_not_implement = LEVELS[1..].find { |i| !@logger.respond_to?(i) }
  if does_not_implement
    raise(ArgumentError,
          "Supplied logger does not implement:#{does_not_implement}. It must implement all of #{LEVELS[1..].inspect}")
  end

  super(**args, &)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



7
8
9
# File 'lib/semantic_logger/appender/wrapper.rb', line 7

def logger
  @logger
end

Instance Method Details

#closeObject

Close underlying log Waits for all queued log messages to be written to disk.



71
72
73
# File 'lib/semantic_logger/appender/wrapper.rb', line 71

def close
  @logger.close if @logger.respond_to?(:close)
end

#flushObject

Flush all pending logs to disk. Waits for all queued log messages to be written to disk.



65
66
67
# File 'lib/semantic_logger/appender/wrapper.rb', line 65

def flush
  @logger.flush if @logger.respond_to?(:flush)
end

#log(log) ⇒ Object

Pass log calls to the underlying Rails, log4j or Ruby logger trace entries are mapped to debug since :trace is not supported by the Ruby or Rails Loggers



57
58
59
60
61
# File 'lib/semantic_logger/appender/wrapper.rb', line 57

def log(log)
  level = log.level == :trace ? :debug : log.level
  @logger.send(level, formatter.call(log, self))
  true
end