glogg/logger

Types

pub opaque type Field
pub type HookFn =
  fn(LogEvent) -> option.Option(LogEvent)
pub type LogEvent {
  LogEvent(
    level: level.Level,
    message: String,
    fields: List(Field),
  )
}

Constructors

pub opaque type Logger

Values

pub fn add_hook(
  logger: Logger,
  hook: fn(LogEvent) -> option.Option(LogEvent),
) -> Logger

Adds a hook function to a logger. The hook function can modify or filter log events. Hooks are executed in the order they are added.

If the hook returns None, the log event will be discarded. If the hook returns Some(event), the modified event will be logged.

Example

let my_logger =
  logger.new("name")
  |> logger.add_hook(fn(event) {
    case event {
      logger.LogEvent(level, message, fields) ->
        // Add a custom field to every log event
        let new_fields =
          list.append(fields, [
            logger.string("custom_field", "value")
          ])
        Some(logger.LogEvent(level, message, new_fields))
    }
  })
pub fn alert(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Alert level to standard error.

Example

my_logger
|> logger.alert("Message", [
  logger.string("key", "value"),
  logger.stacktrace(),
])
pub fn bool(key: String, value: Bool) -> Field

Creates a boolean field with the given key and value.

Example

my_logger
|> logger.info("Message", [
  logger.bool("key", True),
])
pub fn clear_hooks(logger: Logger) -> Logger

Clears all hooks from a logger.

pub fn critical(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Critical level to standard error.

Example

my_logger
|> logger.critical("Message", [
  logger.string("key", "value"),
  logger.stacktrace(),
])
pub fn debug(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Debug level.

Example

my_logger
|> logger.debug("Message", [
  logger.string("key", "value"),
])
pub fn duration_ms(
  key: String,
  duration: duration.Duration,
) -> Field

Creates a duration field in milliseconds with the given key and value.

Example

my_logger
|> logger.info("Message", [
  logger.duration_ms("key", duration.milliseconds(10)),
])
pub fn emergency(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Emergency level to standard error.

Example

my_logger
|> logger.emergency(logger, "Message", [
  logger.string("key", "value"),
  logger.stacktrace(),
])
pub fn error(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Error level to standard error.

Example

my_logger
|> logger.error("Message", [
  logger.string("key", "value"),
  logger.stacktrace(),
])
pub fn fields_to_metadata(
  fields: List(Field),
) -> dict.Dict(String, dynamic.Dynamic)

Converts a list of fields into a metadata dictionary suitable for logging.

pub fn float(key: String, value: Float) -> Field

Creates a float field with the given key and value.

Example

my_logger
|> logger.info("Message", [
  logger.float("key", 3.14),
])
pub fn get_context(logger: Logger) -> List(Field)

Retrieves the context fields of a logger.

pub fn group(key: String, fields: List(Field)) -> Field

Creates a group field with the given key and nested fields.

Example

my_logger
|> logger.info("Message", [
  logger.group("key", [
    logger.string("nested_key", "nested_value"),
  ])
])
pub fn info(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Info level.

Example

my_logger
|> logger.info("Message", [
  logger.string("key", "value"),
])
pub fn int(key: String, value: Int) -> Field

Creates an integer field with the given key and value.

Example

my_logger
|> logger.info("Message", [
  logger.int("key", 42),
])
pub fn new(name: String) -> Logger

Creates a new logger.

The logger will have a default context field “logger” with the given name.

Example

let my_logger = logger.new("name")
pub fn notice(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Notice level.

Example

my_logger
|> logger.notice("Message", [
  logger.string("key", "value"),
])
pub fn stacktrace() -> Field

Creates a stacktrace field that with the key “stacktrace”.

Example

my_logger
|> logger.info("Message", [
  logger.stacktrace(),
])
pub fn string(key: String, value: String) -> Field

Creates a string field with the given key and value.

Example

my_logger
|> logger.info("Message", [
  logger.string("key", "value"),
])
pub fn warning(
  logger: Logger,
  message: String,
  fields: List(Field),
) -> Nil

Logs a message and fields at the Warning level.

Example

my_logger
|> logger.warning("Message", [
  logger.string("key", "value"),
])
pub fn with_context(
  logger: Logger,
  fields: List(Field),
) -> Logger

Adds context fields to a logger.

These fields will be included in every log message.

Example

let my_logger =
  logger.new("name")
  |> logger.with_context([
    logger.string("app", "my_app"),
    logger.string("env", "production"),
  ])
Search Document