All Projects → navinpeiris → Logster

navinpeiris / Logster

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

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Logster

Absinthe plug
Plug support for Absinthe, the GraphQL toolkit for Elixir
Stars: ✭ 209 (+22.22%)
Mutual labels:  phoenix, plug
guardian trackable
A Guardian hook to track user sign ins.
Stars: ✭ 25 (-85.38%)
Mutual labels:  phoenix, plug
alternate
Plug and Phoenix helpers to localize your web app via the URL
Stars: ✭ 26 (-84.8%)
Mutual labels:  phoenix, plug
Appsignal Elixir
🟪 AppSignal for Elixir package
Stars: ✭ 176 (+2.92%)
Mutual labels:  phoenix, plug
Terraform
A simple plug for incrementally transforming an API into Phoenix. Check out the blog post:
Stars: ✭ 379 (+121.64%)
Mutual labels:  phoenix, plug
phoenix-client-ssl
Set of Plugs / Lib to help with SSL Client Auth.
Stars: ✭ 18 (-89.47%)
Mutual labels:  phoenix, plug
plug rest
REST behaviour and Plug router for hypermedia web applications in Elixir
Stars: ✭ 52 (-69.59%)
Mutual labels:  phoenix, plug
accent
Dynamically convert the case of your JSON API keys
Stars: ✭ 27 (-84.21%)
Mutual labels:  phoenix, plug
Guardian
Elixir Authentication
Stars: ✭ 3,150 (+1742.11%)
Mutual labels:  phoenix, plug
plug rails cookie session store
Rails compatible Plug session store
Stars: ✭ 93 (-45.61%)
Mutual labels:  phoenix, plug
Authex
Authex is an opinionated JWT authentication and authorization library for Elixir.
Stars: ✭ 73 (-57.31%)
Mutual labels:  phoenix, plug
Liberator
An Elixir library for building RESTful applications.
Stars: ✭ 28 (-83.63%)
Mutual labels:  phoenix, plug
Plug logger json
Elixir Plug that formats http request logs as json
Stars: ✭ 125 (-26.9%)
Mutual labels:  plug, logging
Tune
A streamlined Spotify client and browser with a focus on performance and integrations.
Stars: ✭ 166 (-2.92%)
Mutual labels:  phoenix
Mongotail
Command line tool to log all MongoDB queries in a "tail"able way
Stars: ✭ 169 (-1.17%)
Mutual labels:  logging
Logging
Microsoft Extension Logging implementation for Blazor
Stars: ✭ 165 (-3.51%)
Mutual labels:  logging
Log Derive
A procedural macro for auto logging output of functions
Stars: ✭ 165 (-3.51%)
Mutual labels:  logging
Logstash
Logstash - transport and process your logs, events, or other data
Stars: ✭ 12,543 (+7235.09%)
Mutual labels:  logging
Logging
Powershell Logging Module
Stars: ✭ 167 (-2.34%)
Mutual labels:  logging
Inquisitor
Composable query builder for Ecto
Stars: ✭ 162 (-5.26%)
Mutual labels:  phoenix

Logster

Build Status Hex version Hex downloads 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, "~> 1.0"}]
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.

Metadata

Custom metadata can be added to logs using Logger.metadata and configuring your logger backend:

# add metadata for all future logs from this process
Logger.metadata(%{user_id: "123", foo: "bar"})

# example for configuring console backend to include metadata in logs.
# see https://hexdocs.pm/logger/Logger.html#module-console-backend documentation for more
# config.exs
config :logger, :console, metadata: [:user_id, :foo]

The easiest way to do this app wide is to introduce a new plug which you can include in your phoenix router pipeline.

For example:

defmodule HelloPhoenix.SetLoggerMetadata do
  def init(opts), do: opts

  def call(conn, _opts) do
    Logger.metadata user_id: get_user_id(conn),
                    remote_ip: format_ip(conn)
    conn
  end

  defp format_ip(%{remote_ip: remote_ip}) when remote_ip != nil, do: :inet_parse.ntoa(remote_ip)
  defp format_ip(_), do: nil

  defp get_user_id(%{assigns: %{current_user: %{id: id}}}), do: id
  defp get_user_id(_), do: nil
end

And then add this plug to the relevant pipelines in the router:

pipeline :browser do
  plug :fetch_session
  plug :fetch_flash
  plug :put_secure_browser_headers
  # ...
  plug HelloPhoenix.SetLoggerMetadata
  # ...
end

Renaming default fields

You can rename the default keys passing a map like %{key: :new_key}:

plug Logster.Plugs.Logger, renames: %{duration: :response_time, params: :parameters}

It will log the following:

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

Excluding fields

You can exclude fields with :excludes:

plug Logster.Plugs.Logger, excludes: [:params, :status, :state]

It will log the following:

[info] method=GET path=/articles/some-article format=html controller=HelloPhoenix.ArticleController action=show duration=0.402

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.

Development

Use the following mix task before pushing commits to run the same checks that are run in CI:

mix ci

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].