palabres/options

Options to configure Palabres at start.

import palabres
import palabres/level
import palabres/options

pub fn configure_logger() {
  options.defaults()
  // Enables or disable colored output.
  |> options.color(True)
  // Enables or disable JSON output. JSON output will never be colored.
  |> options.json(False)
  // Set the minimum log level.
  |> options.level(level.Info)
  // Output to a file instead of stdout.
  |> options.output(to: {
    options.file(test_utils.log_file)
    |> options.flush(every: 5000)
  })
  // Applies or not palabres styling to default logger (on stdout).
  |> options.style_default_logger(True)
  // Configure the logger.
  |> palabres.configure
}

Types

Options to configure the logger. To create with defaults. Below are the configurable options, usable with the corresponding functions.

  • color Defaults to True.
    Allow to choose if the output is colored or not.
    Can be deactivated globally with the NO_COLOR (or NO_COLOUR) environment variable.
    Once set, color defaults to False.
  • json Defaults to False.
    Activate the JSON mode, forcing the logger to output JSON instead of strings.
    JSON output will automatically disable colored logging.
  • level Defaults to level.Info.
    Set the minimum required log level.
    Every level below will be ignored.
  • output Defaults to stdout.
    Allow to choose writing to stdout or a file.
  • style_default_logger Defaults to True.
    Apply the palabres styling to the default logger.
  • auto_at Defaults to True.
    Automatically fill the at field with the module and function creating the log.
  • auto_when Defaults to True.
    Automatically fill the when field with the timestamp of the log.
pub opaque type Options

Configurable output for the logger. Can be the standard output, or a selected file.
Standard output can be simply selected with stdout, while file logging is selected with file.
A file logger will dump the log contents at regular interval. This interval is configurable by using flush.

pub fn configure_logger() {
  options.defaults()
  // Choose to output to stdout.
  |> options.output(to: options.stdout())
  // Choose to output to a file.
  |> options.output(to: {
    options.file("/var/log/my_app.log")
    // Flush interval can be seldected in milliseconds.
    |> options.flush(every: 2500)
  })
}
pub opaque type Output

Values

pub fn auto_at(options: Options, enabled: Bool) -> Options

Defaults to True.
Activate or deactivate the automatic at field. When activated, every log is tagged with the module and the function it has been created in, unless palabres.at overrides it.
Locating a log requires inspecting the call stack, which is cheap but not free. Deactivate it on hot paths if every microsecond matters.

pub fn auto_when(options: Options, enabled: Bool) -> Options

Defaults to True.
Activate or deactivate the automatic when field, holding the timestamp of the log in ISO 8601 format. Deactivate it when your log collector adds its own timestamp, like journald or most cloud log processors, to avoid paying twice for the same information.

pub fn color(options: Options, color: Bool) -> Options

Activate or deactivate colored output.
Defaults to True if environment variable NO_COLOR is unset.
Defaults to False otherwise.
If color is called, it takes precedence on environment variable.

pub fn defaults() -> Options

Initialise the options with defaults.

  • color: True if environment variable NO_COLOR is unset, False otherwise.
  • json: False
  • level: level.Info
  • output: stdout
  • style_default_logger: True
  • auto_at: True
  • auto_when: True
pub fn file(filename: String) -> Output

Creates a file output, to use with output. Flush interval can be configured with flush.

pub fn flush(file: Output, every interval: Int) -> Output

When outputting to a file on Erlang, logger will sync the file every 5 seconds by default. flush allows to choose how long to wait between two syncs. Be careful, setting a too small interval could lead to performance issues, because BEAM will spend too much time in file writing.
Has no effect on JavaScript, where logs are written to the file as they come, through a single stream kept open by the logger.

pub fn json(options: Options, json: Bool) -> Options

Defaults to False.
Activate or deactive JSON ouput.
If activated, colored output is automatically deactivated.

pub fn level(options: Options, level: level.Level) -> Options

Defaults to level.Info. Set the minimum desired log level.

pub fn output(options: Options, to output: Output) -> Options

Defaults to stdout. Choose the desired output.

pub fn stdout() -> Output

Creates the stdout output.

pub fn style_default_logger(
  options: Options,
  style_default: Bool,
) -> Options

Defaults to True.
Activate or deactivate styling from Palabres on default logger.
Has no effect on JavaScript, since there’s no default logger.

✨ Search Document