Skip to content

RaymondLoranger/file_only_logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Proper configuration must be set via config files. See files config/config.exs and config/config_logger.exs as an example (including log file rotation).

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:

You may use file config/runtime.exs to configure the above log level:

import Config

config :file_only_logger, level: :info

Example

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(8)}
    #{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: undefinedModule: LogFunction: 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: undefinedModule: LogFunction: Check.log_warning/1

Note

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(8)}
    #{from(env, __MODULE__)}\
    """)
  end
end

About

A simple logger that writes messages to log files only (not to the console).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages