This article describes how to collect logs using winston in Node.js. The related resources are shown below.
The workflow is shown below.
- Coding preparation
- Coding
- Operation check
Run the following commands in your terminal to prepare for coding.
mkdir nodejs-winston-logging
cd nodejs-winston-logging
npm init -y
npm install --save dotenv winston
touch touch .env logging.js main.js
Open .env in your editor and enter the following 3 variables.
- LOG_CONSOLE: whether to output logs to the console
- LOG_DIRNAME: logs output directory
- LOG_ROTATION: whether to rotate log files
An example is shown below.
Open logging.js in your editor and enter the following content.
The points are shown below.
- makeLogger is a function that creates a winston logger. The first argument is the log level such as error and info, the second argument is the output format such as JSON and raw (raw data), and the third argument is the log file name of the output destination.
- makeFormat is a function that creates a winston log format. It calls the makeFormatJson and makeFormatRaw functions that create a log format based on the first argument.
- makeFormatJson is a function that creates a JSON format log format. It uses the winston.format.timestamp function to add a timestamp to the log.
- makeFormatRaw is a function that creates a log format that outputs the message as it is.
- makeTransportFile is a function that creates a transport (write destination) for outputting logs to a file.
- makeTransportConsole is a function that creates a transport for outputting logs to the console.
Open main.js in your editor and enter the following content.
The points are shown below.
- Call winston.loggers.add function to add a logger.
- Call winston.loggers.get function to get a logger, and then call the method corresponding to the log level (error, warn, etc.) to write a log.
Run the following command in your terminal to output logs.
node -r dotenv/config main.js
Check that the following contents are output to the console.
ERROR
WARN
INFO
QUERY
ACCESS
Also, check that the log directory is created in the working directory and the following files are created.
- access.log
- error.log
- info.log
- query.log
- warn.log
If you deploy your application to Cloud Run, you can check the log by specifying the output destination of log files to /var/log. However, if you always output debug logs (example: SQL query log), storage charges will be high. Therefore, always output only required logs, and output debug logs only when necessary. It is better to switch the output of debug logs through an environment variable.
Collected logs are very helpful in situations such as debugging during development and troubleshooting during production. However, I don't have enough knowledge using log analysis technology, so I haven't been able to use it very well. I'm always looking for a better way to collect logs. If you have any knowledge, I would appreciate your guidance comments, and other comments are welcome. Thank you for reading!