Logging
boxes and glue uses the standard library’s slog package for structured logging.
Enable logging
boxes and glue starts with logging disabled. It uses the bag.Logger as a logger.
To enable logging, you must create a log handler:
myLogger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
bag.SetLogger(myLogger)for JSON output and
myLogger := slog.New(slog.NewTextHandler(os.Stdout, nil))
bag.SetLogger(myLogger)for text output. The bag.SetLogger() function makes sure that the called libraries use the same logger: it sets bag.Logger and the logger of baseline-pdf, the PDF writer, which reports loading and writing fonts and images. Assigning bag.Logger directly does not reach baseline-pdf, so its messages do not show up in your log.
Documents can be made concurrently, each with its own PDFDocument, and they share these loggers. Set the logger once, before you make documents.
Using the logger
The logger can be used like this:
bag.Logger.Info("Read font file", "filename", myfontfile)which outputs (in JSON)
{
"time":"2023-07-16T16:42:59.619968+02:00",
"level":"INFO",
"msg":"Read font file",
"filename":"MinionPro-Regular.otf"
}You can use bag.Logger to set the default logger:
slog.SetDefault(bag.Logger)Changing the log level
Instead of setting nil as the handler option in NewJSONHandler and NewTextHandler you can set the log level:
&slog.HandlerOptions{Level: slog.LevelWarn}this is a complete example for setting the logger with a log level warn:
myLogger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelWarn}))
bag.SetLogger(myLogger)