You can use Winston logger to send logs to Axiom. First, install the winston and @axiomhq/winston packages, then create an instance of the logger with the AxiomTransport.
Install using npm install
:
npm install @axiomhq/winston
import the axiom transport for winston:
import { WinstonTransport as AxiomTransport } from '@axiomhq/winston';
create a winston logger instance with axiom transport:
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',
}),
],
});
then you can use the logger as usual:
logger.log({
level: 'info',
message: 'Logger successfully setup',
});
If you want to log Error
s, we recommend using the
winston.format.errors
formatter, for example like this:
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
like
this:
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----
});
For further examples, head over to the examples directory.
Distributed under the MIT License.