{"id":19204200,"url":"https://github.com/bleacherreport/plug_logger_json","last_synced_at":"2025-04-05T03:12:03.640Z","repository":{"id":52212330,"uuid":"60112431","full_name":"bleacherreport/plug_logger_json","owner":"bleacherreport","description":"Elixir Plug that formats http request logs as json","archived":false,"fork":false,"pushed_at":"2021-05-05T00:08:20.000Z","size":89,"stargazers_count":132,"open_issues_count":5,"forks_count":32,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-03-29T02:08:36.282Z","etag":null,"topics":["elixir","json","logger","logging","plug"],"latest_commit_sha":null,"homepage":"https://dev.bleacherreport.com/elixir-phoenix-centralized-http-logging-aa50efe3105b#.k00ri7og7","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bleacherreport.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-31T18:12:03.000Z","updated_at":"2023-10-14T17:35:20.000Z","dependencies_parsed_at":"2022-09-04T08:31:23.998Z","dependency_job_id":null,"html_url":"https://github.com/bleacherreport/plug_logger_json","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bleacherreport%2Fplug_logger_json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bleacherreport%2Fplug_logger_json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bleacherreport%2Fplug_logger_json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bleacherreport%2Fplug_logger_json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bleacherreport","download_url":"https://codeload.github.com/bleacherreport/plug_logger_json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247280272,"owners_count":20912967,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elixir","json","logger","logging","plug"],"created_at":"2024-11-09T13:06:36.569Z","updated_at":"2025-04-05T03:12:03.623Z","avatar_url":"https://github.com/bleacherreport.png","language":"Elixir","readme":"# PlugLoggerJson\n[![Hex pm](http://img.shields.io/hexpm/v/plug_logger_json.svg?style=flat)](https://hex.pm/packages/plug_logger_json)\n[![Build Status](https://travis-ci.org/bleacherreport/plug_logger_json.svg?branch=master)](https://travis-ci.org/bleacherreport/plug_logger_json)\n[![License](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://github.com/bleacherreport/plug_logger_json/blob/master/LICENSE)\n\nA comprehensive JSON logger Plug.\n\n## Dependencies\n\n* Plug\n* Poison\n\n## Elixir \u0026 Erlang Support\n\nThe support policy is to support the last 2 major versions of Erlang and the three last minor versions of Elixir.\n\n## Installation\n\n1. add plug_logger_json to your list of dependencies in `mix.exs`:\n\n   ```elixir\n   def deps do\n     [{:plug_logger_json, \"~\u003e 0.7.0\"}]\n   end\n   ```\n\n2. ensure plug_logger_json is started before your application (Skip if using Elixir 1.4 or greater):\n\n   ```elixir\n   def application do\n     [applications: [:plug_logger_json]]\n   end\n   ```\n\n3. Replace `Plug.Logger` with either:\n\n   * `Plug.LoggerJSON, log: Logger.level`,\n   * `Plug.LoggerJSON, log: Logger.level, extra_attributes_fn: \u0026MyPlug.extra_attributes/1` in your plug pipeline (in `endpoint.ex` for Phoenix apps),\n\n## Recommended Setup\n\n### Configure `plug_logger_json`\n\nAdd to your `config/config.exs` or `config/env_name.exs` if you want to filter params or headers or suppress any logged keys:\n\n```elixir\nconfig :plug_logger_json,\n  filtered_keys: [\"password\", \"authorization\"],\n  suppressed_keys: [\"api_version\", \"log_type\"]\n```\n\n### Configure the logger (console)\n\nIn your `config/config.exs` or `config/env_name.exs`:\n\n```elixir\nconfig :logger, :console,\n  format: \"$message\\n\",\n  level: :info, # You may want to make this an env variable to change verbosity of the logs\n  metadata: [:request_id]\n```\n\n### Configure the logger (file)\n\nDo the following:\n\n* update deps in `mix.exs` with the following:\n\n    ```elixir\n    def deps do\n     [{:logger_file_backend, \"~\u003e 0.0.10\"}]\n    end\n    ```\n\n* add to your `config/config.exs` or `config/env_name.exs`:\n\n    ```elixir\n    config :logger,\n      format: \"$message\\n\",\n      backends: [{LoggerFileBackend, :log_file}, :console]\n\n    config :logger, :log_file,\n      format: \"$message\\n\",\n      level: :info,\n      metadata: [:request_id],\n      path: \"log/my_pipeline.log\"\n    ```\n\n* ensure you are using `Plug.Parsers` (Phoenix adds this to `endpoint.ex` by default) to parse params as well as request body:\n\n    ```elixir\n    plug Plug.Parsers,\n      parsers: [:urlencoded, :multipart, :json],\n      pass: [\"*/*\"],\n      json_decoder: Poison\n    ```\n\n## Error Logging\n\nIn `router.ex` of your Phoenix project or in your plug pipeline:\n\n* add `require Logger`,\n* add `use Plug.ErrorHandler`,\n* add the following two private functions:\n\n    ```elixir\n    defp handle_errors(%Plug.Conn{status: 500} = conn, %{kind: kind, reason: reason, stack: stacktrace}) do\n      Plug.LoggerJSON.log_error(kind, reason, stacktrace)\n      send_resp(conn, 500, Poison.encode!(%{errors: %{detail: \"Internal server error\"}}))\n    end\n\n    defp handle_errors(_, _), do: nil\n    ```\n\n## Extra Attributes\n\nAdditional data can be logged alongside the request by specifying a function to call which returns a map:\n\n```elixir\ndef extra_attributes(conn) do\n  map = %{\n    \"user_id\" =\u003e get_in(conn.assigns, [:user, :user_id]),\n    \"other_id\" =\u003e get_in(conn.private, [:private_resource, :id]),\n    \"should_not_appear\" =\u003e conn.private[:does_not_exist]\n  }\n\n  map\n  |\u003e Enum.filter(\u0026(\u00261 !== nil))\n  |\u003e Enum.into(%{})\nend\n\nplug Plug.LoggerJSON,\n  log: Logger.level(),\n  extra_attributes_fn: \u0026MyPlug.extra_attributes/1\n```\n\nIn 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.\n\n## Log Verbosity\n\n`LoggerJSON` plug supports two levels of logging:\n\n  * `info` / `error` will log:\n\n    * api_version,\n    * date_time,\n    * duration,\n    * log_type,\n    * method,\n    * path,\n    * request_id,\n    * status\n\n  * `warn` / `debug` will log everything from info and:\n\n    * client_ip,\n    * client_version,\n    * params / request_body.\n\nThe above are default. It is possible to override them by setting a `include_debug_logging` option to:\n\n  * `false` – means the extra debug fields (client_ip, client_version, and params) WILL NOT get logged.\n  * `true` – means the extra fields WILL get logged.\n  * Not setting this option will keep the defaults above.\n\nExample:\n\n```elixir\nplug Plug.LoggerJSON,\n  log: Logger.level,\n  include_debug_logging: true\n```\n\n## Contributing\n\nBefore submitting your pull request, please run:\n\n  * `mix credo --strict`,\n  * `mix coveralls`,\n  * `mix dialyzer`,\n  *  update changelog.\n\nPlease squash your pull request's commits into a single commit with a message and detailed description explaining the commit.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbleacherreport%2Fplug_logger_json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbleacherreport%2Fplug_logger_json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbleacherreport%2Fplug_logger_json/lists"}