Grav v1.14.0

Log

import "std/sys" (included in import "std") · namespace std.sys

Leveled logging: messages marked Debug, Info, Warn or Error, written with a timestamp to standard error or a file, where a level setting decides which ones appear. Use it instead of print for diagnostics, so output for users stays separate from output for operators.

grav
import "std"

fn main() {
    let p = Path.combine(Dir.temp(), "grav-docs-log.log")
    new File(p).delete()

    let db = Log.named("db").toFile(p).with("host", "localhost")
    db.timestamps = false            // so the output below is predictable
    db.level = LogLevel.Info
    db.debug("connecting")           // below the level: skipped
    db.info("connected")
    db.warn("slow query")
    db.error("query failed", new IOError("disk full"))

    print(new File(p).read())
    new File(p).delete()
}
Output
INFO  [db] connected host=localhost
WARN  [db] slow query host=localhost
ERROR [db] query failed: IOError: disk full host=localhost
  • Log.info(...) and friends use a shared logger that writes to standard error, like 2024-05-01 12:00:00 INFO server started.
  • Log.named(name) creates a logger whose lines carry the name; with(key, value) makes a copy that adds key=value to every line, for request ids and the like.
  • The level starts at Info, or at the GRAV_LOG environment variable (debug, info, warn, error, off); change it with Log.setLevel or the level field.
  • toFile(path) appends to a file. Each line is written whole and flushed, so lines from different threads never interleave.

API

enum LogLevel

How important a log message is. A logger shows messages at its level and above.

Values

  • LogLevel.Debug
  • LogLevel.Info
  • LogLevel.Warn
  • LogLevel.Error
  • LogLevel.Off

class Logger

A named logger with its own level, destination and fields.

Constructors

  • new Logger(name: string) — A logger writing to standard error at the default level.

Fields

  • readonly name: string — The logger's name, shown in brackets ("" for none).
  • level: LogLevel — Messages below this level are skipped.
  • timestamps: bool — Whether lines start with the local date and time (default true).

Methods

  • fn toFile(path: string) -> Logger — Also send this logger's lines to the file at path (appending) instead of standard error; IOError when it cannot be opened.
  • fn withLevel(l: LogLevel) -> Logger — Sets the level; returns the logger.
  • fn with(key: string, value: any) -> Logger — A copy of this logger that adds key=value to every line (same level and destination).
  • fn enabled(l: LogLevel) -> bool — Whether a message at l would be written.
  • fn debug(message: string) — Logs at Debug level.
  • fn info(message: string) — Logs at Info level.
  • fn warn(message: string) — Logs at Warn level.
  • fn error(message: string) — Logs at Error level.
  • fn error(message: string, e: Exception) — Logs an exception at Error level: the message, then the exception's describe().
  • fn write(l: LogLevel, message: string) — Writes one line at level l (when enabled).

class Log

The shared logger, and helpers to create named ones.

Static members

  • static fn root() -> Logger — The shared logger used by Log.info and friends.
  • static fn named(name: string) -> Logger — A new logger with a name, starting at the default level.
  • static fn defaultLevel() -> LogLevel — The level a new logger starts at: GRAV_LOG (debug, info, warn, error, off) or Info.
  • static fn parseLevel(name: string, fallback: LogLevel) -> LogLevel — The level a name stands for ("debug", "info", "warn"/"warning", "error", "off"; any case), or fallback.
  • static fn setLevel(l: LogLevel) — Sets the shared logger's level.
  • static fn toFile(path: string) — Sends the shared logger's lines to a file (appending).
  • static fn debug(message: string) — Logs at Debug level on the shared logger.
  • static fn info(message: string) — Logs at Info level on the shared logger.
  • static fn warn(message: string) — Logs at Warn level on the shared logger.
  • static fn error(message: string) — Logs at Error level on the shared logger.

Edit this page on GitHub