Logging
Logging for node.js projects.
Adheres to the rules defined in https://confluence.open-xchange.com/pages/viewpage.action?spaceKey=EN&title=Logging
Works for cjs and esm.
In the current implementation it uses pino and pino-http.
How to use in ESM Projects
Create a file in your project e.g. logger.js
import { createLogger, createHttpLogger } from '@open-xchange/logging'
config()
export const httpLogger = createHttpLogger()
export const logger = createLogger()
How to use in CJS Projects
Create a file in your project e.g. logger.js
const { createLogger, createHttpLogger } = require('@open-xchange/logging')
module.exports = {
httpLogger: createHttpLogger(),
logger: createLogger()
}
If you use express you can add a request logger middleware.
app.use(httpLogger)
Pretty Printing - Programmatically
If NODE_ENV=development is set in your environment logs are automatically pretty-printed, which is particularly useful when debugging e.g. with Okteto.
Pretty Printing - CLI mode
CLI mode is useful in production environments. You may want to use this with kubectl logs -f
.
node index.js | pino-pretty -t 'SYS:mm/dd HH:MM:ss.l' -x fatal:0,error:3,warn:4,info:6,debug:7,trace:8 -X fatal:red,error:red,warn:yellow,info:green,debug:blue,trace:gray
You could also create a .pino-prettyrc in your current directory.
{
"translateTime": "SYS:mm/dd HH:MM:ss.l",
"customLevels": "fatal:0,error:3,warn:4,info:6,debug:7,trace:8",
"customColors": "fatal:red,error:red,warn:yellow,info:green,debug:blue,trace:gray"
}
which gives you a much shorter call:
node index.js | pino-pretty