Reputation: 6013
I have set up a log utility module using Pino in a KeystoneJS/NodeJS project. It works fine, but I want to limit the log level when it goes into production.
The utility file, util/log.js
is set up
import pino from 'pino';
const logger = pino({
prettyPrint: { colorize: true }
});
export default logger;
and imported into files (such as here index.ts
)
import log from './util/log';
but when I try to set the level (here I set it to 'development', to test that this works, later it will be 'production')
if (process.env.NODE_ENV === 'development') log.level('error');
I get the following error
TypeError: log_1.default.level is not a function
at Object.<anonymous> (/Users/abc/Documents/projects/yaa-cms-new/tsout/index.js:29:19)
However all calls to log.error
or log.info
work normally throughout the application, that is, it's importing Pino correctly and I can use it. I just can't set the level. When I trace out the imported log
object I get
[1589881349511] INFO (18641 on AGOMA1002.local):
levels: {
"labels": {
"10": "trace",
"20": "debug",
"30": "info",
"40": "warn",
"50": "error",
"60": "fatal"
},
"values": {
"trace": 10,
"debug": 20,
"info": 30,
"warn": 40,
"error": 50,
"fatal": 60
}
}
I'm not sure where I'm going wrong -- any clues much appreciated!
Using Node 12.14.0, the latest TypeScript and
"pino": "^6.2.1",
"pino-pretty": "^4.0.0"
Upvotes: 4
Views: 6103
Reputation: 674
Use
log.level = 'trace'
or use environment variables:
const logger = require('pino')({
level: process.env.LOG_LEVEL,
prettyPrint: true
});
Upvotes: 1
Reputation: 6013
As @Manuel Spigolon correctly points out above, the correct way to set the Pino level is
log.level = 'error'
because logger.level
is a getter/setter (see https://getpino.io/#/docs/api?id=loggerlevel-string-gettersetter), which I knew, but which I somehow failed to process.
Upvotes: 3