Basic Logging
LogLayer provides a simple and consistent API for logging messages at different severity levels. This guide covers the basics of logging messages.
Log Levels
LogLayer supports six standard log levels, each with its own method:
info()
- For general information messageswarn()
- For warning messageserror()
- For error messagesdebug()
- For debug informationtrace()
- For detailed debugging informationfatal()
- For critical errors that require immediate attention
INFO
Some logging libraries may not support all levels. In such cases:
trace
is mapped todebug
fatal
is mapped toerror
Basic Message Logging
The simplest way to log a message is to use one of the log level methods:
// Basic info message
log.info('User logged in successfully')
// Warning message
log.warn('API rate limit approaching')
// Error message
log.error('Failed to connect to database')
// Debug message
log.debug('Processing request payload')
// Trace message (detailed debugging)
log.trace('Entering authentication function')
// Fatal message (critical errors)
log.fatal('System out of memory')
Message Parameters
All log methods accept multiple parameters, which can be strings, numbers, null, or undefined:
// Multiple parameters
log.info('User', 123, 'logged in')
// With string formatting (if supported by the logging library)
log.info('User %s logged in from %s', 'john', 'localhost')
WARNING
- Support for multiple parameters depends on your logging library. Some libraries may only use the first parameter.
- It's generally not recommended to use multiple parameters for log messages. Especially if you are going to use multiple logging transports as some may not support it.
Message Prefixing
You can add a prefix to all log messages either through configuration or using the withPrefix
method:
// Via configuration
const log = new LogLayer({
prefix: '[MyApp]',
transport: new ConsoleTransport({
logger: console
})
})
// Via method
const prefixedLogger = log.withPrefix('[MyApp]')
// Output: "[MyApp] User logged in"
prefixedLogger.info('User logged in')
Enabling/Disabling Logging
You can control whether logs are output using these methods:
// Disable all logging
log.disableLogging()
// Enable logging again
log.enableLogging()
You can also configure this through the initial configuration:
const log = new LogLayer({
enabled: false, // Starts with logging disabled
transport: new ConsoleTransport({
logger: console
})
})
Transport Instance
You can get the underlying logger for a transport if you've assigned an ID to it:
const log = new LogLayer({
transport: new ConsoleTransport({
logger: console,
id: 'console'
})
})
const consoleLogger = log.getLoggerInstance<typeof console>('console')
import { type P, pino } from "pino";
import { PinoTransport } from "@loglayer/transport-pino";
const log = new LogLayer({
transport: new PinoTransport({
logger: pino(),
id: 'pino'
})
})
const pinoLogger = log.getLoggerInstance<P.Pino>('pino')