Class: RailsSemanticLogger::Appenders
- Inherits:
-
Object
- Object
- RailsSemanticLogger::Appenders
- 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`, a rack
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
-
#console ⇒ Object
readonly
The appenders declared via #add_server / #add_console, each as [Hash args, Proc|nil block].
-
#server ⇒ Object
readonly
The appenders declared via #add_server / #add_console, each as [Hash args, Proc|nil block].
Instance Method Summary collapse
-
#add(**args, &block) ⇒ Object
Declare an appender.
-
#add_console(**args, &block) ⇒ Object
Declare an appender that is only created when running inside a
rails consolesession. -
#add_server(**args, &block) ⇒ Object
Declare an appender that is only created when the application is serving requests:
rails server, a rack server started directly (puma, etc.), or Sidekiq in server mode. - #any? ⇒ Boolean
-
#each ⇒ Object
Yields each #add declaration as [Hash args, Proc|nil block].
-
#initialize ⇒ Appenders
constructor
A new instance of Appenders.
Constructor Details
#initialize ⇒ Appenders
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
#console ⇒ Object (readonly)
The appenders declared via #add_server / #add_console, each as [Hash args, Proc|nil block].
70 71 72 |
# File 'lib/rails_semantic_logger/appenders.rb', line 70 def console @console end |
#server ⇒ Object (readonly)
The appenders declared via #add_server / #add_console, each as [Hash args, Proc|nil block].
70 71 72 |
# File 'lib/rails_semantic_logger/appenders.rb', line 70 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.
63 64 65 66 |
# File 'lib/rails_semantic_logger/appenders.rb', line 63 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, a rack server started directly (puma, etc.), or
Sidekiq in server mode. 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.
54 55 56 57 |
# File 'lib/rails_semantic_logger/appenders.rb', line 54 def add_server(**args, &block) @server << [defaults(args, io: $stdout), block] self end |
#any? ⇒ Boolean
77 78 79 |
# File 'lib/rails_semantic_logger/appenders.rb', line 77 def any? @definitions.any? || @server.any? || @console.any? end |
#each ⇒ Object
Yields each #add declaration as [Hash args, Proc|nil block].
73 74 75 |
# File 'lib/rails_semantic_logger/appenders.rb', line 73 def each(&) @definitions.each(&) end |