Code Monkey home page Code Monkey logo

logger_exporter's Introduction

LoggerExporter

Export your logs to the service of your choice.

I created this library because I wanted to export logs to a different service in Heroku. There is no simple way to export your logs.

Supported exporters:

  • Loki

Implement your own exporter using LoggerExporter.Exporters.Exporter behaviour.

Supported formatters:

  • Basic

Implement your own formatter using LoggerExporter.Formatters.Formatter behaviour.

Plug Logger

A plug for logging request information. It will log the method, path, params, status and duration.

You can add it to your MyApp.Endpoint:

  # Log level defaults to :info
  plug LoggerExporter.Loggers.Plug

  # Dynamic log
  plug LoggerExporter.Loggers.Plug, log: {__MODULE__, :log_level, []}

  # Disables logging for routes like /health_check
  def log_level(%{path_info: ["health_check"]}), do: false
  def log_level(_), do: :info

Installation

Add logger_exporter to your list of dependencies in mix.exs:

def deps do
  [
    {:logger_exporter, "~> 0.3.0"}
  ]
end

Configuration

By default, the timestamp sent for each log to the external service is in utc: System.os_time(:nanosecond)

  • config :logger, LoggerExporter, :level. The logger level to report.
  • config :logger, LoggerExporter, :formatter. Allows the selection of a formatter implementation. Defaults to LoggerExporter.Formatters.BasicFormatter
  • config :logger, LoggerExporter, :metadata. Metadata to log. Defaults to []
  • config :logger, LoggerExporter, :exporter. Allows selection of a exporter implementation. Defaults to LoggerExporter.Exporters.LokiExporter
  • config :logger, LoggerExporter, :batch_every_ms. The time (in ms) between every batch request. Default value is 2000 (2 seconds)
  • config :logger, LoggerExporter, :host. The host of the service without the path. The path is inferred by the exporter. Required
  • config :logger, LoggerExporter, :app_name. The name of the app to use as label for Loki. Required if using LokiExporter
  • config :logger, LoggerExporter, :environment_name. The name of the environment to use as label for Loki. Required if using LokiExporter
  • config :logger, :send_to_http If set to false, the library will not make any actual API calls. This is useful for test or dev environments. Default value is true
  • config :logger, LoggerExporter, :http_auth. See below

HTPP Auth

Supported authentication methods:

  • Basic:

    config :logger, LoggerExporter,
      host: "https://logs-prod.grafana.net",
      http_auth: {:basic, System.fetch_env!("USER"), System.fetch_env!("PASSWORD")}

Usage in Phoenix

  1. Add the following to deps section of your mix.exs: {:logger_exporter, "~> 0.2.2"} and then mix deps.get

  2. Add LoggerExporter.Backend to your logger's backends configuration

    config :logger,
      backends: [:console, LoggerExporter.Backend]
    
  3. Add config related to the exporter and other fields. ie. for LokiExporter

    config :logger, LoggerExporter,
      host: "http://localhost:3100",
      app_name: "my_app",
      environment_name: Mix.env(),
      http_auth: {:basic, System.fetch_env!("LOKI_USER"), System.fetch_env!("LOKI_PASSWORD")},
      metadata: [:request_id]
  4. Start the LoggerExporter GenServer in the supervised children list. In application.ex add to the children list:

    children [
      ...
      LoggerExporter
    ]
  5. (Optional) Add custom Plug logger. In MyApp.Endpoint add the plug after Plug.Parsers. If you see duplicate logs, remove Plug.Telemetry from your endpoint.

    plug Plug.Parsers,
      ...
    
    plug LoggerExporter.Loggers.Plug

JSON Formatter

If you want to log in JSON format, you can use the formatter of another library: logger_json

You can configure it like this:

config :logger, LoggerExporter,
  formatter: LoggerJSON.Formatters.BasicLogger

And it will work out of the box :)

Telemetry

Telemetry integration for logging and error reporting.

There is a default logger for you to attach. It logs when the status is :error

In your application.ex

:ok = LoggerExporter.Telemetry.attach_default_logger()

HTTP Post batch events

LoggerExporter emits the following events for processing each batch (sending it through http)

  • [:logger_exporter, :batch, :start] -- starting to process the events
  • [:logger_exporter, :batch, :stop] -- after the events is processed
  • [:logger_exporter, :batch, :exception] -- after the events are processed

The following chart shows which metadata you can expect for each event:

event measures metadata
:start :system_time :events
:stop :duration :events, :status, :response
:exception :duration :events, :kind, :reason, :stacktrace

Metadata

  • :events - the list of LoggerExporter.Event processed
  • :status - either :ok or :error
  • :response - information of the response. Is a Finch.Response struct

logger_exporter's People

Contributors

rlopzc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.