import ReplaceDatasetToken from "/snippets/replace-dataset-token.mdx" import Prerequisites from "/snippets/standard-prerequisites.mdx"
Install SDK
To install the SDK, run the following:
npm install @axiomhq/winstonImport 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: 'DATASET_NAME',
token: 'API_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----
});Examples
For more examples, see the examples in GitHub.