javascriptnode.jspinojs

How to make the pino library as generic and call everywhere in the project


I'm very new to node.js and JS world. I'm doing logger into the existing node.js based testing application. I just picked 'pino' and using it as like below:

const pino = require('pino');
const logger = pino({
    prettyPrint: true
  });

logger.info("Flow---XXXX");

It returned below response when I run the npm run test | pino-pretty -c -t

Output:

[1589538447177] INFO (00020 on L-MAA-13i28828820): Flow---XXXX

I have two questions here:

  1. I set -t flag in the command and I want it to convert the time stamp to user understanding format (like yyyy-mm-dd HH:mm:ss) but still it prints epoch format as look like above.

  2. I'm trying to add the below lines into all the JS files in the project. Is it possible to call it some JS file and export the logger object into other js files? It's like instead of calling the below lines everywhere in the project, can I create reusable js file and export the function?

  const pino = require('pino');
    const logger = pino({
        prettyPrint: true
      });

I'm not sure whether the 2 request is possible to achieve but It would be great if I get some leads..

Thanks in advance.

Updates:

I resolved the first question by using module.export option as like below:

 module.export.log = function(input)
    {

    logger.info(input);
    }

Can someone help me to display the user readable format time in the log? Right now It prints epoch.. I tried below code but still no luck.

const logger = pino({
    prettyPrint: true,
    timestamp:`,"time":"${new Date(Date.now())}"`
  });

I can see there is const variable in the pino library called timestamp and default value is assigned as epoch time.

I will post if I find any solution


Solution

  • The correct way of logging a useful time representation before 2022 was to configure pino as given below:

    const logger = pino({
        name: __filename,
        level: process.env.LOG_LEVEL || 'debug',
        prettyPrint: {
            colorize: true,
            translateTime: 'SYS:standard',
            ignore: 'hostname,pid',
        }
    });
    

    The above is actually in the pino-logger package. The setting that does the trick is prettyPrint.translateTime.