ArrchanaMohan
ArrchanaMohan

Reputation: 2566

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

Upvotes: 4

Views: 17866

Answers (5)

migdsb
migdsb

Reputation: 705

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.

Upvotes: 11

M. Hamza Rajput
M. Hamza Rajput

Reputation: 10276

Working fine on pino-pretty version 10.0.1

Just found a new way to format timestamp

const pino = require('pino');
const stream = pretty({
    colorize: true,
    translateTime: "SYS:standard"
}) 

const logger = pino({
    level: process.env.LOG_LEVEL || 'debug',
}, stream)

logger.info('hi')

Output

[2023-07-15 00:29:42.039 +0500] INFO (9088): hi

Upvotes: 3

chalda
chalda

Reputation: 722

Based on the https://github.com/pinojs/pino-pretty documentation for version 9.x I found my way to print ISO UTC format with prettyPrint

import { pino, Logger } from 'pino'
const logger: Logger = pino({
  transport: {
    target: 'pino-pretty',
    options: {
      translateTime: 'UTC:yyyy-mm-dd HH:MM:ss.l o',
    }
  },
  level: 'info',
})

Upvotes: 1

PirateApp
PirateApp

Reputation: 6220

2023 update

// logger.js
import pino from 'pino';

// Create a logging instance
export const logger = pino({
  enabled: process.env.LOG_ENABLED === 'true',
  formatters: {
    level: (label) => {
      return { level: label };
    },
  },
  level: process.env.LOG_LEVEL || 'info',
  name: process.env.LOGGER_NAME,
  redact: {
    paths: ['email', 'password', 'token'],
  },
  // https://github.com/pinojs/pino/issues/674
  timestamp: pino.stdTimeFunctions.isoTime,
});

We need a timestamp in ISO 8601 time format which is described here We dont want level numbers like 30 for info instead we want labels which is why I added a formatter

Upvotes: 10

ArrchanaMohan
ArrchanaMohan

Reputation: 2566

I just changed the below details in the pino.js file and It now return the user readable format of time stamp

const defaultOptions = {

timestamp:isoTime

}

const time = (timestamp instanceof Function)
    ? timestamp : (timestamp ? isoTime : nullTime)
  const timeSliceIndex = time().indexOf(':') + 1

time.js file we have following value for isoTime

const isoTime = () => `,"time":"${new Date(Date.now()).toISOString()}"`

It returned the below outcome which is expected:

["2020-05-15T12:10:03.955Z"] INFO (00020 on L-MAA-13i28828820): Flow---XXXX

Upvotes: 0

Related Questions