/logster

Easily parsable single line, plain text and JSON logger for Plug and Phoenix applications

Primary LanguageElixirMIT LicenseMIT

Logster

Build Status Hex version Hex downloads Deps Status License

An easy to parse, one line logger for Elixir Plug.Conn and Phoenix, inspired by lograge.

With the default Plug.Logger, the log output for a request looks like:

[info] GET /articles/some-article
[info] Sent 200 in 21ms

With Logster, we've got logging that's much easier to parse and search through, such as:

[info] method=GET path=/articles/some-article format=html controller=HelloPhoenix.ArticleController action=show params={"id":"some-article"} status=200 duration=0.402 state=set

This becomes handy specially when integrating with log management services such as Logentries or Papertrail.

Installation

First, add Logster to your mix.exs dependencies:

def deps do
  [{:logster, "~> 0.4"}]
end

and add it to your list of applications:

def application() do
  [applications: [:logster]]
end

Then, update your dependencies:

$ mix deps.get

Usage

To use with a Phoenix application, replace Plug.Logger in the projects endpoint.ex file with Logster.Plugs.Logger:

# plug Plug.Logger
plug Logster.Plugs.Logger

To use it in as a plug, just add plug Logster.Plugs.Logger into the relevant module.

Filtering parameters

By default, Logster filters parameters named password, and replaces the content with [FILTERED].

You can update the list of parameters that are filtered by adding the following to your configuration file:

config :logster, :filter_parameters, ["password", "secret", "token"]

HTTP headers support

By default, Logster won't parse and log HTTP headers.

But you can update the list of headers that should be parsed and logged. The logged headers will be added under headers. Both plain text and JSON formatters are supported.

config :logster, :allowed_headers, ["my-header-one", "my-header-two"]

Changing the log level for a specific controller/action

To change the Logster log level for a specific controller and/or action, you use the Logster.Plugs.ChangeLogLevel plug.

For example, to change the logging of all requests in a controller to debug, add the following to that controller:

plug Logster.Plugs.ChangeLogLevel, to: :debug

And to change it only for index and show actions:

plug Logster.Plugs.ChangeLogLevel, to: :debug when action in [:index, :show]

This is specially useful for cases such as when you want to lower the log level for a healthcheck endpoint that gets hit every few seconds.

Changing the formatter

Logster allows you to use a different formatter to get your log lines looking just how you want. It comes with two built-in formatters: Logster.StringFormatter and Logster.JSONFormatter

To use Logster.JSONFormatter, supply the formatter option when you use the Logster.Plugs.Logger plug:

plug Logster.Plugs.Logger, formatter: Logster.JSONFormatter

That means your log messages will be formatted thusly:

{"status":200,"state":"set","path":"hello","params":{},"method":"GET","format":"json","duration":20.647,"controller":"App.HelloController","action":"show"

Caution: There is no guarantee that what reaches your console will be valid JSON. The Elixir Logger module has its own formatting which may be appended to your message. See the Logger documentation for more information.

Adding custom metadata

Custom metadata can be added using Logger.metadata such as:

Logger.metadata(%{user_id: "123", foo: "bar"})

Writing your own formatter

To write your own formatter, all that is required is a module which defines a format/1 function, which accepts a keyword list and returns a string.

License

The MIT License

Copyright (c) 2016-present Navin Peiris

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.