Logging
WunderGraph includes built-in support for structured logging, using the RequestLogger
interface.
This interface provides logging through functions with the given prototype:
This means every logging function accepts a string
as a first argument that represents the log message, optionally followed by an object with additional properties to be logged as JSON. For example, calling logger.debug('hello', { a: 1, b: 2 });
will produce {"level":"debug","a":1,"b":2,"msg":"hello"}
Error handling
If the second argument passed to a logging function is an Error
or if it contains an error
property, it will be interpreted as an error to be unwrapped, logging its message and stack too.
Attaching additional properties to a RequestLogger
RequestLogger
implements also a withFields()
function, which allows creating a new logger with the same level but with additional properties attached, avoiding repeating the same arguments multiple times. This means, these two blocks are equivalent:
Output formats
All logs are produced in JSON. During development (with wunderctl up
) logs are provided with colorized output based on their levels. In production (with wunderctl start
) logs are printed to the standard output as JSON formatted lines.
Availability
Functions
In functions the WunderGraph RequestLogger
is available as part of the context:
Hooks
In the same as functions, RequestLogger
is available through the context:
Webhooks
In webhooks, the RequestLogger
is part of the context:
Builtin GraphQL server
In GraphQL handlers, the RequestLogger
can be accessed via the ctx.wundergraph
property: