Noel's opinionated logging transport for Pino
@augu/pino-transport is an opinionated logging transport for the Pino logging library. This was made to not repeat what this library entails. If you like it, then install it with:
$ npm i @augu/pino-transport
$ yarn add @augu/pino-transport
$ pnpm i @augu/pino-transport
- The library expects you to use
msg
instead anything set in thename
options forpino()
.
import pino from 'pino';
const log = pino({
transports: [
{
target: '@augu/pino-transport',
options: {
json: true
}
}
]
});
log.info('Hello, world!');
You can create custom transports that the transport will transform the logs to. You will need to create a second file that will be serialized to the proper value instead of a plain object; related issue (pinojs/pino#262)
// transport.js
import noelPino, { BaseFormatter, type LogRecord } from '@augu/pino-transport';
class MyFormatter extends BaseFormatter {
override transform(record: LogRecord) {
return record.msg;
}
}
export default (options) =>
noelPino({
...options,
transport: new MyFormatter()
});
// main.js
import pino from 'pino';
const log = pino({
transport: {
target: './transport.js'
}
});
log.info('Hello, world!');
// => "Hello, world!" is printed instead
This library also comes with custom serializers that I recommend setting in the serializers
option when creating a root Pino logger since it will work better with the Default and Json formatters.
import { serializers } from '@augu/pino-transport';
import pino from 'pino';
const log = pino({
serializers: {
err: serializers.createErrorSerializer(),
req: serializers.request,
res: serializers.response
}
});
log.info({ err: new Error('woof') }, 'waff');
In 1.3.0, the library provides a createSerializers
method to create serializers type-safely:
import { createSerializers } from '@augu/pino-transport';
import pino from 'pino';
const log = pino({
serializers: createSerializers({
// Enables the request serializer for `request` from the log record.
request: false,
// Enables the response serializer for `response` from the log record.
response: false,
// Enables the error serializer for `error` from the log record.
error: true,
// List of easier-to-write names when passing in from the log
// record.
allow: {
req: false,
res: false,
err: true
}
})
});
log.info({ err: new Error('woof') }, 'waff');
@augu/pino-transport is released under the MIT License with love by Noel!