Logging
Hub uses pino for structured logging.
Structured Logging
Hub uses structured logging, which means log data is output as JSON (when not pretty-printed). This makes logs easier to parse and analyze in production environments.
Example:
// Instead of string concatenation
logger.info(`User ${userId} performed action ${action}`);
// Use structured data
logger.info({ userId, action }, "User performed action");
Env var: LOG_LEVEL
Here are the available log levels:
trace
:logger.trace(...)
debug
:logger.debug(...)
info
:logger.info(...)
- defaultwarn
:logger.warn(...)
error
:logger.error(...)
silent
: silence all logs
You can set the log level with the LOG_LEVEL
environment variable. e.g. LOG_LEVEL=debug
.
The log level determines which logs are printed. So if you set the log level to info
, then trace
and debug
logs are not printed but everything above info
is printed.
The default log level is info
.
Env var: LOG_PRETTY
You can set LOG_PRETTY=true
or LOG_PRETTY=false
to force pretty-printing or not. If not, logs are printed in JSON format.
Ensure that pino-pretty is installed if you want pretty-printing; it’s useful for development since it’s more human-readable.
By default, hub pretty-prints logs when NODE_ENV
is not production
and the pino-pretty package is installed.
If you set LOG_PRETTY=true
but pino-pretty is not installed, then hub falls back to default JSON logging.
Adding your own logging
Consult pino’s documentation to learn more about its logging API.
But the important thing to know is that you can optionally pass in an object as the first argument and it will be merged into the log object. The message string appears in the object as a msg
property.
import { logger } from "@moneypot/hub/logger";
logger.debug("hello world");
logger.debug({ foo: "bar" }, "hello world");
logger.info("example of an info log");
logger.warn("example of a warn log");
logger.error(error, "example of an error log");