ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - jclem/logfmt-elixir: Decode and encode Logfmt lines in Elixir
Decode and encode Logfmt lines in Elixir. Contribute to jclem/logfmt-elixir development by creating an account on GitHub.
Visit Site

GitHub - jclem/logfmt-elixir: Decode and encode Logfmt lines in Elixir

GitHub - jclem/logfmt-elixir: Decode and encode Logfmt lines in Elixir

Logfmt

Build Status Module Version Hex Docs Total Download License Last Updated

Logfmt is a module for encoding and decoding logfmt-style log lines.

Installation

The package can be installed by adding :logfmt to your list of dependencies in mix.exs:

def deps do
  [
    {:logfmt, "~> 3.3"}
  ]
end

Usages

Decode log lines into maps:

iex> Logfmt.decode "foo=bar"
%{"foo" => "bar"}

Encode Dict implementation values into log lines:

iex> Logfmt.encode [foo: "bar"]
"foo=bar"

iex> Logfmt.encode %{foo: "bar"}
"foo=bar"

Custom types can encoded by implementing the ValueEncoder protocol for it.

For example to encode DateTime and NaiveDateTime and implementation could look like this:

defimpl Logfmt.ValueEncoder, for: NaiveDateTime do
  def encode(naive_date_time), do: NaiveDateTime.to_iso8601(naive_date_time)
end

defimpl Logfmt.ValueEncoder, for: DateTime do
  def encode(date_time), do: DateTime.to_iso8601(date_time)
end

Type Coercion

When decoding a log line, Logfmt will coerce some strings into booleans and numbers:

iex> Logfmt.decode "foo=true"
%{"foo" => true}

iex> Logfmt.decode "foo=-1.2e9"
%{"foo" => -1.2e9}

In the future, this may be optional, or more robust. For example, it might make sense for "foo=true" to decode into %{"foo" => true}, but ~s(foo="true") to decode into %{"foo" => "true"}.

Another option might be to allow the user to provide a formatting map to the decode function, which expects coercion functions as values:

iex> "foo=1 bar=2" |> Logfmt.decode %{
...> foo: &Logfmt.TypeCoercion.parse_integer/1
...> }
%{"foo" => 1, "bar" => "2"}

Why decode into maps?

Originally, this library both decoded and encoded maps. However, this was problematic because key ordering in maps is not guaranteed. A developer wants to be able to ensure that their log output will have identical ordering for multiple calls for the sake of readability.

To solve this, the second version encoded and decoded Keyword lists only. Of course, this is also problematic because decoding log lines into Keyword lists involves converting user strings into non-garbage-collected atoms.

Now, this module decodes into maps only (with string keys) and encodes any Dict implementation type. This is a fair compromise, because ordering upon decoding a Logfmt line is not important, and keeping only the last value for a duplicate key in a log line is fair, as well.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory