log
Recording what a program does, with levels, a prefix, structured fields, JSON output and writing to a file.
import log
For a program running on a server, println is not enough: you need to know how important each message is and when it was written, and to be able to silence the unimportant ones. The log package has four levels, debug, info, warn and error, and records only the messages at or above the level you set; the default level is info.
By default the log goes to standard error with a timestamp. The examples below turn the time off so their output is stable.
Constants and variables
LevelDebug
const LevelDebug: i32 = 0import log
func main:
log.ToStdout()
log.ShowTime(false)
log.SetPrefix("[app]")
log.Debug("this is not shown")
log.Info("the server is ready")
log.Warn("memory is running low")
log.Error("the connection dropped")
log.SetLevel(log.LevelDebug)
log.Debug("now it is shown")
log.Log(log.LevelWarn, "at a level of my choosing")
println log.GetLevel(), log.LevelName(log.LevelError), log.ParseLevel("warn"), log.LevelInfo
endLevelInfo
const LevelInfo: i32 = 1The example for this item is shown with the ones above: log-Info.salam
LevelWarn
const LevelWarn: i32 = 2The example for this item is shown with the ones above: log-Info.salam
LevelError
const LevelError: i32 = 3The example for this item is shown with the ones above: log-Info.salam
FormatText
const FormatText: i32 = 0import log
func main:
log.ToStdout()
log.ShowTime(false)
mut keys := Vector {} as Vector<str>
mut values := Vector {} as Vector<str>
defer keys.free()
defer values.free()
keys.push("user")
values.push("sara")
keys.push("ms")
values.push("42")
log.InfoKV("request served", keys, values)
log.WarnKV("it was slow", keys, values)
log.ErrorKV("it failed", keys, values)
log.SetLevel(log.LevelDebug)
log.DebugKV("the details", keys, values)
log.SetFormat(log.FormatJSON)
log.Info("as JSON")
println log.GetFormat() == log.FormatJSON, log.FormatText
mut fields := Vector {} as Vector<str>
defer fields.free()
fields.push(log.Field("path", "/login"))
fields.push(log.FieldInt("status", 200))
fields.push(log.FieldBool("cached", false))
println log.KV("response", fields)
endFormatJSON
const FormatJSON: i32 = 1The example for this item is shown with the ones above: log-InfoKV.salam
Functions
LevelName
func LevelName(l: int): strThe example for this item is shown with the ones above: log-Info.salam
ParseLevel
func ParseLevel(s: str): int"warn" into a level, for reading the level from a configuration file.The example for this item is shown with the ones above: log-Info.salam
SetLevel
func SetLevel(l: int)The example for this item is shown with the ones above: log-Info.salam
GetLevel
func GetLevel(): intThe example for this item is shown with the ones above: log-Info.salam
SetPrefix
func SetPrefix(p: str)The example for this item is shown with the ones above: log-Info.salam
ShowTime
func ShowTime(b: bool)The example for this item is shown with the ones above: log-Info.salam
SetFormat
func SetFormat(f: int)The example for this item is shown with the ones above: log-InfoKV.salam
GetFormat
func GetFormat(): intThe example for this item is shown with the ones above: log-InfoKV.salam
SetRotation
func SetRotation(max_bytes: i64, max_backups: int)max_bytes it is set aside, and at most max_backups old copies are kept.import log
import io
func main:
log.ShowTime(false)
println log.ToFile("app.log")
log.SetRotation(1024 * 1024, 3)
log.Info("written to the file")
log.CloseFile()
log.ToStderr()
print io.ReadFile("app.log")
endToStderr
func ToStderr()The example for this item is shown with the ones above: log-ToFile.salam
ToStdout
func ToStdout()The example for this item is shown with the ones above: log-Info.salam
ToFile
func ToFile(path: str): intThe example for this item is shown with the ones above: log-ToFile.salam
CloseFile
func CloseFile()The example for this item is shown with the ones above: log-ToFile.salam
Log
func Log(l: int, msg: str)The example for this item is shown with the ones above: log-Info.salam
Debug
func Debug(msg: str)The example for this item is shown with the ones above: log-Info.salam
Info
func Info(msg: str)The example for this item is shown with the ones above: log-Info.salam
Warn
func Warn(msg: str)The example for this item is shown with the ones above: log-Info.salam
Error
func Error(msg: str)The example for this item is shown with the ones above: log-Info.salam
Debugf
func Debugf(tmpl: str, args: Vector<str>){} is filled with the next value.import log
func main:
log.ToStdout()
log.ShowTime(false)
mut args := Vector {} as Vector<str>
defer args.free()
args.push("Sara")
args.push("3")
log.Infof("user {} signed in and has {} messages", args)
log.Warnf("a failed attempt for {}", args)
log.Errorf("user {} was blocked", args)
log.SetLevel(log.LevelDebug)
log.Debugf("id {}", args)
endInfof
func Infof(tmpl: str, args: Vector<str>)The example for this item is shown with the ones above: log-Infof.salam
Warnf
func Warnf(tmpl: str, args: Vector<str>)The example for this item is shown with the ones above: log-Infof.salam
Errorf
func Errorf(tmpl: str, args: Vector<str>)The example for this item is shown with the ones above: log-Infof.salam
Fatal
func Fatal(msg: str)import log
func main:
log.Fatal("the configuration file was not found")
endField
func Field(key: str, val: str): strkey=value field.The example for this item is shown with the ones above: log-InfoKV.salam
FieldInt
func FieldInt(key: str, val: i64): strThe example for this item is shown with the ones above: log-InfoKV.salam
FieldBool
func FieldBool(key: str, val: bool): strThe example for this item is shown with the ones above: log-InfoKV.salam
KV
func KV(msg: str, fields: Vector<str>): strThe example for this item is shown with the ones above: log-InfoKV.salam
DebugKV
func DebugKV(msg: str, keys: Vector<str>, vals: Vector<str>)The example for this item is shown with the ones above: log-InfoKV.salam
InfoKV
func InfoKV(msg: str, keys: Vector<str>, vals: Vector<str>)The example for this item is shown with the ones above: log-InfoKV.salam
WarnKV
func WarnKV(msg: str, keys: Vector<str>, vals: Vector<str>)The example for this item is shown with the ones above: log-InfoKV.salam
ErrorKV
func ErrorKV(msg: str, keys: Vector<str>, vals: Vector<str>)The example for this item is shown with the ones above: log-InfoKV.salam