{"id":13508491,"url":"https://github.com/jclem/logfmt-elixir","last_synced_at":"2025-04-13T19:20:44.809Z","repository":{"id":32950854,"uuid":"36547317","full_name":"jclem/logfmt-elixir","owner":"jclem","description":"Decode and encode Logfmt lines in Elixir","archived":false,"fork":false,"pushed_at":"2022-01-10T20:06:20.000Z","size":94,"stargazers_count":27,"open_issues_count":1,"forks_count":12,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-10T17:31:10.889Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://hex.pm/packages/logfmt","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/jclem.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-30T06:59:04.000Z","updated_at":"2023-07-20T11:02:43.000Z","dependencies_parsed_at":"2022-07-16T05:00:35.108Z","dependency_job_id":null,"html_url":"https://github.com/jclem/logfmt-elixir","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Flogfmt-elixir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Flogfmt-elixir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Flogfmt-elixir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jclem%2Flogfmt-elixir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jclem","download_url":"https://codeload.github.com/jclem/logfmt-elixir/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248766687,"owners_count":21158302,"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":[],"created_at":"2024-08-01T02:00:53.862Z","updated_at":"2025-04-13T19:20:44.778Z","avatar_url":"https://github.com/jclem.png","language":"Elixir","funding_links":[],"categories":["Logging"],"sub_categories":[],"readme":"# Logfmt\n\n[![Build Status](https://github.com/jclem/logfmt-elixir/workflows/CI/badge.svg)](https://github.com/jclem/logfmt-elixir/actions?workflow=CI)\n[![Module Version](https://img.shields.io/hexpm/v/logfmt.svg)](https://hex.pm/packages/logfmt)\n[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/logfmt/)\n[![Total Download](https://img.shields.io/hexpm/dt/logfmt.svg)](https://hex.pm/packages/logfmt)\n[![License](https://img.shields.io/hexpm/l/logfmt.svg)](https://github.com/jclem/logfmt-elixir/blob/master/LICENSE.md)\n[![Last Updated](https://img.shields.io/github/last-commit/jclem/logfmt-elixir.svg)](https://github.com/jclem/logfmt-elixir/commits/master)\n\nLogfmt is a module for encoding and decoding logfmt-style log lines.\n\n## Installation\n\nThe package can be installed by adding `:logfmt` to your list of dependencies in\n`mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:logfmt, \"~\u003e 3.3\"}\n  ]\nend\n```\n\n## Usages\n\nDecode log lines into maps:\n\n```elixir\niex\u003e Logfmt.decode \"foo=bar\"\n%{\"foo\" =\u003e \"bar\"}\n```\n\nEncode Dict implementation values into log lines:\n\n```elixir\niex\u003e Logfmt.encode [foo: \"bar\"]\n\"foo=bar\"\n\niex\u003e Logfmt.encode %{foo: \"bar\"}\n\"foo=bar\"\n```\n\nCustom types can encoded by implementing the ValueEncoder protocol for it.\n\nFor example to encode DateTime and NaiveDateTime and implementation could look like this:\n\n```elixir\ndefimpl Logfmt.ValueEncoder, for: NaiveDateTime do\n  def encode(naive_date_time), do: NaiveDateTime.to_iso8601(naive_date_time)\nend\n\ndefimpl Logfmt.ValueEncoder, for: DateTime do\n  def encode(date_time), do: DateTime.to_iso8601(date_time)\nend\n```\n\n## Type Coercion\n\nWhen decoding a log line, Logfmt will coerce some strings into booleans and\nnumbers:\n\n```elixir\niex\u003e Logfmt.decode \"foo=true\"\n%{\"foo\" =\u003e true}\n\niex\u003e Logfmt.decode \"foo=-1.2e9\"\n%{\"foo\" =\u003e -1.2e9}\n```\n\nIn the future, this may be optional, or more robust. For example, it might make\nsense for `\"foo=true\"` to decode into `%{\"foo\" =\u003e true}`, but `~s(foo=\"true\")`\nto decode into `%{\"foo\" =\u003e \"true\"}`.\n\nAnother option might be to allow the user to provide a formatting map to the\n`decode` function, which expects coercion functions as values:\n\n```elixir\niex\u003e \"foo=1 bar=2\" |\u003e Logfmt.decode %{\n...\u003e foo: \u0026Logfmt.TypeCoercion.parse_integer/1\n...\u003e }\n%{\"foo\" =\u003e 1, \"bar\" =\u003e \"2\"}\n```\n\n## Why decode into maps?\n\nOriginally, this library both decoded and encoded maps. However, this was\nproblematic because key ordering in maps is not guaranteed. A developer wants to\nbe able to ensure that their log output will have identical ordering for\nmultiple calls for the sake of readability.\n\nTo solve this, the second version encoded and decoded Keyword lists only. Of\ncourse, this is also problematic because decoding log lines into Keyword lists\ninvolves converting user strings into non-garbage-collected atoms.\n\nNow, this module decodes into maps only (with string keys) and encodes any Dict\nimplementation type. This is a fair compromise, because ordering upon decoding a\nLogfmt line is not important, and keeping only the last value for a duplicate\nkey in a log line is fair, as well.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Flogfmt-elixir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjclem%2Flogfmt-elixir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjclem%2Flogfmt-elixir/lists"}