glight

Types

This determines whether the logger functions will use call or cast to messages to the loger actor. Cast is preferred in long running processes, as it is much ligherweight and just fires and forgets. However in a short- lived script this will drop a bunch of logs when the process ends before the logs get processed. In this case use DispatchCall to ensure that each log function waits for the log message to be processed before returning.

pub type DispatchMode {
  DispatchCall
  DispatchCast
}

Constructors

  • DispatchCall

    DispatchCall leverages process.call to wait for log to be processed before returning control back to the caller. Use this in a one off script or short-lived process.

  • DispatchCast

    DispatchCast fires and forgets the log message, returning control to the caller right away. Use this in something long lived or if there are performance considerations or if capturing exhaustive logs is not critical.

Single error type encapsulating all possible errors this library can return

pub type GlightError {
  FileStreamError(file_stream_error.FileStreamError)
  ActorStartError(actor.StartError)
}

Constructors

  • FileStreamError(file_stream_error.FileStreamError)
  • ActorStartError(actor.StartError)

Configuration that users construct to determine how this logger behaves. These options are individually set with a builder pattern, and their behaviors are defined in those setter function docs.

pub type LogConfig {
  LogConfig(
    level: LogLevel,
    is_color: Bool,
    dispatch_mode: DispatchMode,
    time_key: String,
    msg_key: String,
    level_key: String,
  )
}

Constructors

  • LogConfig(
      level: LogLevel,
      is_color: Bool,
      dispatch_mode: DispatchMode,
      time_key: String,
      msg_key: String,
      level_key: String,
    )

LogContext contains the logger process itself, the configuration and structured data that is being accumulated for the individual log message.

pub type LogContext {
  LogContext(
    logger: Subject(Message),
    config: LogConfig,
    data: Dict(String, String),
  )
}

Constructors

  • LogContext(
      logger: Subject(Message),
      config: LogConfig,
      data: Dict(String, String),
    )

The logger actor itself - Log message type contains all the context needed to produce a log for each of the tranports. Note that the caller is optional, and this is used to determine if the caller is waiting for a response or not.

pub type Message {
  Log(
    caller: Option(Subject(Bool)),
    level: LogLevel,
    msg: String,
    data: Dict(String, String),
    config: LogConfig,
  )
}

Constructors

  • Log(
      caller: Option(Subject(Bool)),
      level: LogLevel,
      msg: String,
      data: Dict(String, String),
      config: LogConfig,
    )

Transports are the things that actually do the logging. Glight supports multiple transports at once, so you can log to the console and a file at the same time, or multiple files or multiple consoles, etc. Be creative!

pub type Transport {
  ConsoleTransport
  FileTransport(file: String)
  CustomTransport(
    log: fn(LogLevel, String, Dict(String, String), LogConfig) ->
      Nil,
  )
}

Constructors

  • ConsoleTransport

    ConsoleTransport logs to std out, by default with nice colors that can be turned off with the logger configuration.

  • FileTransport(file: String)

    FileTransport streams JSON formatted logs to a file that is created if it does not exist. JSON keys are configurable in the logger configuration.

  • CustomTransport(
      log: fn(LogLevel, String, Dict(String, String), LogConfig) ->
        Nil,
    )

    CustomTransport allows you to provide your own logging function. Provide a callback that takes the log level, log message itself, structured data associated with the log and the configuration.

Functions

pub fn alert(ctx: LogContext, log_msg: String) -> LogContext

Log at the alert level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn critical(ctx: LogContext, log_msg: String) -> LogContext

Log at the critical level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn debug(ctx: LogContext, log_msg: String) -> LogContext

Log at the debug level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn emergency(ctx: LogContext, log_msg: String) -> LogContext

Log at the emergency level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn error(ctx: LogContext, log_msg: String) -> LogContext

Log at the error level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn info(ctx: LogContext, log_msg: String) -> LogContext

Log at the info level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn must_start(transports: List(Transport)) -> LogContext

Convenience function that will panic if the logger cannot be started.

pub fn notice(ctx: LogContext, log_msg: String) -> LogContext

Log at the notice level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn set_dispatch_mode(
  ctx: LogContext,
  dispatch_mode: DispatchMode,
) -> LogContext

Set whether the logger functions use call or cast to send messages to the logger actor. DispatchCast by default.

Examples

glight.must_start([glight.ConsoleTransport])
|> glight.info("in a one off script, this log may be dropped")

glight.must_start([glight.ConsoleTransport])
|> glight.set_dispatch_mode(glight.DispatchCall)
|> glight.info("this log will always be processed before returning")
pub fn set_is_color(
  ctx: LogContext,
  is_color: Bool,
) -> LogContext

Set whether color strings are used in the console transport.

Examples

glight.must_start([glight.ConsoleTransport])
|> glight.set_is_color(True)
|> glight.info("this log will be colorful")

glight.must_start([glight.ConsoleTransport])
|> glight.set_is_color(False)
|> glight.info("this will not be colorful")
pub fn set_level(ctx: LogContext, level: LogLevel) -> LogContext

Set the log level for the logger. Logs below this level will be ignored.

Examples

glight.must_start([glight.ConsoleTransport])
|> glight.set_level(logging.Debug)
|> glight.debug("this message will be logged")
|> glight.warn("this message will also be logged")

glight.must_start([glight.ConsoleTransport])
|> glight.set_level(logging.Warning)
|> glight.debug("this message not will be logged")
|> glight.warn("but this message will be")
pub fn set_level_key(
  ctx: LogContext,
  level_key: String,
) -> LogContext

Set the key that is used for the log level in the JSON output. level by default.

Examples


glight.must_start([glight.FileTransport("server.log")])
|> glight.info("this message's level will be keyed with 'level'")
|> glight.set_level_key("lvl")
|> glight.info("this message's level will be keyed with 'lvl'")
→ cat server.log
{"time":"2025-04-05T10:18:31.410-07:00","level":"info","msg":"this message's level will be keyed with 'level'"}
{"time":"2025-04-05T10:18:31.410-07:00","lvl":"info","msg":"this message's level will be keyed with 'lvl'"}
pub fn set_msg_key(
  ctx: LogContext,
  msg_key: String,
) -> LogContext

Set the key that is used for the log message in the JSON output. msg by default.

Examples


glight.must_start([glight.FileTransport("server.log")])
|> glight.info("this message will be keyed with 'msg'")
|> glight.set_msg_key("message")
|> glight.info("this message will be keyed with 'message'")
|> glight.set_msg_key("MASSAGE")
|> glight.info("this message will be humorously keyed with 'MASSAGE'")
→ cat server.log
{"time":"2025-04-05T10:18:31.410-07:00","level":"info","msg":"this message will be keyed with 'msg'"}
{"time":"2025-04-05T10:18:31.410-07:00","level":"info","message":"this message will be keyed with 'message'"}
{"time":"2025-04-05T10:18:31.428-07:00","level":"info","MASSAGE":"this message will be humorously keyed with 'MASSAGE'"}
pub fn set_time_key(
  ctx: LogContext,
  time_key: String,
) -> LogContext

Set the key that is used for the time in the JSON output. time by default.

Examples


glight.must_start([glight.FileTransport("server.log")])
|> glight.info("this message's time will be keyed with 'time'")
|> glight.set_time_key("ts")
|> glight.info("this message's time will be keyed with 'ts'")
→ cat server.log
{"time":"2025-04-05T10:18:31.410-07:00","level":"info","msg":"this message's time will be keyed with 'time'"}
{"ts":"2025-04-05T10:18:31.410-07:00","lvl":"info","msg":"this message's time will be keyed with 'ts'"}
pub fn start(
  transports: List(Transport),
) -> Result(LogContext, GlightError)

Initialize the logger actor. This can fail if the actor does not start or if the transports are invalid (mostly likely a file that cannot be streamed to).

pub fn warn(ctx: LogContext, log_msg: String) -> LogContext

Log at the warning level. See the erlang log level documentation for more information and context. https://www.erlang.org/doc/apps/kernel/logger_chapter.html#log-level

pub fn with(
  ctx: LogContext,
  key: String,
  value: String,
) -> LogContext

This function accumulates structured data for the log message. This data will show up keyed in the json logs and formatted nicely in the console logs.

Search Document