This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

In JavaScript development, console.log is often used to debug programs.

During the development phase, whether logging or debugging, it is easy to trace the program and detect errors. But in a production environment, you should think more about logging, because it is critical for application monitoring and troubleshooting.

This article introduces node.js logging concepts and introduces the popular logging script library. And in the article “Node.js Logging Winston Usage Guide” code practice simple logging module.

About Logging

Log recording is the process of recording the information generated by the running of an application program in a log file. The records saved in a log file are called logs. Logs are a simple method to save information about an application program.

Logging is the preferred way for programmers to track the flow of errors and events, which used to be common in server-side development and is now essential for the WEB front end.

For front-end development, console.log or debugger are often used to track errors during development.

But when the application goes into production and users start interacting with it, console.log is no longer available. If something goes wrong and the application crashes, you can’t use the console to know.

This is why a clean, clean, and efficient logging framework is needed. Instead, by providing a live stream of log events, these logs can be used to diagnose exceptions, malware activity, or unauthorized resource access in real time.

Therefore, logging will make it easier for developers, DevOps, SysAdmins, or SecOps to understand and identify the root cause of application and infrastructure problems.

The log level

This is the most important part of any logging system. Log levels are metadata of logs that define the severity of recorded event information. The reason for setting log levels in the first place is so you can quickly see which logs need more attention. Generally, there are five main log levels, which are listed in order of priority as follows:

  • ERROR: A serious problem/failure occurred while processing the current operation. This type of log needs to be handled as soon as possible.
  • WARN: warning level, which does not prevent the application from continuing and emits an alarm log when unexpected application problems are detected. Logs at this level are generally checked to determine if they should be resolved.
  • INFO: Normal behavior of the application
  • DEBUG: This level is intended for developers and provides diagnostic information in a detailed manner, often used to obtain information needed for diagnosis, troubleshooting, or testing applications.
  • TRACE: Captures all detailed information about the application’s behavior, primarily for detailed tracing of application logic.

Note: The normal behavior of loggers is to track only logs at the current level and above. Therefore, if the log level is set to INFO, only INFO, WARN, and ERROR log information will be obtained.

The best principle

There are generally five basic principles that should be followed for logging systems:

1. Record meaning and purpose

Do not add unnecessary logs, because extra log information becomes noise. In addition, if the frequency of logging is too high, the application performance will be directly affected.

Note: DEBUG or TRACE logging should not be enabled in production unless there is a problem with the code base. (Make sure to switch it back to INFO or ERROR when the problem is resolved.)

2. Split logs to avoid large log files

If logs are not partitioned, log files can be very large and cumbersome to analyze. In the case of file logs, too large files also affect performance. You can use a separate log file for a separate log level, or you can try using the rolling log file feature available in most logging frameworks. Split and compress log files based on time or size.

3. It should be hierarchical

As described above, logs should be processed according to the priority level. This will help you analyze the logs and find problems in a very short time.

In addition, log namespaces are used to keep content clean, and log timestamps are essential key information for log information.

4. There should be no side effects

The side effect here is that logging should not affect the application itself and should not cause serious program problems because of logging.

5. Do not record any sensitive information

When logging in, you must ensure that no sensitive information is recorded, such as user login name and password, ID card, mobile phone number, bank card number, etc.

The above five are the minimum principles that a logging system should follow. Here are a few frameworks you can use for logging to avoid reinventing the wheel and increase efficiency. These are the most popular logging libraries available on NPM:

  • Winston: A flexible general-purpose log library
  • Morgan: HTTP request logger middleware
  • Pino: Super fast (very low overhead), pure native JSON logger
  • Loglevel: JavaScript’s minimal lightweight simple logging
  • Log4js: No runtime dependent logging framework

conclusion

This article introduced the concepts related to logging in Node.js and introduced five popular script libraries on NPM, some of which support multiple languages and environments. At present, the application and platform business logic are relatively complex, so the log system needs to provide better services for the transformation, experience and upgrade of the application and platform.