log API reference
Leveled, structured logging. A log.Logger carries a minimum level (debug < info < warn < error), an output format (text / logfmt / json), and a sink; log.info(logger, message, fields) (and the sibling levels) render one record - a timestamp, the level, the message, and the caller's key/value fields - and write it, dropping records below the logger's level.
Sinks: stdout / stderr (both binaries, via io), a file (append, via fs), and an RFC 5424 syslog sink over UDP (net, so the syslog sink needs the default jennifer binary; the console / file sinks work on both). Over io / fs + json + strings + time + os (+ net for syslog).
Import with import "log.j" as log;. See the log guide for prose and examples.
Contents
| Name | What it is |
|---|---|
at | Emit a record at an explicit level (skipped if below the logger's level). |
debug | Emit a debug record. |
error | Emit an error record. |
fatal | Emit a fatal record and then terminate the program with exit code 1. |
info | Emit an info record. |
new | A logger writing to standard output. |
toFile | A logger appending to a file (created if missing). |
toStderr | A logger writing to standard error. |
toSyslog | A logger sending RFC 5424 records to a syslog server over UDP. |
warn | Emit a warn record. |
with | A child logger carrying persistent context fields. |
Logger | struct: A value-semantic logger configuration. |
Functions
at
log.at(
logger as Logger,
level as string,
message as string,
fields as map of string to string)Emit a record at an explicit level (skipped if below the logger's level).
logger- the logger
level- "debug", "info", "warn", or "error"
message- the log message
fields- structured key/value fields ({} for none)
- Throws
Error - kind "log" on an unknown level; a positioned
fs/neterror on a sink failure
debug
log.debug(
logger as Logger,
message as string,
fields as map of string to string)Emit a debug record.
logger- the logger
message- the log message
fields- structured fields ({} for none)
error
log.error(
logger as Logger,
message as string,
fields as map of string to string)Emit an error record.
logger- the logger
message- the log message
fields- structured fields ({} for none)
fatal
log.fatal(
logger as Logger,
message as string,
fields as map of string to string)Emit a fatal record and then terminate the program with exit code 1. The "fatal" level ranks above every other, so the record is always emitted regardless of the logger's minimum level.
logger- the logger
message- the log message
fields- structured fields ({} for none)
info
log.info(
logger as Logger,
message as string,
fields as map of string to string)Emit an info record.
logger- the logger
message- the log message
fields- structured fields ({} for none)
new
log.new(format as string, level as string)A logger writing to standard output.
format- "text", "logfmt", or "json"
level- the minimum level ("debug"/"info"/"warn"/"error")
- Returns
Logger - the logger
- Throws
Error - kind "log" on an unknown format or level
toFile
log.toFile(format as string, level as string, path as string)A logger appending to a file (created if missing).
format- "text", "logfmt", or "json"
level- the minimum level
path- the file path
- Returns
Logger - the logger
- Throws
Error - kind "log" on an unknown format or level
toStderr
log.toStderr(format as string, level as string)A logger writing to standard error.
format- "text", "logfmt", or "json"
level- the minimum level
- Returns
Logger - the logger
- Throws
Error - kind "log" on an unknown format or level
toSyslog
log.toSyslog(level as string, address as string, app as string)A logger sending RFC 5424 records to a syslog server over UDP. Needs the default jennifer binary (net).
level- the minimum level
address- the syslog server "host:port" (e.g. "localhost:514")
app- the APP-NAME tag
- Returns
Logger - the logger
- Throws
Error - kind "log" on an unknown level
warn
log.warn(
logger as Logger,
message as string,
fields as map of string to string)Emit a warn record.
logger- the logger
message- the log message
fields- structured fields ({} for none)
with
log.with(logger as Logger, fields as map of string to string)A child logger carrying persistent context fields. Every record emitted through the returned logger includes these fields, merged with any per-call fields (a per-call key of the same name wins). Composes: calling with again on the result merges the new fields on top of the carried ones. The parent logger is unchanged (value semantics).
logger- the parent logger
fields- the persistent fields to attach
- Returns
Logger - a new logger with the merged persistent fields
Structs
Logger
A value-semantic logger configuration. Build one with new / toStderr / toFile / toSyslog.
def struct Logger {
level as string,
format as string,
sink as string,
target as string,
app as string,
fields as map of string to string
};level- the minimum level to emit: "debug", "info", "warn", or "error"
format- the record format: "text", "logfmt", or "json" (ignored by the syslog sink)
sink- where records go: "stdout", "stderr", "file", or "syslog"
target- the file path (file sink) or "host:port" (syslog sink); "" for a console sink
app- an application / tag name (the syslog APP-NAME; "" means none)
fields- persistent context fields attached to every record (see
with); {} for none