FileOnly Logger
A simple logger that writes messages to log files only (not to the console). Elixir's Logger Backends were abandoned in favor of Erlang's Logger handlers.
Installation
Add file_only_logger to your list of dependencies in mix.exs:
def deps do
[
{:file_only_logger, "~> 0.2.0"}
]
end
Logger configuration may but does not have to be set via config files.
See files config/config.exs and config/config_logger.exs as an example
(including log file rotation).
In the absence of any manual configuration, the default handler (:default)
will automatically feature the following colors:
| Level | Color |
|---|---|
| :debug | :light_cyan |
| :info | :light_green |
| :warning | :light_yellow |
| :error | :light_red |
Likewise without any manual configuration, the handlers below will automatically log to their corresponding files:
| Level | Handler | File |
|---|---|---|
| :debug | :debug_handler | "./log/debug.log" |
| :info | :info_handler | "./log/info.log" |
| :warning | :warning_handler | "./log/warning.log" |
| :error | :error_handler | "./log/error.log" |
All of the files above will have a log rotation of 5 archives. For example, active file "debug.log" may have 5 archives: "debug.log.0" up to "debug.log.4".
Usage
Trying to log any message with severity less than the configured level will simply cause the message to be ignored.
The configuration values for log level are:
- :all (default)
- :none
- Levels
You may use file config/runtime.exs to configure the above log level:
import Config
config :file_only_logger, level: :infoExample
defmodule Log do
use File.Only.Logger
warning :error_occurred, {reason, file, env} do
"""
\n'error' occurred...
• Reason => '#{:file.format_error(reason)}'
• File => #{Path.expand(file) |> inspect() |> maybe_break(10)}
#{from(env, __MODULE__)}\
"""
end
end
defmodule Check do
def log_warning(file) do
Log.warning(:error_occurred, {:enoent, file, __ENV__})
end
end
Check.log_warning(__ENV__.file)
Check.log_warning("generate-line-break")
# will respectively log these lines in the configured log file(s):
2025-01-13 12:27:14.532 [warning]
'error' occurred...
• Reason => 'no such file or directory'
• File => "c:/Users/Ray/Documents/ex_dev/projects/file_only_logger/iex"
• App: undefined
• Module: Log
• Function: Check.log_warning/1
2025-01-13 12:30:46.370 [warning]
'error' occurred...
• Reason => 'no such file or directory'
• File =>
"c:/Users/Ray/Documents/ex_dev/projects/file_only_logger/generate-line-break"
• App: undefined
• Module: Log
• Function: Check.log_warning/1Note
If you'd like to write a message to both the log files and the console, simply change the macro to a function like so...
Example
defmodule Log do
use File.Only.Logger
require Logger
def warning(:error_occurred, {reason, file, env}) do
Logger.warning("""
\n'error' occurred...
• Reason => '#{:file.format_error(reason)}'
• File => #{Path.expand(file) |> inspect() |> maybe_break(10)}
#{from(env, __MODULE__)}\
""")
end
end