
console.log method. Looking for information on debugging JavaScript code, you will find hundreds of blog articles, as well as instructions on StackOverflow that advise you to “simply” output data to the console using the console.log method. This is such a common practice that I had to introduce rules for code quality control, such as no-console , so as not to leave random log entries in production code. But what if you need to specifically register an event to provide additional information?console.log and console.error methods in Node.js and demonstrates how to pass the logging function to libraries without overloading the user console. 
console.log and console.error methods can be used both in the browser and in Node.js. However, when using Node.js, there is one important thing to keep in mind. If you create the following code in Node.js using a file called index.js ,
node index.js , then the results of command execution will be located one above the other:
console operation in the Node.js documentation , it turns out that console.log displays the result through stdout , and console.error through stderr .stream ) by default: stdin , stdout and stderr . The stdin stream processes input for a process, for example, button clicks or redirected output (more on that below). The standard stdout output stream is for outputting application data. Finally, the standard stderr error stream is designed to display error messages. If you need to figure out what stderr and when to use it, read this article .> ) and pipeline ( | ) operators to work with errors and diagnostic information separately from the actual results of the application. If the > operator allows you to redirect the output of the result of the command to a file, then using the 2> operator you can redirect the output of the stderr error stream to a file. For example, this command sends Hello there to the hello.log file, and Bye bye to the error.log file.

User-Agent client application is being used. You can also find out the time the error occurred and its cause.index.js for the code to be used, and execute the following commands to start the project and install express :
console.log method. We put the following lines in the index.js file:
console.log('%O', req) to log the entire object in the log. From an internal structure point of view, the console.log method uses util.forma t, which, in addition to %O supports other placeholders. Information about them can be found in the Node.js documentation .node index.js to start the server and switching to localhost : 3000, the console displays a lot of unnecessary information:
console.log('%s', req) so as not to display the object completely, you won’t get much information:
pm2 ;stdout and stderr streams, there is a need to keep a journal at different levels, as well as configure and filter journal entries depending on the level.process and writing several lines of code in JavaScript. However, Node.js is remarkable in that it already has an npm ecosystem and several libraries that can be used for these purposes. These include:pino preferred because it is fast and has its own ecosystem. Let's see how pino can help with logging. Another advantage of this library is the express-pino-logger package, which allows you to register requests.pino and express-pino-logger :
index.js file to use the event logger and middleware:
logger for pino for the event logger and passed it to express-pino-logger to create new cross-platform event logging software with which you can call app.use . In addition, console.log replaced by logger.info at server logger.info and added logger.debug to the route to display different levels of the log.node index.js , you will get a different result at the output, in which each line / line will be output in JSON format. Again, go to localhost : 3000 to see another new line in JSON format.
logger.debug message logger.debug not displayed. In order to make it visible, you need to change the default log level. After creating an instance of the logger event registration, the value process.env.LOG_LEVEL was set. This means that you can change the value or accept the default info value. Running LOG_LEVEL=debug node index.js , we change the level of the log.pino philosophy, for performance reasons, it is necessary to transfer the processing of journal entries to a separate process, passing the output (using the | operator). The process involves translating the output into a format more convenient for human perception, or uploading it to the cloud. This task is performed by transfer tools called transports . Check out the transports toolkit documentation and see why pino errors are not output via stderr .pino-pretty tool. Run in the terminal:
| at the disposal of pino-pretty , due to which the output is "cleared", which will contain only important information displayed in different colors. If you query localhost : 3000 again, a debug debugging message should appear.
pino-colada . These tools will be useful for local development. When the server is in production, it may be necessary to transfer log data using another tool , write it to disk using > for subsequent processing, or do two operations at the same time using a specific command, for example tee .express framework. Many processes take place in the internal structure of the express framework, which may cause interest in studying it more deeply while debugging the application. The documentation for the express framework says that you can add DEBUG=express:* to the beginning of the command as follows:

debug package for this. It can be used to write messages in the "namespace", and if the library user includes this namespace or a wildcard that matches it in their DEBUG environment variable , the messages will be displayed. First you need to install the debug library:
random-id.j s that will simulate the library and put the following code into it:
debug event logger will be created with the mylib:randomid , in which two messages will then be registered. We use it in index.js from the previous section:
DEBUG=mylib:randomid node index.js this time, then the debug log entries for our “library” will be displayed:
pino log entries, they can use a library called pino-debug created by the pino command to format these entries correctly.
debug for the first time, pino-debug must be initialized. The easiest way to do this is to use the -r or --require flags to request a module before running the script. We restart the server using the command (provided that pino-colada installed):

debug . In this case, you can reuse the program logic without being limited to one scenario using the command line interface.CI flag is set. You can verify that you are in a continuous integration system using the is-ci package, which supports several such systems.chalk , define continuous integration systems and cancel the output of colored text to the console. Let's see how it works.chalk with the npm install chalk and create a file called cli.js Put the following lines in the file:
node cli.js , the results will be presented using different colors:
CI=true node cli.js , the color formatting of the texts will be canceled:
stdout running in terminal mode, i.e. data is output to the terminal. In this case, the results can be nicely displayed using boxen . Otherwise, the output will most likely be redirected to a file or somewhere else.stdin , stdout or stderr streams in terminal mode by looking at the isTTY attribute of the corresponding stream. For example, process.stdout.isTTY . TTY means "teletypewriter" and in this case is designed specifically for the terminal.process.stdout.isTTY varies in different situations. cli.js file to check it:
node cli.js in the terminal and see the word true , after which the message is displayed in colored font:

undefined appeared in the terminal, followed by a message displayed in a colorless font, since the redirection of the stdout stream brought it out of terminal mode. Here chalk uses the supports-color tool, which from the point of view of the internal structure checks the isTTY corresponding stream.
chalk do these things on their own. However, when developing a command line interface, you should always remember about situations when the interface works in a continuous integration system or the output is redirected. These tools help you use the command line interface at a higher level. For example, the data in the terminal can be organized in a more structured way, and if isTTY is undefined , switch to a simpler way of analysis.console.log quite simple. But before you deploy the code in production, you should consider several aspects of using the log. This article is only an introduction to the various methods and solutions used in organizing the event log. It does not contain everything you need to know. Therefore, it is recommended to pay attention to successful open source projects and monitor how they solved the logging problem and what tools are used. Now try to log yourself without outputting data to the console.Source: https://habr.com/ru/post/461881/
All Articles