Skip to content

Tip: Multiple Transports with Different Formatting (es6) #141

Description

@patrickmcintire

This may be a little hacky, but helpful to me. It allows me to have a standard console output and a log I can output in JSON format that I can logrotate however I wish. Helps with development and debugging even in a production environment.

// Tracer Logger
import pkg from 'tracer';
const fileFormat = '{"timestamp": "{{timestamp}}", "level": "{{title}}", "file": "{{file}}", "line": "{{line}}", "message": "{{message}}"}';
const fileName = '../logs/node/node.log';

export const tR = pkg.console({
	format: fileFormat,
	transport: [
		(data) => {
			fs.appendFile(fileName, data.rawoutput + '\n', (err) => {
				if (err) throw err;
			});
		},
		(data) => {
			const parsedData = JSON.parse(data.output);
			const consoleFormat = 
				parsedData.file +
				':' + parsedData.line +
				' <' + parsedData.level + '> ' +
				parsedData.message;
			console.log(consoleFormat);
		},
	]
});

// console: server.js:373 <info> HTTPS API listening on port 3443.
// file: {"timestamp": "2026-01-09T22:46:54-0500", "level": "info", "file": "server.js", "line": "373", "message": "HTTPS API listening on port 3443."}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions