I am trying to use pino library but I am getting error
My code
I created a logger.js file and imported pino from node_module and added transport of pino-pretty.
logger.js
import pino from "pino";
const logger = pino({
transport: {
target: "pino-pretty",
options: {
colorize: true,
},
},
});
export default logger;
I created a database file and imported pino from logger file and used info function to display my error.
database.js
import mongoose from "mongoose";
import logger from "./logger";
const DB_CONNECTION_STRING =
process.env.DB_CONNECTION_STRING ||
"mongodb://localhost:27017/*******";
try {
await mongoose.connect(DB_CONNECTION_STRING);
logger.info("Connect to database");
} catch (e) {
logger.error(e, "Failed to connect to database. Goodbye");
process.exit(1);
}

You can use it as a stream if it doesn't work with the transport option:
import pino from "pino";
import pretty from "pino-pretty";
const stream = pretty({
levelFirst: true,
colorize: true,
ignore: "time,hostname,pid",
});
const logger = pino(
{
name: "MyLogger",
level: process.env.NODE_ENV === "development" ? "debug" : "info",
},
stream
);
export default logger;
More details in the docs: https://github.com/pinojs/pino-pretty#usage-as-a-stream
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With