All Projects → bleacherreport → Plug_logger_json

bleacherreport / Plug_logger_json

Licence: apache-2.0
Elixir Plug that formats http request logs as json

Programming Languages

elixir
2628 projects

Projects that are alternatives of or similar to Plug logger json

Tslog
📝 tslog - Expressive TypeScript Logger for Node.js.
Stars: ✭ 321 (+156.8%)
Mutual labels:  json, logging, logger
Log Viewer
Web UI to viewing logs
Stars: ✭ 59 (-52.8%)
Mutual labels:  logging, logger
Bulk
👨‍💻 Bulk is a library for buffering the objects. Pipeline(Sink) receives the object and emits the object bulked.
Stars: ✭ 59 (-52.8%)
Mutual labels:  logging, logger
React Native Logs
Performance-aware simple logger for React-Native with namespaces, custom levels and custom transports (colored console, file writing, etc.)
Stars: ✭ 84 (-32.8%)
Mutual labels:  logging, logger
Heroku Logger
A dead simple logger, designed to be perfect for Heroku apps.
Stars: ✭ 57 (-54.4%)
Mutual labels:  logging, logger
Log
Structured logging package for Go.
Stars: ✭ 1,094 (+775.2%)
Mutual labels:  logging, logger
Loguru
Python logging made (stupidly) simple
Stars: ✭ 10,510 (+8308%)
Mutual labels:  logging, logger
Qtwebapp
QtWebApp is a HTTP server like Java servlets, written in C++ with the Qt framework.
Stars: ✭ 50 (-60%)
Mutual labels:  logging, logger
Logger json
JSON console backend for Elixir Logger.
Stars: ✭ 108 (-13.6%)
Mutual labels:  json, logging
Node Lambda Log
Basic logging mechanism for Node 6.10+ Lambda Functions
Stars: ✭ 115 (-8%)
Mutual labels:  logging, logger
Zoya
Truly highly composable logging utility
Stars: ✭ 116 (-7.2%)
Mutual labels:  json, logging
Ios Sdk
AppSpector is a debugging service for mobile apps
Stars: ✭ 56 (-55.2%)
Mutual labels:  logging, logger
Plog
Portable, simple and extensible C++ logging library
Stars: ✭ 1,061 (+748.8%)
Mutual labels:  logging, logger
Yii2 Psr Log Target
Yii 2.0 log target that is able to write messages to PSR-3 compatible logger
Stars: ✭ 58 (-53.6%)
Mutual labels:  logging, logger
Serverless Es Logs
A Serverless plugin to transport logs to ElasticSearch
Stars: ✭ 51 (-59.2%)
Mutual labels:  logging, logger
Android Filelogger
A general-purpose logging library with built-in support to save logs to file efficiently.
Stars: ✭ 70 (-44%)
Mutual labels:  logging, logger
Serverlog
A simple, practical and innovative Node.js log library that enables you to view logs in Chrome dev tools and browser Console.
Stars: ✭ 117 (-6.4%)
Mutual labels:  logging, logger
Loglevelnext
A modern logging library for Node.js that provides log level mapping to the console
Stars: ✭ 33 (-73.6%)
Mutual labels:  logging, logger
Browser Logger
A dead simple logger, designed to be perfect for the browser.
Stars: ✭ 44 (-64.8%)
Mutual labels:  logging, logger
Logsip
A simple, concise, colorful logger for Go
Stars: ✭ 94 (-24.8%)
Mutual labels:  logging, logger

PlugLoggerJson

Hex pm Build Status License

A comprehensive JSON logger Plug.

Dependencies

  • Plug
  • Poison

Elixir & Erlang Support

The support policy is to support the last 2 major versions of Erlang and the three last minor versions of Elixir.

Installation

  1. add plug_logger_json to your list of dependencies in mix.exs:

    def deps do
      [{:plug_logger_json, "~> 0.7.0"}]
    end
    
  2. ensure plug_logger_json is started before your application (Skip if using Elixir 1.4 or greater):

    def application do
      [applications: [:plug_logger_json]]
    end
    
  3. Replace Plug.Logger with either:

    • Plug.LoggerJSON, log: Logger.level,
    • Plug.LoggerJSON, log: Logger.level, extra_attributes_fn: &MyPlug.extra_attributes/1 in your plug pipeline (in endpoint.ex for Phoenix apps),

Recommended Setup

Configure plug_logger_json

Add to your config/config.exs or config/env_name.exs if you want to filter params or headers or suppress any logged keys:

config :plug_logger_json,
  filtered_keys: ["password", "authorization"],
  suppressed_keys: ["api_version", "log_type"]

Configure the logger (console)

In your config/config.exs or config/env_name.exs:

config :logger, :console,
  format: "$message\n",
  level: :info, # You may want to make this an env variable to change verbosity of the logs
  metadata: [:request_id]

Configure the logger (file)

Do the following:

  • update deps in mix.exs with the following:

    def deps do
     [{:logger_file_backend, "~> 0.0.10"}]
    end
    
  • add to your config/config.exs or config/env_name.exs:

    config :logger,
      format: "$message\n",
      backends: [{LoggerFileBackend, :log_file}, :console]
    
    config :logger, :log_file,
      format: "$message\n",
      level: :info,
      metadata: [:request_id],
      path: "log/my_pipeline.log"
    
  • ensure you are using Plug.Parsers (Phoenix adds this to endpoint.ex by default) to parse params as well as request body:

    plug Plug.Parsers,
      parsers: [:urlencoded, :multipart, :json],
      pass: ["*/*"],
      json_decoder: Poison
    

Error Logging

In router.ex of your Phoenix project or in your plug pipeline:

  • add require Logger,

  • add use Plug.ErrorHandler,

  • add the following two private functions:

    defp handle_errors(%Plug.Conn{status: 500} = conn, %{kind: kind, reason: reason, stack: stacktrace}) do
      Plug.LoggerJSON.log_error(kind, reason, stacktrace)
      send_resp(conn, 500, Poison.encode!(%{errors: %{detail: "Internal server error"}}))
    end
    
    defp handle_errors(_, _), do: nil
    

Extra Attributes

Additional data can be logged alongside the request by specifying a function to call which returns a map:

def extra_attributes(conn) do
  map = %{
    "user_id" => get_in(conn.assigns, [:user, :user_id]),
    "other_id" => get_in(conn.private, [:private_resource, :id]),
    "should_not_appear" => conn.private[:does_not_exist]
  }

  map
  |> Enum.filter(&(&1 !== nil))
  |> Enum.into(%{})
end

plug Plug.LoggerJSON,
  log: Logger.level(),
  extra_attributes_fn: &MyPlug.extra_attributes/1

In this example, the :user_id is retrieved from conn.assigns.user.user_id and added to the log if it exists. In the example, any values that are nil are filtered from the map. It is a requirement that the value is serialiazable as JSON by the Poison library, otherwise an error will be raised when attempting to encode the value.

Log Verbosity

LoggerJSON plug supports two levels of logging:

  • info / error will log:

    • api_version,
    • date_time,
    • duration,
    • log_type,
    • method,
    • path,
    • request_id,
    • status
  • warn / debug will log everything from info and:

    • client_ip,
    • client_version,
    • params / request_body.

The above are default. It is possible to override them by setting a include_debug_logging option to:

  • false – means the extra debug fields (client_ip, client_version, and params) WILL NOT get logged.
  • true – means the extra fields WILL get logged.
  • Not setting this option will keep the defaults above.

Example:

plug Plug.LoggerJSON,
  log: Logger.level,
  include_debug_logging: true

Contributing

Before submitting your pull request, please run:

  • mix credo --strict,
  • mix coveralls,
  • mix dialyzer,
  • update changelog.

Please squash your pull request's commits into a single commit with a message and detailed description explaining the commit.

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].