https://github.com/tagbase-io/logger-binary
A custom Logger formatter for handling binary data.
https://github.com/tagbase-io/logger-binary
binary elixir elixir-logger logger
Last synced: 5 months ago
JSON representation
A custom Logger formatter for handling binary data.
- Host: GitHub
- URL: https://github.com/tagbase-io/logger-binary
- Owner: tagbase-io
- License: mit
- Created: 2025-01-27T15:48:03.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-02T08:01:29.000Z (over 1 year ago)
- Last Synced: 2025-10-11T02:20:16.540Z (9 months ago)
- Topics: binary, elixir, elixir-logger, logger
- Language: Elixir
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# LoggerBinary [](https://github.com/tagbase-io/logger-binary/actions/workflows/test.yml)
A custom `Logger` formatter for handling binary data.
This formatter is designed to correctly format binary messages. It converts
non-printable binary data into a hexadecimal string representation and can
optionally add a directional indicator (`"< "` or `"> "`) when needed.
## Usage
```elixir
def deps do
[
{:logger_binary, "~> 0.2.0"}
]
end
```
```elixir
config :logger, LoggerBinary.Formatter, format: "[$level] $message\n"
config :logger, :console,
format: {LoggerBinary.Formatter, :format},
metadata: [:direction]
```
## Features
* Formats binary data as uppercase hexadecimal strings via `LoggerBinary.format/1`.
* Prepends formatted binary messages with a directional indicator if the `:direction`
metadata is present. Supported values for the `:direction` metadata are `:in` and `:out`.
* Uses the default Logger format for all other types of messages (e.g., printable strings and charlists).
## Direction Indicator
You can add the `:direction` metadata to your log messages to indicate if the
binary data is an incoming or outgoing message:
```elixir
Logger.debug(<<0x01, 0x02, 0x03>>, direction: :in)
# Logs: "[debug] < 01 02 03"
Logger.debug(<<0x01, 0x02, 0x03>>, direction: :out)
# Logs: "[debug] > 01 02 03"
```
Without directional metadata, it simply logs the formatted binary:
```elixir
Logger.debug(<<0x01, 0x02, 0x03>>)
# Logs: "[debug] 01 02 03"
```
## Direct Formatting
You can also format binary data directly without logging:
```elixir
LoggerBinary.format(<<0xDE, 0xAD, 0xBE, 0xEF>>)
# Returns: "DE AD BE EF"
LoggerBinary.format(:timeout)
# Returns: ":timeout"
```