InterviewSolution
| 1. |
How to use a pino logger in an express app? |
|
Answer» Pino is a small and FAST logger, which can be used in express apps. It logs additional information which is very useful for debugging. First, we need to create a new folder and change directory. After that we use the npm INIT command with -y option, to create a package.json file, which is mandatory for any Node.js app. Notice that we have given the -y option, or ELSE we would have to answer a lot of questions. Next, we will install express and two pino packages. One is for normal logging and the other is for http logging. Now, create a file index.js and put the below code in it. Here, we are first importing express, pino-http and pino. The log from the app.get() is using the pino-http module and has been done by req.log.info(). The simple log INSIDE app.listen is using the simple pino, with logger.info(). Also notice that we can pass the number separately in it. Now, when we run the app with node index.js, we will get the detailed log which INCLUDES the timestamp, pid and hostname also. Now, go to http://localhost:3000/ and we will get the detailed log of the http request being made. |
|