comet

Create a gleaming trail of application logs.

Types

pub type AttributeJsonSerializer(t) =
  fn(t) -> #(String, json.Json)
pub type ColorFn =
  fn(Segment, String) -> String

Entry is a log entry containing the log level, message, and metadata. This will be provided to the log formatter to be converted into a string.

pub type Entry(t) {
  Entry(level: Level, message: String, metadata: Metadata(t))
}

Constructors

  • Entry(level: Level, message: String, metadata: Metadata(t))

Formatter is called by the logger to format the log entry into a string before it is sent to the log handler. The formatter is passed the context and the log entry and should return a string. The formatter is optional, if one is not provided than the default formatter is used.

pub type Formatter(t) =
  fn(Context(t), Entry(t)) -> String

Handlers are functions which receive a formatted string to be sent to the log destination. The default handler emits logs to the console or standard_io in the case of the erlang backend. Additional handlers can be added to the logger to send logs to other destinations, such as shipping logs to a remote server or writing logs to a file.

pub type Handler =
  fn(String) -> Nil

LevelTextFn can be provided to the log context to override the level names in logs

pub type LevelTextFn =
  fn(Level) -> String

Metadata is a list of attributes that can be attached to a log entry.

pub type Metadata(t) =
  List(t)
pub type Segment {
  Message
  Value
  Label
}

Constructors

  • Message
  • Value
  • Label

Functions

pub fn attribute(md: List(a), attribute: a) -> List(a)

Add an attribute to the metadata.

pub fn attributes(md: List(a), attributes: List(a)) -> List(a)

Append a list of attributes to the metadata.

pub fn color_plain(segment: Segment, value: String) -> String

ColorFn that doesn’t colorize the text.

pub fn colorize(segment: Segment, value: String) -> String

default ColorFn

pub fn configure(ctx: Context(a)) -> Context(a)

configure is used to initialize the global logger settings. It should be called once at the start of the application. You should not call it again after the logger has been initialized as it will overwrite the global erlang and javascript logger settings.

pub fn debug(md: List(a), msg: String) -> Nil

log a message at the debug level

pub fn error(md: List(a), msg: String) -> Nil

log a message at the warning level log a message at the error level

pub fn get_formatter(
  ctx: Context(a),
) -> Option(fn(Context(a), Entry(a)) -> String)
pub fn get_handler(ctx: Context(a)) -> Option(fn(String) -> Nil)
pub fn info(md: List(a), msg: String) -> Nil

log a message at the debug level log a message at the info level

pub fn json_formatter(
  serializer: fn(a) -> #(String, Json),
) -> fn(Context(a), Entry(a)) -> String
pub fn log() -> List(a)

creates a new leg entry builder.

pub fn new() -> Context(a)

new creates a new logger context with default settings. The default settings are:

  • level_text: debug, info, warn, error
  • formatter: None (uses the backend default formatter)
  • min_level: Info
pub fn set_handler(
  ctx: Context(a),
  name: String,
  handler: fn(String) -> Nil,
) -> Nil

Set the context’s handler to a custom Handler.

pub fn text_formatter(ctx: Context(a), entry: Entry(a)) -> String

This is the standard text formatter for logs. It will format the log entry into a string

pub fn timestamp(ctx: Context(a), active: Bool) -> Context(a)

toggle timestamps

pub fn warning(md: List(a), msg: String) -> Nil

log a message at the info level log a message at the warning level

pub fn with_color_fn(
  ctx: Context(a),
  func: fn(Segment, String) -> String,
) -> Context(a)

set the text color function for the logger.

pub fn with_formatter(
  ctx: Context(a),
  formatter: fn(Context(a), Entry(a)) -> String,
) -> Context(a)

provide a custom formatter to format log entries

pub fn with_level(ctx: Context(a), level: Level) -> Context(a)

set the log level for the logger

pub fn with_level_text(
  ctx: Context(a),
  func: fn(Level) -> String,
) -> Context(a)

Provide a function to override the log level names emitted in logs

Search Document