{"id":13508496,"url":"https://github.com/navinpeiris/logster","last_synced_at":"2025-05-15T00:06:50.454Z","repository":{"id":45492943,"uuid":"55137996","full_name":"navinpeiris/logster","owner":"navinpeiris","description":"Easily parsable single line, plain text and JSON logger for Plug and Phoenix applications","archived":false,"fork":false,"pushed_at":"2025-01-19T01:30:40.000Z","size":221,"stargazers_count":207,"open_issues_count":7,"forks_count":28,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-10T10:03:55.509Z","etag":null,"topics":["elixir","logging","phoenix","plug"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/navinpeiris.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-03-31T09:32:48.000Z","updated_at":"2025-04-01T07:07:06.000Z","dependencies_parsed_at":"2025-01-29T02:31:34.968Z","dependency_job_id":null,"html_url":"https://github.com/navinpeiris/logster","commit_stats":{"total_commits":102,"total_committers":18,"mean_commits":5.666666666666667,"dds":"0.22549019607843135","last_synced_commit":"e48cd299d618abcfd732cf1c49026351758e6692"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navinpeiris%2Flogster","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navinpeiris%2Flogster/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navinpeiris%2Flogster/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/navinpeiris%2Flogster/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/navinpeiris","download_url":"https://codeload.github.com/navinpeiris/logster/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254249198,"owners_count":22039029,"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","logging","phoenix","plug"],"created_at":"2024-08-01T02:00:53.943Z","updated_at":"2025-05-15T00:06:50.436Z","avatar_url":"https://github.com/navinpeiris.png","language":"Elixir","readme":"# Logster\n\n[![CI](https://github.com/navinpeiris/logster/actions/workflows/ci.yml/badge.svg)](https://github.com/navinpeiris/logster/actions/workflows/ci.yml)\n[![Hex version](https://img.shields.io/hexpm/v/logster.svg \"Hex version\")](https://hex.pm/packages/logster)\n[![Hex downloads](https://img.shields.io/hexpm/dt/logster.svg \"Hex downloads\")](https://hex.pm/packages/logster)\n[![License](http://img.shields.io/:license-mit-blue.svg)](http://doge.mit-license.org)\n\n\u003e **Note**\\\n\u003e This is the documentation for v2 of Logster. If you're looking for v1, see the [v1 branch](https://github.com/navinpeiris/logster/tree/v1.x).\n\nAn easy-to-parse, single-line logger for Elixir Phoenix and Plug applications. Supports logfmt, JSON and custom formatting.\n\n## Motivation\n\nBy default, the Phoenix log output for a request looks like:\n\n```text\n[info] GET /articles/some-article\n[debug] Processing with HelloPhoenix.ArticleController.show/2\n  Parameters: %{\"id\" =\u003e \"some-article\"}\n  Pipelines: [:browser]\n[info] Sent 200 in 21ms\n```\n\nThis can be handy for development, but cumbersome in production. The log output is spread across multiple lines making it difficult to parse and search.\n\nLogster aims to solve this problem by logging the request in a easy-to-parse single line like:\n\n```text\n[info] state=sent method=GET path=/articles/some-article format=html controller=HelloPhoenix.ArticleController action=show params={\"id\":\"some-article\"} status=200 duration=0.402\n```\n\nThis is especially handy when integrating with log management services such as [Better Stack](https://betterstack.com/telemetry) or [Papertrail](https://papertrailapp.com/).\n\nAlternatively, Logster can also output JSON formatted logs (see configuration section below), or you can provide a custom formatter:\n\n```text\n[info] {\"state\":\"sent\",\"method\":\"GET\",\"path\":\"/articles/some-article\",\"format\":\"html\",\"controller\":\"HelloPhoenix.ArticleController\",\"action\":\"show\",\"params\":{\"id\":\"some-article\"},\"status\":200,\"duration\":0.402}\n```\n\n## Migrating from v1.x to v2.x\n\nSee [Migration Guide](MIGRATION_GUIDE.md) for more information on migrating from v1.x to v2.x.\n\n## Installation\n\nAdd `:logster` to the list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [{:logster, \"~\u003e 2.0.0-rc.1\"}]\nend\n```\n\n## Usage\n\n### Using with Phoenix\n\nAttach the Logster Phoenix logger in the `start` function in your project's `application.ex` file:\n\n```elixir\n# lib/my_app/application.ex\ndef start(_type, _args) do\n  children = [\n    # ...\n  ]\n\n  #\n  # Add the line below:\n  #\n  :ok = Logster.attach_phoenix_logger()\n\n  opts = [strategy: :one_for_one, name: MyApp.Supervisor]\n  Supervisor.start_link(children, opts)\nend\n```\n\nNext, disable the default Phoenix logger by adding the following line to your `config.exs` file:\n\n```elixir\n# config/config.exs\nconfig :phoenix, :logger, false\n```\n\n### Using with Plug\n\nAdd `Logster.Plug` to your plug pipeline, or in the relevant module:\n\n```elixir\nplug Logster.Plug\n```\n\n### Using the standalone logger\n\nLogster provides `debug`, `info`, `warning`, `error` etc convenience functions that mimic those provided by the elixir logger, which outputs messages in your chosen log format.\n\nFor example:\n\n```elixir\nLogster.info(service: :payments, event: :received, amount: 1000, customer: 123)\n```\n\nwill output the following:\n\n```\n[info] service=payments event=received amount=1000 customer=123\n```\n\nYou can also provide a function to be called lazily, which will only be called if the log level is enabled:\n\n```elixir\nLogster.debug(fn -\u003e\n  # some potentially expensive operation\n  # won't be called if the log level is not enabled\n  customer = get_customer_id()\n\n  [service: :payments, event: :received, amount: 1000, customer: customer]\nend)\n```\n\n## Configuration\n\n### Application wide\n\nYou can configure Logster application wide using your `config.exs`, or environment specific config file by providing options like:\n\n```elixir\nconfig :logster,\n  formatter: :json,\n  headers: [\"content-type\"],\n  excludes: [:params]\n```\n\n### Per request\n\nYou can then customize each option on a request basis by passing them as options to the `Logster.ChangeConfig` plug in the relevant controller or plug:\n\n```elixir\nplug Logster.ChangeConfig, status_2xx_level: :debug, headers: [\"content-type\", \"x-request-id\"]\n```\n\nor, for specific actions in the controller:\n\n```elixir\nplug Logster.ChangeConfig, [status_2xx_level: :debug, headers: [\"content-type\", \"x-request-id\"]] when action in [:index, :show]\n```\n\nThis 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.\n\n### Plug level\n\nIf you're using the `Logster.Plug` plug, you can also pass options to it directly:\n\n```elixir\nplug Logster.Plug, status_2xx_level: :debug, headers: [\"content-type\", \"x-request-id\"]\n```\n\n## Configuration options\n\n### Formatter\n\n#### JSON formatter\n\n```elixir\nconfig :logster, formatter: :json\n```\n\n_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](http://elixir-lang.org/docs/stable/logger/Logger.html) for more information.\n\n#### Custom formatter\n\nProvide a function that takes one argument, the parameters as input, and returns formatted output\n\n```elixir\nconfig :logster, formatter: \u0026MyCustomFormatter.format/1\n```\n\n### Log level per status group\n\nYou can change the log level for each status group by using the following configuration options:\n\n```elixir\nconfig :logster,\n  status_2xx_level: :debug, # default: :info\n  status_3xx_level: :debug, # default: :info\n  status_4xx_level: :info,  # default: :warning\n  status_5xx_level: :error  # default: :error\n```\n\n### Fine grained log level configuration\n\nYou can specify a function to be called to determine the log level for each request.\n\nThis function will be called with the `conn`, and expects a logger level, or `false` to not log the request as return value.\n\n```elixir\n# config/config.exs\nconfig :logster, log: {MyLoggingModule, :log_level, []}\n```\n\n```elixir\ndefmodule MyLoggingModule do\n  def log_level(%{status: status}) when status \u003e= 500, do: :error\n  def log_level(%{status: status}) when status \u003e= 400, do: :warning\n  def log_level(%{path_info: [\"status\" | _]}), do: false\n  def log_level(_), do: :info\nend\n```\n\n### Request headers\n\nBy default, Logster won't log any request headers. To log specific headers, you can use the `:headers` option:\n\n```elixir\nconfig :logster, headers: [\"my-header-one\", \"my-header-two\"]\n```\n\n### Enabling extra fields\n\nOne or more of the following fields can be optionally enabled through the `extra_fields` configuration option:\n\n- host\n- query\n\nExample:\n\n```elixir\nconfig :logster, extra_fields: [:host, :query]\n```\n\n### Excluding fields\n\nYou can exclude fields with `:excludes`:\n\n```elixir\nconfig :logster, excludes: [:params, :status, :state]\n```\n\nExample output:\n\n```\n[info] method=GET path=/articles/some-article format=html controller=HelloPhoenix.ArticleController action=show duration=0.402\n```\n\n### Renaming default fields\n\nYou can rename the default keys passing a keyword list like:\n\n```elixir\nconfig :logster, renames: [duration: :response_time, params: :parameters]\n```\n\nExample output:\n\n```\n[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\n```\n\n### Filtering parameters\n\nBy default, Logster filters parameters named `password`.\n\nTo change the filtered parameters:\n\n```elixir\nconfig :logster, filter_parameters: [\"password\", \"secret\", \"token\"]\n```\n\n### Metadata\n\nCustom metadata can be added to logs using `Logger.metadata` and configuring your logger backend:\n\n```elixir\n# add metadata for all future logs from this process\nLogger.metadata(%{user_id: \"123\", foo: \"bar\"})\n\n# example for configuring console backend to include metadata in logs.\n# see https://hexdocs.pm/logger/Logger.html#module-console-backend documentation for more\n# config.exs\nconfig :logger, :console, metadata: [:user_id, :foo]\n```\n\nThe easiest way to do this app wide is to introduce a new plug which you can include in your Phoenix router pipeline.\n\nFor example:\n\n```elixir\ndefmodule HelloPhoenix.SetLoggerMetadata do\n  def init(opts), do: opts\n\n  def call(conn, _opts) do\n    Logger.metadata user_id: get_user_id(conn),\n                    remote_ip: format_ip(conn)\n    conn\n  end\n\n  defp format_ip(%{remote_ip: remote_ip}) when remote_ip != nil, do: :inet_parse.ntoa(remote_ip)\n  defp format_ip(_), do: nil\n\n  defp get_user_id(%{assigns: %{current_user: %{id: id}}}), do: id\n  defp get_user_id(_), do: nil\nend\n```\n\nAnd then add this plug to the relevant pipelines in the router:\n\n```elixir\npipeline :browser do\n  plug :fetch_session\n  plug :fetch_flash\n  plug :put_secure_browser_headers\n  # ...\n  plug HelloPhoenix.SetLoggerMetadata\n  # ...\nend\n```\n\n## Development\n\nUse the following mix task before pushing commits to run the same checks that are run in CI:\n\n```\nmix ci\n```\n\n## Acknowledgements\n\nThis library was inspired by the ruby [lograge](https://github.com/roidrage/lograge) gem.\n\n## License\n\nThe MIT License\n\nCopyright (c) 2016-present Navin Peiris\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","funding_links":[],"categories":["Logging"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnavinpeiris%2Flogster","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnavinpeiris%2Flogster","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnavinpeiris%2Flogster/lists"}