An Elixir Log Wrapper allows Maps and Strings to be passed to the Elixir Logger, along with that it will ouput to JSON.


License
MIT

Documentation

Stump

Build Status

Stump is an Elixir log wrapper that allows you to pass Maps/Structs into the built in Logger function, returning them in a JSON format and outputting to a file in Production mode. Providing you with the ability to write more descriptive log messages and send logs to services expecting logs in the json/map format. The library is not limited to maps, it can also take in strings and create JSON formatted log messages.

Please note, from Version 1.1 Stump will be using Jason as its JSON encoder. This will effect the ordering of logged items.

It is strongly advised that you use version 1.4 of Stump and above, version 1.3 could cause an error to be thrown when trying to encode log messages.

Installation

If available in Hex, the package can be installed by adding stump to your list of dependencies in mix.exs:

def deps do
  [
    {:stump, "~> 1.7.0"}
  ]
end

Documentation can be found at https://hexdocs.pm/stump/Stump.html.

Usage

Once the package has been installed into your project, the following usage is recommended:

  def foo do
    Stump.log(:error, %{message: "Error Logged"})
  end

Providing you with output in this format:

 {"message":"Error Logged","level":"error","datetime":"2019-03-06T12:18:24.179731Z"}
 :ok

You can also pass in strings if you would prefer not to use maps:

  Stump.log(:error, "Error Logged")
  {"message":"Error Logged","level":"error","datetime":"2019-03-06T12:21:52.661587Z"}

It can also be nicely used in conjuction with libraries such as HTTPoison

  def process(_, {:error, %HTTPoison.Error{reason: reason}}) do
    Stump.log(:error, %{message: "Failed to process HTTP request, reason: #{reason}", event: "HTTPoison.Error"})
    {:error, reason}
  end

Configuration

The default configuration for this will simply log to the console, if you would like to configure it you can simply edit your config.exs file. It is worth noting that you must keep the format as format: "$message\n" with whichever logging backend you choose to use otherwise you will get duplication of information.

If for example you would like to Log to a file the following configuration would be recommended:

  • First edit your mix.exs and add the LoggerFileBackend
  defp deps do
    [
      {:logger_file_backend, "~> 0.0.10"}
    ]
  end
  • Run mix deps.get

  • Edit your config.exs

config :logger,
  backends: [{LoggerFileBackend, :file}]

config :logger, :file,
  path: "/var/log/my_app/error.log",
  format: "$message\n",
  level: :debug