ExSyslog

ExSyslog is custom backend for Elixir Logger that logs to syslog by wrapping erlang-syslog.

Requirements

Features

Installation

Add :exsyslog and :syslog as a dependency in your mix.exs file


defp deps do
  [
    {:exsyslog, "~> 0.1.0"}
  ]
end

Configuration

ExSyslog is a Logger custom backend, as such, it relies on :logger application.

On your config.exs file tell Logger that it should add ExSyslog backend

config :logger,
  backends: [
            {ExSyslog, :exsyslog_error},
            {ExSyslog, :exsyslog_debug}
            {ExSyslog, :exsyslog_json}
            ]

With the configuration above, logger application will add three ExSyslog backend with the name {ExSyslog, :exsyslog_error}, {ExSyslog, :exsyslog_debug} and {ExSyslog, :exsyslog_json}.

You might notice that instead of just passing the Module name, we’re passing a tuple with {Module name, backend configuration name}. This allow us to have multiple backends with different configuration. Let’s configure the backends:

config :logger, :exsyslog_error,
  level: :error,
  format: "$date $time [$level] $levelpad$node $metadata $message",
  metadata: [:module, :line, :function],
  ident: "MyApplication",
  facility: :local0,
  option: [:pid, :cons]

config :logger, :exsyslog_debug,
  level: :debug,
  format: "$date $time [$level] $message",
  ident: "MyApplication",
  facility: :local1,
  option: [:pid, :perror]

config :logger, :exsyslog_json,
  level: :debug,
  format: "$message",
  formatter: ExSyslog.JsonFormatter,
  metadata: [:module, :line, :function],
  ident: "MyApplication",
  facility: :local1,
  option: :pid

Backend configuration properties

Custom Formatters

ExSyslog by default uses Logger.Formatter. However, it comes with a JSON formatter that formats a given log entry to a JSON string. NOTE: ExSyslog.JsonFormatter can be use as an example if one wants to build his own formatter.

To build a custom formatter the formatter needs to implement the following functions:

compile(str) Compiles a format string

compile(binary | nil) :: [Logger.Formatter.pattern | binary]
compile({atom, atom}) :: {atom, atom}

format(format, level, msg, timestamp, metadata, config_metadata) Takes a compiled format and transforms it on a string that will be pass to syslog

format({atom, atom} | [Logger.Formatter.pattern | binary], Logger.level, Logger.message, Logger.Formatter.time, Keyword.t, [atom]) :: IO.chardata

To add the custom formatter you will need to set the formatter property on the configuration as exemplified above with ExSyslog.JsonFormatter

Try it

In another shell:

$ tail -f /var/log/syslog

(Mac users)

$ tail -f /var/log/system.log

NOTE Mac has a funny syslog. Your info logs might not show up. You’ll need to configure your Mac syslog.

Erlang/OTP 18 [erts-7.0.1] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Interactive Elixir (1.0.5) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> require Logger
nil
iex(2)> Logger.error "Hello syslog"
:ok

Authors

License

exsyslog is copyright (c) 2015 22cans Ltd.

The source code is released under the MIT License.

Check LICENSE for more information.