# LoggerJSON

[![Deps Status](]( [![ Downloads](]( [![Latest Version](]( [![License](]( [![Build Status](]( [![Coverage Status](]( [![Ebert](](

JSON console back-end for Elixir Logger.

It can be used as drop-in replacement for default `:console` Logger back-end in cases where you use
use Google Cloud Logger or other JSON-based log collectors.

## Motivation

[We]( deploy our applications as dockerized containers in Google Container Engine (Kubernetes cluster), in this case all your logs will go to `stdout` and log solution on top of Kubernetes should collect and persist it elsewhere.

In GKE it is persisted in Google Cloud Logger, but traditional single Logger output may contain newlines for a single log line, and GCL counts each new line as separate log entry, this making it hard to search over it.

This backed makes sure that there is only one line per log record and adds additional integration niceness, like [LogLine]( format support.

After adding this back-end you may also be interested in [redirecting otp and sasl reports to Logger]( (see "Error Logger configuration" section).

## Log Format

By default, output JSON is compatible with
[Google Cloud Logger format]( with
additional properties in `serviceLocation` and `metadata` objects:


You can change this structure by implementing `LoggerJSON.Formatter` behaviour and passing module
name to `:formatter` config option. Example module can be found in `LoggerJSON.Formatters.GoogleCloudLogger`.

  config :logger_json, :backend,
    formatter: MyFormatterImplementation

## Installation

It's [available on Hex](, the package can be installed as:

  1. Add `:logger_json` and `:jason` to your list of dependencies in `mix.exs`:

    def deps do
      [{:jason, "~> 1.0"},
       {:logger_json, "~> 1.0.1"}]

  2. Ensure `logger_json` and `:jason` is started before your application:

    def application do
      [extra_applications: [:jason, :logger_json]]

  3. Set configuration in your `config/config.exs`:

    config :logger_json, :backend,
      json_encoder: Jason,
      metadata: :all

  Some integrations (for eg. Plug) uses `metadata` to log request
  and response parameters. You can reduce log size by replacing `:all`
  (which means log all) with a list of the ones that you actually need.

  4. Replace default Logger `:console` back-end with `LoggerJSON`:

    config :logger,
      backends: [LoggerJSON]

  5. Optionally. Log requests and responses by replacing a `Plug.Logger` in your endpoint with a:

    plug LoggerJSON.Plug

  6. Optionally. Log Ecto queries via Plug:

    config :my_app, MyApp.Repo,
      adapter: Ecto.Adapters.Postgres,
      loggers: [{LoggerJSON.Ecto, :log, [:info]}]

  `Jason` can be replaced by any other compatible encoder, see "Encoders support" section.

## Dynamic configuration

For dynamically configuring the endpoint, such as loading data
from environment variables or configuration files, LoggerJSON provides
an `:on_init` option that allows developers to set a module, function
and list of arguments that is invoked when the endpoint starts.

    config :logger_json, :backend,
      on_init: {YourApp.Logger, :load_from_system_env, []}

## Encoders support

You can replace default Jason encoder with other module that supports `encode!/1` function. This can be even used
as custom formatter callback.

Popular Jason alternatives:

 * [poison](;
 * [exjsx](;
 * [elixir-json](;
 * [jiffy](

## Documentation

The docs can be found at [](

## Thanks

Many source code has been taken from original Elixir Logger `:console` back-end source code, so I want to thank all it's authors and contributors.

Part of `LoggerJSON.Plug` module have origins from `plug_logger_json` by @bleacherreport,
originally licensed under Apache License 2.0. Part of `LoggerJSON.PlugTest` are from Elixir's Plug licensed under Apache 2.