Prerequisites
Install SDK
To install the SDK, run the following:
npm install @axiomhq/winston
Import the Axiom transport for Winston
import { WinstonTransport as AxiomTransport } from '@axiomhq/winston';
Create a Winston logger instance
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
// You can pass an option here. If you don’t, the transport is configured automatically
// using environment variables like `AXIOM_DATASET` and `AXIOM_TOKEN`
new AxiomTransport({
dataset: 'my-dataset',
token: 'my-token',
}),
],
});
After setting up the Axiom transport for Winston, use the logger as usual:
logger.log({
level: 'info',
message: 'Logger successfully setup',
});
Error, exception, and rejection handling
To log errors, use the winston.format.errors
formatter. For example:
import winston from 'winston';
import { WinstonTransport as AxiomTransport } from '@axiomhq/winston';
const { combine, errors, stack } = winston.format;
const axiomTransport = new AxiomTransport({ ... });
const logger = winston.createLogger({
// 8<----snip----
format: combine(errors({ stack: true }), json()),
// 8<----snip----
});
To automatically log exceptions and rejections, add the Axiom transport to the exceptionHandlers
and rejectionHandlers
. For example:
import winston from 'winston';
import { WinstonTransport as AxiomTransport } from '@axiomhq/winston';
const axiomTransport = new AxiomTransport({ ... });
const logger = winston.createLogger({
// 8<----snip----
transports: [axiomTransport],
exceptionHandlers: [axiomTransport],
rejectionHandlers: [axiomTransport],
// 8<----snip----
});
Running on Edge runtime isn’t supported.
Examples
For more examples, see the examples in GitHub.