Logging part 3 - destination
Published on: 25th Apr 2025
Overview
Logging to a flat file is easy to implement. But, it is going to be time consuming if you need to monitor multiple applications that runs on multiple servers. In this case, you might want to centralize the log for monitoring.
The log destination
Let's continue from what we have done in Logging part 2 - runtime performance.
Here's some sample log messages:
2024-12-07T11:12:40.604Z|i|app-summ.js:handle_req:tid=2,rid=182,req-done,~4.915ms
2024-12-07T11:12:42.221Z|i|app-summ.js:handle_req:tid=2,rid=183,req-done,~3.262ms
The log contains the information about which script file and function is running. It also contains the duration on each function call and all looks good.
To centralize the logs by saving them into a database or a log server, this can be done easily in Node.js. We will have to override the console.log()
function.
Here's the code on how to override console.log()
:
let log_writer_old = process.stdout.write;
process.stdout.write = function(string, encoding, callback) {
// 1. Save to a database or a log server.
//...
// 2. Optional: dump the log on the console.
// log_writer_old.call(process.stdout, 'for console: ' + string, encoding, callback);
};
To make the log more easy to identify the 'source', you will have to prefix the log message with the 'server ID' and 'application ID'. For example, your program tries to log the following messages:
console.log('2024-12-07T11:12:40.604Z|i|app-summ.js:handle_req:tid=2,rid=182,req-done,~4.915ms');
console.log('2024-12-07T11:12:42.221Z|i|app-summ.js:handle_req:tid=2,rid=183,req-done,~3.262ms');
Here's the code that override console.log()
:
let log_writer_old = process.stdout.write;
process.stdout.write = function(string, encoding, callback) {
// Save to a database or a log server.
const server_id = 'server-01:';
const app_id = 'accounting:';
const log_msg = server_id + app_id + string;
const log_msg = `insert into tb_log (msg) values (${log_msg});`;
your_db.execute(log_msg_sql)
.then(() => {
// the log has been saved to database
})
.catch((x) => {
// something bad has happened, log to a file:
let your_log_file = fs.openSync('/var/log/my-app-log', 'w');
fs.writeSync(your_log_file, log_msg);
fs.closeSync(your_log_file);
});
};
Notes: your_db
is a variable that holds the reference to your database connection or log server.
Once the above code has run, you will be find the following records in tb_log table which looks like this:
server-01:accounting:2024-12-07T11:12:40.604Z|i|app-summ.js:handle_req:tid=2,rid=182,req-done,~4.915ms
server-01:accounting:2024-12-07T11:12:42.221Z|i|app-summ.js:handle_req:tid=2,rid=183,req-done,~3.262ms
Conclusion
It's quite easy to centralize the log in Node.js. Right? Try it out yourself to build a central log server.
Related posts
- Logging part 1 - down the process ID (pid) and thread ID (threadId).
- Logging part 2 - runtime performance.
- Logging part 3 - destination.
Back to #NODEJS blog
Back to #blog listing
Author
Lau Hon Wan, software developer.