Deba
Deba

Reputation: 989

Unable to write the logs to file using Pino logger in NodeJS

I have an application which has two major modules. One is ui-component and another is service-component. ui-component uses winston logger and service-component uses pino logger. The link for pino logger is https://getpino.io/#/. I tried with the following code, but I could not see the log file, even the log file is not getting generated. The service-component is used as a node module inside ui-component which uses electron, angular 8 and NodeJs. When I run the command yarn start, the application will run and I do some validations to see the logs in the log file.

Please help me , I am new to NodeJs, Pino. Is is possible that two different logger implementation as in this will create any conflict in NodeJs application?

//import pino from "pino";
/*const dest = pino.extreme();
export const logger = pino(dest);*/

/*const dest = pino.destination('./logs/log')
export const logger = pino({ level: 'info' }, dest)*/


export const logger = require('pino')()
const tee = require('pino-tee')
const fs = require('fs')
const stream = tee(process.stdin)
stream.tee(fs.createWriteStream('myLogFile'), line => line.level >= 0)
stream.pipe(process.stdout)

logger.info('hello world')
logger.error('this is at error level')

Upvotes: 8

Views: 18747

Answers (1)

Jayantha
Jayantha

Reputation: 2349

This is ts solution for saving Pino logs in a log file.

npm i pino, pino-pretty

install pino and pino-pretty

import pino from "pino";

const logger = pino(
  {
    prettyPrint: {
      colorize: true,
      levelFirst: true,
      translateTime: "yyyy-dd-mm, h:MM:ss TT",
    },
  },
  pino.destination("./pino-logger.log")
);


logger.info('hi');

Upvotes: 5

Related Questions