Class: SemanticLogger::Appender::NewRelicLogs
- Inherits:
-
Subscriber
- Object
- Base
- Subscriber
- SemanticLogger::Appender::NewRelicLogs
- Defined in:
- lib/semantic_logger/appender/new_relic_logs.rb
Constant Summary collapse
- CAPTURE_CONTEXT =
lambda do |log| = ::NewRelic::Agent. log.capture(:new_relic_metadata, ) unless .empty? end
Constants inherited from Subscriber
Instance Attribute Summary
Attributes inherited from Subscriber
#application, #environment, #formatter, #host, #logger, #metrics
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(formatter: nil, **args, &block) ⇒ NewRelicLogs
constructor
Create Appender.
-
#log(log) ⇒ Object
Send the log entry to the New Relic agent, which forwards it to New Relic Logs.
-
#message_error?(_exception) ⇒ Boolean
Every error that #log raises is one that writing the entry again cannot fix, so an asynchronous appender discards the entry, counts it and reports it, rather than backing off.
Methods inherited from Subscriber
#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #close, #console_output?, #console_stream, #fails_any_batch?, #flush, #inspect, #inspect_details, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?
Constructor Details
#initialize(formatter: nil, **args, &block) ⇒ NewRelicLogs
Create Appender
Parameters level: [:trace | :debug | :info | :warn | :error | :fatal] Override the log level for this appender. Default: none (:trace), so it writes every entry that a logger emits; each logger filters by its own 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: SemanticLogger::Formatters::NewRelicLogs
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.
49 50 51 52 53 54 55 56 57 |
# File 'lib/semantic_logger/appender/new_relic_logs.rb', line 49 def initialize(formatter: nil, **args, &block) super # Record NewRelic's "trace.id"/"entity.name"/"hostname"/etc, so we can include them # later in the formatted output. # These are thread-local, so need to be captured as soon as the log-message is created. # https://rubydoc.info/gems/newrelic_rpm/NewRelic/Agent#linking_metadata-instance_method SemanticLogger.on_log(CAPTURE_CONTEXT) end |
Class Method Details
.log_newrelic(json_message, level) ⇒ Object
87 88 89 |
# File 'lib/semantic_logger/appender/new_relic_logs.rb', line 87 def self.log_newrelic(, level) ::NewRelic::Agent.agent.log_event_aggregator.record(, level) end |
Instance Method Details
#log(log) ⇒ Object
Send the log entry to the New Relic agent, which forwards it to New Relic Logs.
Raises any error, as other appenders do, so that the asynchronous appender or the appenders that called it can count and report it (see #message_error?).
63 64 65 66 67 68 69 |
# File 'lib/semantic_logger/appender/new_relic_logs.rb', line 63 def log(log) = format_log(log) # Generate the structured log = Utils.to_json() # Convert the log to JSON, repairing any non UTF-8 data level = log.level.to_s.upcase # Determine the log level self.class.log_newrelic(, level) true end |
#message_error?(_exception) ⇒ Boolean
Every error that #log raises is one that writing the entry again cannot fix, so an asynchronous appender discards the entry, counts it and reports it, rather than backing off.
The agent's LogEventAggregator#record only adds the entry to the agent's own buffer in this process, and rescues any StandardError of its own; the agent sends that buffer to New Relic later, on its own thread. So nothing here waits on New Relic, and an error comes either from the entry, when it cannot be formatted or converted to JSON, or from the agent not being available, before newrelic_rpm has been initialized, when NewRelic::Agent.agent is nil, such as during Rails boot before its initializer runs. An agent that is initialized but not started, or disabled, is not nil, and drops the entry itself without raising. Backing off for the latter would hold up every entry, and stop the worker thread after async_max_retries, dropping every entry after it, even once the agent has started.
83 84 85 |
# File 'lib/semantic_logger/appender/new_relic_logs.rb', line 83 def (_exception) true end |