Infrastructure agent logging behavior

New Relic's infrastructure agent gathers its own data as well as integrations's logs and consolidates them in a single source. By default, logs appear in standard-output and are added to a log file. To disable logs in standard output, see the agent's config options.

Logging severity levels

Infrastructure uses a subset of the standard Syslog severity levels:

  • ERROR: Error conditions met
  • WARN: Warning conditions met
  • INFO: Informational messages
  • DEBUG: Contains debug-level messages (useful when troubleshooting)

DEBUG level is only shown when the verbose mode is enabled.

Log formatting

For infrastructure agent v1.4.9 or higher, log messages are inlined with context values. This offers better grouping and filtering; for example:

containerized agent found in container
     containerID: VALUE

By default, Infrastructure logs are formatted as text:

  • In foreground mode, log output is colored, without a timestamp:

    DEBUG Sending deltas divided in blocks component=PatchSender mentityKey=ohaimaci mnumberOfBlocks=1
  • In background mode, logs are timestamped output, used when running as a service or dumping logs to a file:

    time="2019-07-12T09:54:15+02:00" level=info msg="Agent service manager shutdown completed successfully." component=AgentService service=newrelic-infra

Alternatively, logs can be formatted as a JSON file:

{"context":{},"level":"info","msg":"upstart_interval_sec: 0","timestamp":"2019-07-11T18:24:03+02:00"} 
{"context":{},"level":"info","msg":"plugin_dir: ","timestamp":"2019-07-11T18:24:03+02:00"}

To change the log format, see the agent configuration settings.

Log rotation

The infrastructure agent does not provide any native log rotation or compression mechanism. Instead, we encourage you to use consolidated log rotation tools, such as the Linux logrotate tool, which is usually installed by default in most Linux distributions.

Logrotate can be configured as an entry in /etc/logrotate.conf, or as a file in the /etc/logrotate.d directory.

Logrotate config file sample

A sample logrotate config file looks like this:

/var/log/newrelic-infra/newrelic-infra.log { 
     copytruncate 
     compress 
     daily 
     dateext 
     maxage 7 
}

Where:

  • /var/log/newrelic-infra/newrelic-infra.log: The Infrastructure agent log file. It must match the log_file configuration parameter in the /etc/newrelic-infra.yml file.
  • copytruncate: Indicates that the log file is truncated but not deleted when it is rotated. This configuration option is mandatory, otherwise the log file will be deleted and won’t be recreated.
  • compress: Compresses (usually in Gzip format) the rotated log files.
  • daily: The agent rotates logs daily.
  • dateext: Appends a date (by default, in the format YYYYMMDD) to the rotated log file (e.g. newrelic-infra.log-20190708.gz)
  • maxage 7: Makes logrotate remove rotated files after 7 days.

For a complete description of the logrotate configuration options, see the Linux Logrotate documentation.

Since logrotate is usually executed automatically as a cron job, verify that there is a logrotate entry in cron (for example, /etc/cron.daily/logrotate) similar to:

#!/bin/sh 

/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf EXITVALUE=$? 
if [ $EXITVALUE != 0 ]; then 
     /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" 
fi 
exit 0

Smart verbose mode

For infrastructure agent versions 1.9.0 or higher, you can enable smart verbose mode for logs.

Smart verbose mode prevents debug messages being logged until an error message is logged. Once an error has been logged, the cached debug messages are logged, but only the most recent number of configured debug messages. For example, if you have a configured limit of 10, after an error is logged, only the 10 most recent debug messages are logged, and older logs are discarded.

For more information on how to enable smart verbose mode and the debug message limit, see Infrastructure configuration settings.

Logging before Infrastructure agent v1.4.9

Here is a comparison of functionality for Infrastructure agent versions before and after v1.4.9:

Agent v1.4.9 and higher Before v1.4.9
Foreground mode logged. The agent couldn't log some entries in foreground mode because the logging service wasn't able to write data until the agent was completely configured.
Logs in text and JSON formats. Logs in text only.
Logs displayed as inline text. Logs displayed as static literals in a single, decontextualized line.

Integration log management

Integrations write JSON payloads into STDOUT and plain-text (JSON structured in the future) logs into STDERR.

The infrastructure agent handles integration STDERR lines and forward this output into the agent one, usually the service log.

Agent handles each STDERR line as follows:

  • when agent runs in verbose mode: it just forwards the full STDERR line as a DEBUG agent log entry placing integration line contexts within the `msg` field.
  • otherwise: it parses the line against the expected format (see below) and only logs as agent ERROR level, entries produced by integrations with `fatal` or `error` severity levels. In this case fields are extracted and forwarded in structured manner (therefore if JSON output is enabled for the agent fields become queryable.

Integration STDERR expected format

A line is expected to be a list of key-value pairs separated by an equal character. Keys can contain any character, whereas values can have three different formats:

  1. string: <quote>any character including escaped quotes \"<quote>
  2. map: &{any character}
  3. word: any character except spaces

Internally agent used this regex to extract the fields:

([^\s]*?)=(".*?[^\\]"|&{.*?}|[^\s]*)

For instance, this line:

time="2015-03-26T01:27:38-04:00" level=error msg="Foo bar baz" foo=bar

Will generate a structured agent log line with these fields:

- "time": "2015-03-26T01:27:38-04:00"
- "level": "error"
- "msg": "Foo bar baz"
- "foo": "bar"

For more help

If you need more help, check out these support and learning resources: