Logging to Papertrail from Symfony Applications

PMG uses Papertrail to aggregate logs and we’ve found it to be really solid over the last four years.

Example Monolog Configuration

Here’s a quick example of configuration for Monolog Bundle to get logs to papertrail. This uses Monolog’s remote syslog handler to do its work.

  # from a log desintation
  papertrail_host: CHANGEME
  # Same story, from the log desintaiton, see below
  papertrail_port: 1234

  # get {field} replacements in messages
    class: Monolog\Processor\PsrLogMessageProcessor:
      - { name: monolog.processor }
  # slightly less noisy log output, we'll use this below
    class: Monolog\Formatter\LineFormatter
      - "[%%level_name%% - request(%%extra.request_id%%)] %%message%%"

  # ...
      type: syslogudp
      host: "%papertrail_host%"
      port: "%papertrail_port%"
      level: error # whatever 
      formatter: app.monolog.syslog_formatter

Couple key points around the services here:

  • PsrLogMessageProcessor is present so PSR-3 placeholders like {this} get handled correctly in log messages.
  • The custom formatter for the syslog handler reduces some log message noise (no context array is in the final message). The assumption here is that log context was used only for placeholders.

On Log Desintations

A log destination is what Papertrail calls a host + port listener that can receive remote syslog messages. Papertrail sets up a single log destination per account by default.

One listener is okay for something simple, but it’s well worth it to create multiple log destinations that dump systems in certain groups as they come online. This is especially helpful with immutable infrastructure where systems are not unique and hard to managed by name.

In the settings area click on log desintations then Create Log Destination.

The crucial setting here are highlighted below. The group setting assigns systems to a new group for easy browsing.

The monolog configuration above requires that UDP be enabled.

Once the listener is created, the UI will show a host + port pair that can be used in the monolog configuration above.

On Remote Logging

Remote logging is a game changer. It allows the diagnosis of errors without ever having to touch the server. That’s huge if immutable infrastructure is in play or no SSH access is granted to get to the logs. Papertrail is a nice balance of simplicity and power. It’s simple get started and free for small applications.