gelfx v0.1.2 Gelfx View Source

A logger backend for Elixir applications using Logger and Graylog based on GELF (Graylog extended logging format).

Usage

Add Gelfx to your application by adding {:gelfx, "~> 0.1.0"} to your list of dependencies in mix.exs:

def deps do
  [
    {:gelfx, "~> 0.1.0"}
  ]
end

And adding it to your :logger configuration in config.exs:

config :logger,
  backends: [
    :console,
    Gelfx
  ]

Since GELF relies on json to encode the payload Gelfx will need a JSON library. By default Gelfx will use Jason which needs to be added to your deps in mix.exs:

{:jason, "~> 1.0"}

Options

Besides :level, :format and :metadata, which are advised by Logger Gelfx supports:

  • :protocol - either :tcp or :udp, :http is not jet supported, defaults to :udp
  • :format - defaults to "$message"
  • :host - hostname of the server running the GELF endpoint
  • :hostname - used as source field in the GELF message, defaults to the hostname returned by :inet.gethostname()
  • :json_library - json library to use, has to implement a encode/1 which returns a {:ok, json} tuple in case of success
  • :port - port on which the graylog server runs the respective GELF input
  • :compression - either :gzip or :zlib can be set and will be used for package compression when UDP is used as protocol

Message Format

The GELF message format version implemented by this library is 1.1 docs.

Messages can include a short_message and a full_message, Gelfx will use the first line of each log message for the short_message and will place the whole message in the full_message field.

Metadata will be included in the message using the additional field syntax. The Keys of the metadata entries have to match ^\_?[\w\.\-]*$ with keys missing an leading underscore are automatically prepended with one. Key collisions are NOT prevented by Gelfx, additionally the keys id and _id are automatically omitted due to the GELF spec.

Levels

Graylog relies on the syslog definitions for logging levels. Gelfx maps the Elixir levels as follows:

ElixirSysylogGELF - Selector
Emergency0
Alert1
Critical2
:errorError3
:warnWarning4
Notice5
:infoInformational6
:debugDebug7

Link to this section Summary

Functions

Encodes the given LogEntry using the configured json library

Callback implementation for c::gen_event.handle_call/2

Callback implementation for c::gen_event.handle_event/2

Callback implementation for c::gen_event.handle_info/2

Callback implementation for c::gen_event.init/1

Spawns a :gen_udp / :gen_tcp connection based on the configuration

Sends the given payload over the connection

Callback implementation for c::gen_event.terminate/2

Link to this section Functions

Link to this function

encode(log_entry, state) View Source

Encodes the given LogEntry using the configured json library

Link to this function

handle_call(arg1, state) View Source

Callback implementation for c::gen_event.handle_call/2.

Link to this function

handle_event(event, state) View Source

Callback implementation for c::gen_event.handle_event/2.

Link to this function

handle_info(arg1, state) View Source

Callback implementation for c::gen_event.handle_info/2.

Callback implementation for c::gen_event.init/1.

Spawns a :gen_udp / :gen_tcp connection based on the configuration

Link to this function

spawn_conn(protocol, host, port) View Source

Sends the given payload over the connection.

In case an TCP connection is used the 0x00 delimiter required by gelf is added.

Should the used connection use UDP the payload is compressed using the configured compression, in case the given payload exceeds the chunk threshold it is chunked.

Link to this function

submit(payload, conn, comp) View Source

Callback implementation for c::gen_event.terminate/2.