Class: RailsSemanticLogger::Appenders

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rails_semantic_logger/appenders.rb

Overview

Collects the appenders declared by the application via:

config.rails_semantic_logger.appenders do |appenders|
appenders.add(file_name: "log/#{Rails.env}.log", formatter: :json)
appenders.add_server(io: $stdout, formatter: :color)
appenders.add_console(io: $stderr, formatter: :color)
end

When at least one appender is declared this way, Rails Semantic Logger stops building its own default file appender (format, ap_options, filter, and add_file_appender no longer apply) and instead creates exactly the appenders declared here.

The methods name the context in which the appender is created; the destination and formatting are ordinary SemanticLogger.add_appender arguments:

#add         - always created, during Rails initialization.
#add_server  - created only when serving requests (`rails server`, Sidekiq
             in server mode). Defaults to `$stdout`.
#add_console - created only inside a `rails console` session. Defaults to
             `$stderr` so log output does not tangle with command results.

Because each call appends to its context, any appender works in any context, and a context can have several (e.g. a server-only stdout and file appender).

Constant Summary collapse

DESTINATIONS =

Destination keys understood by SemanticLogger.add_appender. When none is supplied to #add_server / #add_console, the context's default stream is used.

%i[io file_name appender logger metric].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAppenders

Returns a new instance of Appenders.



33
34
35
36
37
# File 'lib/rails_semantic_logger/appenders.rb', line 33

def initialize
  @definitions = []
  @server      = []
  @console     = []
end

Instance Attribute Details

#consoleObject (readonly)

The appenders declared via #add_server / #add_console, each as [Hash args, Proc|nil block].



71
72
73
# File 'lib/rails_semantic_logger/appenders.rb', line 71

def console
  @console
end

#serverObject (readonly)

The appenders declared via #add_server / #add_console, each as [Hash args, Proc|nil block].



71
72
73
# File 'lib/rails_semantic_logger/appenders.rb', line 71

def server
  @server
end

Instance Method Details

#add(**args, &block) ⇒ Object

Declare an appender. Accepts the same arguments (and optional block) as SemanticLogger.add_appender. Returns self so calls can be chained.



41
42
43
44
# File 'lib/rails_semantic_logger/appenders.rb', line 41

def add(**args, &block)
  @definitions << [args, block]
  self
end

#add_console(**args, &block) ⇒ Object

Declare an appender that is only created when running inside a rails console session. Identical to #add_server except it defaults to $stderr.

Accepts the same arguments (and optional block) as SemanticLogger.add_appender.



64
65
66
67
# File 'lib/rails_semantic_logger/appenders.rb', line 64

def add_console(**args, &block)
  @console << [defaults(args, io: $stderr), block]
  self
end

#add_server(**args, &block) ⇒ Object

Declare an appender that is only created when the application is serving requests: rails server, Sidekiq in server mode, or when the app calls RailsSemanticLogger.add_server_appenders from its own server's boot hook. It is never created during a non-serving boot (rake tasks, runners, generators), so it only appears where it is useful.

Accepts the same arguments (and optional block) as SemanticLogger.add_appender. When no destination is given it defaults to $stdout; the formatter defaults to :color.



55
56
57
58
# File 'lib/rails_semantic_logger/appenders.rb', line 55

def add_server(**args, &block)
  @server << [defaults(args, io: $stdout), block]
  self
end

#any?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rails_semantic_logger/appenders.rb', line 78

def any?
  @definitions.any? || @server.any? || @console.any?
end

#eachObject

Yields each #add declaration as [Hash args, Proc|nil block].



74
75
76
# File 'lib/rails_semantic_logger/appenders.rb', line 74

def each(&)
  @definitions.each(&)
end