pino-template
v1.0.4
Published
An Eta template transport for the pino logging library
Readme
An Eta template transport for the pino logging library
About The Project
This is a pipeline transport for the pino.js logging library, which allows you to transform the logging output by applying an Eta template.
For those unfamiliar with Eta, perhaps you've encountered EJS. Eta templates are very similar to EJS templates.
e.g.
template
<%
// extract the context and this log message from `it`
const {data: d, context: ctx} = it;
// get the name of the level from the mapping provided as context
const levelName = (ctx?.[d.level] ?? d.level).toUpperCase(); // e.g. "INFO"
%><%=
// output our formatted log message as a string
`${
new Date(d.time)
.toISOString()
.substring(0, 19)
} - ${
levelName
} - ${
d.msg
}`
%>input
{"levelName":"info","level":30,"time":1531171074631,"msg":"hello world","pid":657,"hostname":"Davids-MBP-3.fritz.box"}result
2018-07-09T21:17:54 - INFO - hello worldBuilt With
Getting Started
Prerequisites
While this project has no dependencies, it does have the following peerDependencies:
pino@^10.1.0
npm install "pino@^10.1.0" --saveeta@^4.0.1
npm install "eta@^4.0.1" --saveNode versions: This project is tested on Node
24.12.0.ES Module: This project is only available as an ES module (a.k.a. "ESM") i.e. it probably won't work in CommonJS, except perhaps using dynamic import.
Installation
- Ensure you've installed the prerequisites above.
- Install this NPM package (pino-template):
npm install pino-template --save
Usage
Combine pino-template (this module) as part of a pino transport pipeline.
Options
| option | description | example |
| ----------------- | ---------------------------------------------------------------------------------------- | -------------------------------------------------- |
| template | The Eta template as a string. Each log message will be available as it.data. Required. | <%= it.data.msg + it.context.foo %> |
| templateContext | An object which will be made available to the Eta template as it.context . Optional. | {"foo":"bar"} |
| templateOptions | Options which will be passed to the Eta constructor . Optional. | {"debug":false,"cache":false,"autoEscape":false} |
Example
The example below adds an extra "levelName" property to each JSON log line i.e. it changes this:
{"level":30,"time":1531171074631,"msg":"hello world","pid":657,"hostname":"Davids-MBP-3.fritz.box"}into this:
{"levelName":"info","level":30,"time":1531171074631,"msg":"hello world","pid":657,"hostname":"Davids-MBP-3.fritz.box"}import pino from 'pino'
const logger = pino({
transport: {
pipeline: [{
target: 'pino-template',
options: {
// Provide the Eta template here as the "template" property value. The `"it.data"`
// object contains the log data (i.e. level, time, msg, hostname etc); while
// the `"it.context"` object contains whatever you provide as the
// `"templateContext"` property below
template: `<%
const {context, data} = it;
%><%=
JSON.stringify({...data, "levelName":context?.levelMapping?.[data.level] ?? data.level})
%>`,
templateContext: { levelMapping: pino.levels.labels },
},
}, {
// Use target: 'pino/file' with STDOUT descriptor 1 to write
// logs without any change.
target: 'pino/file',
options: { destination: 1 }
}]
}
})
logger.info('hello world')License
Distributed under the MIT license. See LICENSE.txt for more information.
