cromo La guida di Cromo
Scarica PDF EN

std.log

The log of a program: lines with the time, a level and a message, on the standard output or error, at the end of a file or both, as text with colors or as JSON for the services.

cromo
import std.log

log.info("listening on {}", port)           // 2026-11-12T10:03:12.250+01:00 INFO  listening on 8080

log.Logger service
service.setLevel(log.Level.Debug)           // debug() writes too
service.setFile("service.log")              // at the end of that file too
service.setConsole(log.Console.Off)         // and no more on the standard output
service.setJson(true)                       // {"time":"...","level":"INFO","message":"..."}
service.info("started")

log.Logger request = service.with("request", id).withInt("user", 42)
request.warn("slow answer")                 // ... "message":"slow answer","request":"7f3a","user":42}

The guide: Files, processes and time.

Generata dalla documentazione nei sorgenti della libreria, che è in inglese.

#Tipi

#Level

cromo
enum Level

How much a line of the log matters.

  • Debug: the details that serve who looks for a problem
  • Info: what the program does: the level of a logger that is told nothing else
  • Warn: something to look at, which did not stop the work
  • Error: something that failed

#Console

cromo
enum Console

Where a logger writes on the terminal of the program.

  • Output: the standard output: where a logger that is told nothing else writes
  • Error: the standard error
  • Off: nowhere: the lines go to the file alone

#Logger

cromo
struct Logger

Writes the lines of a log: to the standard output or error, to a file or to both, from a level on, as text or as JSON, with some fields of its own after the message. One declared without a value writes text to the standard output from Info on, with colors when that is a terminal. It is a value: a copy has what the logger had when it was copied, and is given to the functions and to the tasks that write.

#Logger.setLevel

cromo
void setLevel(Level level)

The lowest level that the logger writes, Info at the start: the lines below it are not written.

#Logger.setJson

cromo
void setJson(bool on)

With true the lines are objects of JSON, one for each line, with "time", "level", "message" and the fields of the logger, for the tools that collect the logs; with false they are text again.

#Logger.setConsole

cromo
void setConsole(Console where)

Where the lines go on the terminal: Console.Output, the standard output, at the start; Console.Error, the standard error; Console.Off, nowhere, for a logger that writes to its file alone.

#Logger.setFile

cromo
void setFile(string file)

The lines go also at the end of the file at file, created if it does not exist, and with "" to no file again. setConsole(Console.Off) leaves the file alone. The file is opened for each line, so a tool that rotates the logs can move it away at any time: the next line makes a new one. A line that cannot be written to the file goes to the standard error, if the terminal did not have it already.

#Logger.setColor

cromo
void setColor(bool on)

Whether the lines of text on the terminal have colors, true at the start: the time in grey, DEBUG in blue, INFO in green, WARN in orange and ERROR in red. The colors are written only when the standard output or error is a terminal: one sent to a file or to another program gets the text alone. The lines of JSON have no colors.

#Logger.setTimeFormat

cromo
void setTimeFormat(string pattern)

How the time of a line is written: a pattern of DateTime.format of std.time, as "%F %T" for 2026-11-12 10:03:12, without the zone, or "%T.%3f" for 10:03:12.250; with "", as at the start, the format of ISO 8601 with the milliseconds and the zone, 2026-11-12T10:03:12.250+01:00. A pattern with a letter that format does not know writes the time of ISO 8601.

#Logger.setFileColor

cromo
void setFileColor(bool on)

Whether the lines of text in the file have the colors too, false at the start: for a file that is read with a program that shows them, as less -R or tail -f in a terminal.

#Logger.with

cromo
Logger with(string key, string value) const

A logger as this one, with the field key with the text value too.

#Logger.withInt

cromo
Logger withInt(string key, i64 value) const

A logger as this one, with the field key with the number value too.

#Logger.debug

cromo
void debug(string message) const

Writes a line for who looks for a problem, when the level is Debug.

#Logger.info

cromo
void info(string message) const

Writes a line that says what the program does.

#Logger.warn

cromo
void warn(string message) const

Writes a line about something to look at.

#Logger.error

cromo
void error(string message) const

Writes a line about something that failed.

#Funzioni

#info

cromo
void info(string message)

Writes a line that says what the program does, on the standard output, as a Logger that is told nothing does. The message is a text, or a format with its arguments, as that of println: log.info("{} users", count).

#warn

cromo
void warn(string message)

Writes a line about something to look at, on the standard output.

#error

cromo
void error(string message)

Writes a line about something that failed, on the standard output.

#with

cromo
Logger with(string key, string value)

A logger that writes on the standard output, whose lines have the field key with the text value after the message: the request, the user, the part of the program that writes.

#withInt

cromo
Logger withInt(string key, i64 value)

A logger that writes on the standard output, whose lines have the field key with the number value.