get line number and filename for a log output - node.js
Is it possible to get the line number and file for each log output ?
For example:
var winston = require('winston');
winston.log('info', 'some message!'); // this is at line 4 of myfile.js
should specify in log file that 'some message' came from myFile.js line 4.
You can pass the file name as label and you can get the file name from callingModule.
Create logger.js file and code like
var winston = require('winston');
var getLabel = function (callingModule) {
var parts = callingModule.filename.split('/');
return parts[parts.length - 2] + '/' + parts.pop();
};
module.exports = function (callingModule) {
return new winston.Logger({
transports: [
new winston.transports.Console({
label: getLabel(callingModule),
json: false,
timestamp: true,
depth:true,
colorize:true
})
]
});
};
Now Here your test file
var logger = require('./logger')(module);
function test() {
logger.info('test logger');
}
test();
and if you run test file than the output looks like
2017-07-08T07:15:20.671Z - info: [utils/test.js] test logger
winston didn't have the plan to do that due to performance concerns. Please check here for detailed information.
I tried https://github.com/baryon/tracer but it isn't good, e.g. line number is incorrect from time to time.
I found this code somewhere, yeah but It is working. Use it in a new winston.js and then requires that in any file.
var winston = require('winston')
var path = require('path')
var PROJECT_ROOT = path.join(__dirname, '..')
var appRoot = require('app-root-path');
const options = {
file: {
level: 'info',
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
timestamp: true
},
console: {
level: 'debug',
handleExceptions: true,
json: true,
colorize: true,
timestamp: true
}
};
var logger = new winston.Logger({
transports: [
new winston.transports.File(options.file),
new winston.transports.Console(options.console)
],
exitOnError: false // do not exit on handled exceptions
});
logger.stream = {
write: function (message) {
logger.info(message)
}
}
// A custom logger interface that wraps winston, making it easy to instrument
// code and still possible to replace winston in the future.
module.exports.debug = module.exports.log = function () {
logger.debug.apply(logger, formatLogArguments(arguments))
}
module.exports.info = function () {
logger.info.apply(logger, formatLogArguments(arguments))
}
module.exports.warn = function () {
logger.warn.apply(logger, formatLogArguments(arguments))
}
module.exports.error = function () {
logger.error.apply(logger, formatLogArguments(arguments))
}
module.exports.stream = logger.stream
/**
* Attempts to add file and line number info to the given log arguments.
*/
function formatLogArguments (args) {
args = Array.prototype.slice.call(args)
var stackInfo = getStackInfo(1)
if (stackInfo) {
// get file path relative to project root
var calleeStr = '(' + stackInfo.relativePath + ':' + stackInfo.line + ')'
if (typeof (args[0]) === 'string') {
args[0] = calleeStr + ' ' + args[0]
} else {
args.unshift(calleeStr)
}
}
return args
}
/**
* Parses and returns info about the call stack at the given index.
*/
function getStackInfo (stackIndex) {
// get call stack, and analyze it
// get all file, method, and line numbers
var stacklist = (new Error()).stack.split('\n').slice(3)
// stack trace format:
// http://code.google.com/p/v8/wiki/JavaScriptStackTraceApi
// do not remove the regex expresses to outside of this method (due to a BUG in node.js)
var stackReg = /at\s+(.*)\s+\((.*):(\d*):(\d*)\)/gi
var stackReg2 = /at\s+()(.*):(\d*):(\d*)/gi
var s = stacklist[stackIndex] || stacklist[0]
var sp = stackReg.exec(s) || stackReg2.exec(s)
if (sp && sp.length === 5) {
return {
method: sp[1],
relativePath: path.relative(PROJECT_ROOT, sp[2]),
line: sp[3],
pos: sp[4],
file: path.basename(sp[2]),
stack: stacklist.join('\n')
}
}
}
Source: https://gist.github.com/transitive-bullshit/39a7edc77c422cbf8a18
Update (for Winston 3.x)
I also created a gist for the following code:
const { format } = require('winston');
const { combine, colorize, timestamp, printf } = format;
/**
* /**
* Use CallSite to extract filename and number, for more info read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
* #param numberOfLinesToFetch - optional, when we want more than one line back from the stacktrace
* #returns {string|null} filename and line number separated by a colon, if numberOfLinesToFetch > 1 we'll return a string
* that represents multiple CallSites (representing the latest calls in the stacktrace)
*
*/
const getFileNameAndLineNumber = function getFileNameAndLineNumber (numberOfLinesToFetch = 1) {
const oldStackTrace = Error.prepareStackTrace;
const boilerplateLines = line => line &&
line.getFileName() &&
(line.getFileName().indexOf('<My Module Name>') &&
(line.getFileName().indexOf('/node_modules/') < 0));
try {
// eslint-disable-next-line handle-callback-err
Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace;
Error.captureStackTrace(this);
// we need to "peel" the first CallSites (frames) in order to get to the caller we're looking for
// in our case we're removing frames that come from logger module or from winston
const callSites = this.stack.filter(boilerplateLines);
if (callSites.length === 0) {
// bail gracefully: even though we shouldn't get here, we don't want to crash for a log print!
return null;
}
const results = [];
for (let i = 0; i < numberOfLinesToFetch; i++) {
const callSite = callSites[i];
let fileName = callSite.getFileName();
fileName = fileName.includes(BASE_DIR_NAME) ? fileName.substring(BASE_DIR_NAME.length + 1) : fileName;
results.push(fileName + ':' + callSite.getLineNumber());
}
return results.join('\n');
} finally {
Error.prepareStackTrace = oldStackTrace;
}
};
function humanReadableFormatter ({ level, message, ...metadata }) {
const filename = getFileNameAndLineNumber();
return `[${level}] [${filename}] ${message} ${JSON.stringify(metadata)}`;
}
const logger = winston.createLogger({
transports: [
new winston.transports.Console({
level: 'info',
handleExceptions: true,
humanReadableUnhandledException: true,
json: false,
colorize: { all: true },
stderrLevels: ['error', 'alert', 'critical', 'bizAlert'],
format: combine(
colorize(),
timestamp(),
humanReadableFormatter,
),
})
]
});
Original Answer (for Winston 2.x)
I'm using winston 2.x (but the same solution will work for winston 3.x) and that's the way I'm logging the filename and linenumber of the caller:
IMPORTANT: pay attention to the embedded code comments!
/**
* Use CallSite to extract filename and number
* #returns {string} filename and line number separated by a colon
*/
const getFileNameAndLineNumber = () => {
const oldStackTrace = Error.prepareStackTrace;
try {
// eslint-disable-next-line handle-callback-err
Error.prepareStackTrace = (err, structuredStackTrace) => structuredStackTrace;
Error.captureStackTrace(this);
// in this example I needed to "peel" the first 10 CallSites in order to get to the caller we're looking for, hence the magic number 11
// in your code, the number of stacks depends on the levels of abstractions you're using, which mainly depends on winston version!
// so I advise you to put a breakpoint here and see if you need to adjust the number!
return this.stack[11].getFileName() + ':' + this.stack[11].getLineNumber();
} finally {
Error.prepareStackTrace = oldStackTrace;
}
};
And (a simplified version of) the formatter function:
function humanReadableFormatter ({level, message}) {
const filename = getFileNameAndLineNumber();
return `[${level}] ${filename} ${message}`;
}
Then declare the transport to use the formatter:
new winston.transports.Console({
level: 'info',
handleExceptions: true,
humanReadableUnhandledException: true,
json: false,
colorize: 'level',
stderrLevels: ['warn', 'error', 'alert'],
formatter: humanReadableFormatter,
})
To read more about prepareStackTrace read: https://v8.dev/docs/stack-trace-api#customizing-stack-traces
You can do string operations on
console.log(new Error().stack.split('\n')[1].slice(7));
to get line Number and file path too along with function name.
The output would look like
AutomationFramework._this.parsingLogic (/Users/user_name/Desktop/something/some-file.js:49:25)
here is how I go around it
import winston from 'winston'
const {format } = winston
const { combine, timestamp, printf } = format;
const myFormat = printf(({ level, message,timestamp , at }) => {
let on = at.stack.split('\n')[1].slice(7).split('/').pop() ;
let file = on.split(':')[0]
let line = on.split(':')[1]
let data = Date(timestamp).toString().split(' GMT')[0]
return `${level}: [${data}] Hello <yourName>, there is a ${level} message on file:${file} line ${line}
${message}`;
});
const logger = winston.createLogger({
level: 'info',
format: combine(
timestamp(),
myFormat ,
),
transports: [new winston.transports.Console()],
});
on loggin the output will be like
logger.info('wrong file type' , {at : new Error})
//logs
warn: [Thu Aug 18 2022 15:06:28] Hello <yourName>, there is a warn message on file:winston.js line 7
>>wrong file type
Related
pino-pretty, how to add file name to log line
i need to add file name to pino-pretty line output, now i'm using: const pino = require('pino'); const logger = pino({ prettyPrint: { colorize: true, translateTime: 'yyyy-mm-dd HH:MM:ss', ignore: 'pid,hostname' } }) and have this output: [2020-05-14 16:25:45] INFO : Network is private but i want something like this: [2020-05-14 16:25:45] INFO myFile.js: Network is private i.e. i want see filename in line witch was launch, i try play with customPrettifiers option but can't get hoped result, for example i try this: const pino = require('pino'); const path = require('path'); const logger = pino({ prettyPrint: { colorize: true, translateTime: 'yyyy-mm-dd HH:MM:ss', ignore: 'pid,hostname', customPrettifiers: { filename: path.basename(__filename) } } })
I think the closest you can get is as follows: const path = require('path'); const pino = require('pino'); const logger = pino({ prettyPrint: { // Adds the filename property to the message messageFormat: '{filename}: {msg}', // need to ignore 'filename' otherwise it appears beneath each log ignore: 'pid,hostname,filename', }, }).child({ filename: path.basename(__filename) }); Note that you can't style the filename differently to the message, but hopefully that's good enough. It's probably also better to have a separate logger.js file where the default pino options are passed e.g.: // logger.js const logger = require('pino')({ prettyPrint: { messageFormat: '{filename}: {msg}', ignore: 'pid,hostname,filename', }, }); module.exports = logger; // file_with_logging.js const parentLogger = require('./logger.js'); const logger = parentLogger.child({ filename: path.basename(__filename) });
Winston logger not saving logs correctly - two directories created
This is what my winston logger looks like: var winston = require('winston') var moment = require('moment') var fs = require('fs') var logDir = 'logs' // Create the log directory if it does not exist if (!fs.existsSync(logDir)) { fs.mkdirSync(logDir) } function formatter (options) { if (options.level === 'error') { return options.timestamp() + ' *** ' + options.level.toUpperCase() + ' *** ' + (undefined !== options.message ? options.message : '') + (options.meta && Object.keys(options.meta).length ? ' ' + JSON.stringify(options.meta) : ' ') } else if (options.level === 'info') { return options.timestamp() + ' - ' + options.level.toUpperCase() + ' - ' + (undefined !== options.message ? options.message : '') + (options.meta && Object.keys(options.meta).length ? ' ' + JSON.stringify(options.meta) : '') } } var systemLogger = new (winston.Logger)({ transports: [ new (winston.transports.Console)({ timestamp: function () { return moment().format('D/MM/YYYY HH:mm:ss:SSS') } }), new (require('winston-daily-rotate-file'))({ filename: 'logs/-system.log', datePattern: 'dd-MM-yyyy', prepend: true, json: false, timestamp: function () { return moment().format('D/MM/YYYY HH:mm:ss:SSS') }, formatter: formatter }) ] }) module.exports = systemLogger To me it looks like everything is fine in the above code. However there are two errors... instead of creating one directory called 'logs' inside the project directory, the program creates two directories called 'logs' - once inside the project directory and one outside of it. Any idea why? So in my actual program, I call the logger like this: // winston logger var systemLogger = require('./logging.js') systemLogger.info(Server Updated', { 'ip': ip }) Nowhere do I specify that I want two 'logs' directories, so Im slightly confused. Any help would be appreciated.
It is because when you are creating directory using "fs" module it creates new directory at same level in which your file exists. But using Winston it created directory (filename: 'logs/-system.log') at the project level. That's why two directories are created.
How to log request & transaction id in each log line using winston for Node JS?
I have built a Node JS REST service using Express. Every request which lands on this service has headers like 'X-org-ReqId' & 'X-org-Tid' which I need to log in all the log lines which are written during execution of this request. Basically I need to log some contextual information with each log line to help me do transaction/request tracing through multiple services. I am using winston logger initialized like this: var winston = require('winston'); var appLogger = new(winston.Logger)({ transports: [ new(winston.transports.Console)({ level: 'info', //TODO: Should be changed to Error in prod colorize: true }), new(winston.transports.DailyRotateFile)({ filename: '/var/log/org/my-service.log', datePattern: '.yyyy-MM-dd', tailable: true, // handleExceptions: true, json: true, logstash: true }) ], exitOnError: false }); appLogger.on('error', function(err) { console.error("Logger in error", err); }); module.exports.logger = function() { return appLogger; }; and in individual classes wherever I want to use it, I do like this: var logger = require('../config/logger').logger(); myObject.on("error", function (err) { logger.error("Error connecting bucket=" + bucketId , err); }); This will produce log like this: {"level":"info","message":"Error connecting bucket=2.....","timestamp":"2015-06-10T06:44:48.690Z"} Winston by default add timestamp to this log statement but I also want to log things like req.headers['X-org-ReqId'] & req.headers['X-org-Tid'] so that I know which transaction was failed and what was the error. I want log like this: {"level":"info","message":"Error connecting bucket=2....","timestamp":"2015-06-10T06:44:48.690Z", "tid":"a3e8b380-1caf-11e5-9a21-1697f925ec7b", "reqid":"aad28806-1caf-11e5-9a21-1697f925ec7b"} In java world we used to have NDC, is there an equivalent in Node JS world?
I encountered the same issue as you. It may not be the best way of doing things because you have to propagate the 'req' object but it works. I am willing to get any improvement :) Frist I overwrite winston logging methods in my 'log.js' : // MyLogger definition function MyLogger() { this.__proto__.__proto__.constructor.apply(this, arguments); } // Inheritance MyLogger.prototype.__proto__ = winston.Logger.prototype; // Overwriting methods MyLogger.prototype.log = function() { var args = []; // Copying arguments not to modify them for (var i = 0; i < arguments.length; i++) { args[i] = arguments[i]; } // Adding information in logs var lastArg = args[arguments.length - 1]; if (typeof lastArg === 'object' && lastArg.headers) { args[arguments.length - 1] = { // Liste des infos ajoutées dans les logs requestId: lastArg.headers['x-request-id'] ? lastArg.headers['x-request-id'] : arguments[arguments.length - 1].id, host: arguments[arguments.length - 1].headers.host, pId: process.pid }; } // Calling super this.__proto__.__proto__.log.apply(this, args); } MyLogger.prototype.error = function() { var args = ["error"]; for (var i = 0; i < arguments.length; i++) { args[i + 1] = arguments[i]; } this.__proto__.log.apply(this, args); } MyLogger.prototype.warn = function() { var args = ["warn"]; for (var i = 0; i < arguments.length; i++) { args[i + 1] = arguments[i]; } this.__proto__.log.apply(this, args); } MyLogger.prototype.info = function() { var args = ["info"]; for (var i = 0; i < arguments.length; i++) { args[i + 1] = arguments[i]; } this.__proto__.log.apply(this, args); } MyLogger.prototype.verbose = function() { var args = ["verbose"]; for (var i = 0; i < arguments.length; i++) { args[i + 1] = arguments[i]; } this.__proto__.log.apply(this, args); } MyLogger.prototype.debug = function() { var args = ["debug"]; for (var i = 0; i < arguments.length; i++) { args[i + 1] = arguments[i]; } this.__proto__.log.apply(this, args); } MyLogger.prototype.silly = function() { var args = ["silly"]; for (var i = 0; i < arguments.length; i++) { args[i + 1] = arguments[i]; } this.__proto__.log.apply(this, args); } var logger = new MyLogger({ transports: [ new winston.transports.File({ level: config.LOG_LEVEL, filename: config.LOG_FILE, handleExceptions: true, json: true, maxsize: 5242880, //5MB maxFiles: 5, colorize: false }), new winston.transports.Console({ level: config.LOG_LEVEL, handleExceptions: true, json: false, colorize: true }) ], exitOnError: false }); module.exports = logger; Then in every module that needs logging : var logger = require("../log.js"); ... logger.debug("My message", req); Produces a log in console and a JSON log in log file with every information neede to to follow a stream. My next step would be not to have to propagate 'req' and getting the needed infos from a context or a session. Hope that helped :)
my 2 cent idea, Use logFormatter from winston to add more fields to each logs:- One example below: const { format} = require("winston"); var reqId = '123123' //function or const request id const addRequestId = format((info, opts) => { if(reqId) info.reqId= reqId; return info; }); Then add below config while creating logger var config = { format: format.combine( addRequestId(), format.timestamp(new Date().toISOString()), format.json(), ), transports: [new transports.Console()], level: 'debug' } const logger = createLogger(config);
winston logger file permission exception
I have written an API using node.js ,in that used "Winston logger". In that the file i have mentioned to write all my logs has owner as "ROOT", so my application exited. When i change the file owner to "my user" my application works. So my conclusion is that the file permission is the problem. My winston code: new(winston.transports.File)({ filename: config.path,-------------------->/home/username/logsdir/logfile //maxsize: 1024 * 1024 * 10, // 10MB exitOnError: false, maxsize: 1024 * 1024, level: 'info', handleExceptions: true, levels: customLevels.levels, rotationFormat: function() { return getFormattedDate(); function getFormattedDate() { var temp = new Date(); return dateStr = padStr(temp.getFullYear()) + padStr(1 + temp.getMonth()) + padStr(temp.getDate()) + padStr(temp.getHours()) + padStr(temp.getMinutes()) + padStr(temp.getSeconds()); } function padStr(i) { return (i < 10) ? "0" + i : "" + i; } } })] Winston is not throwing any exception, simply my application exited. IS it possible to check the owner of a file, Or winston has any option to tackle this. I googled a lot but i cant find the solution. Help me to solve this. Thanks in advance.
Winston appears not to provide any help for such cases. The solution is to check for yourself if the file is writable. Here's one way to do so: var logFile; [ '/var/log/my-app.log', path.join(process.cwd(), 'my-app.log'), path.join('/tmp', 'my-app.log'), ] .forEach(function (path) { if (logFile) { return; } if (fs.accessSync && fs.accessSync(path, fs.W_OK)) { console.log('logging to ' + path); logFile = path; return; } var fh; try { fh = fs.openSync(path, 'a'); } catch (e) { console.error(e.message); return; } console.log('logging to ' + path); logFile = path; fs.closeSync(fh); }); Now the variable logFile will have the name of a writable file, if one is writable.
Configure Node.js to log to a file instead of the console
Can I configure console.log so that the logs are written on a file instead of being printed in the console?
You could also just overload the default console.log function: var fs = require('fs'); var util = require('util'); var log_file = fs.createWriteStream(__dirname + '/debug.log', {flags : 'w'}); var log_stdout = process.stdout; console.log = function(d) { // log_file.write(util.format(d) + '\n'); log_stdout.write(util.format(d) + '\n'); }; Above example will log to debug.log and stdout. Edit: See multiparameter version by Clément also on this page.
Update 2013 - This was written around Node v0.2 and v0.4; There are much better utilites now around logging. I highly recommend Winston Update Late 2013 - We still use winston, but now with a logger library to wrap the functionality around logging of custom objects and formatting. Here is a sample of our logger.js https://gist.github.com/rtgibbons/7354879 Should be as simple as this. var access = fs.createWriteStream(dir + '/node.access.log', { flags: 'a' }) , error = fs.createWriteStream(dir + '/node.error.log', { flags: 'a' }); // redirect stdout / stderr proc.stdout.pipe(access); proc.stderr.pipe(error);
If you are looking for something in production winston is probably the best choice. If you just want to do dev stuff quickly, output directly to a file (I think this works only for *nix systems): nohup node simple-server.js > output.log &
I often use many arguments to console.log() and console.error(), so my solution would be: var fs = require('fs'); var util = require('util'); var logFile = fs.createWriteStream('log.txt', { flags: 'a' }); // Or 'w' to truncate the file every time the process starts. var logStdout = process.stdout; console.log = function () { logFile.write(util.format.apply(null, arguments) + '\n'); logStdout.write(util.format.apply(null, arguments) + '\n'); } console.error = console.log;
Winston is a very-popular npm-module used for logging. Here is a how-to. Install winston in your project as: npm install winston --save Here's a configuration ready to use out-of-box that I use frequently in my projects as logger.js under utils. /** * Configurations of logger. */ const winston = require('winston'); const winstonRotator = require('winston-daily-rotate-file'); const consoleConfig = [ new winston.transports.Console({ 'colorize': true }) ]; const createLogger = new winston.Logger({ 'transports': consoleConfig }); const successLogger = createLogger; successLogger.add(winstonRotator, { 'name': 'access-file', 'level': 'info', 'filename': './logs/access.log', 'json': false, 'datePattern': 'yyyy-MM-dd-', 'prepend': true }); const errorLogger = createLogger; errorLogger.add(winstonRotator, { 'name': 'error-file', 'level': 'error', 'filename': './logs/error.log', 'json': false, 'datePattern': 'yyyy-MM-dd-', 'prepend': true }); module.exports = { 'successlog': successLogger, 'errorlog': errorLogger }; And then simply import wherever required as this: const errorLog = require('../util/logger').errorlog; const successlog = require('../util/logger').successlog; Then you can log the success as: successlog.info(`Success Message and variables: ${variable}`); and Errors as: errorlog.error(`Error Message : ${error}`); It also logs all the success-logs and error-logs in a file under logs directory date-wise as you can see here.
const fs = require("fs"); const {keys} = Object; const {Console} = console; /** * Redirect console to a file. Call without path or with false-y * value to restore original behavior. * #param {string} [path] */ function file(path) { const con = path ? new Console(fs.createWriteStream(path)) : null; keys(Console.prototype).forEach(key => { if (path) { this[key] = (...args) => con[key](...args); } else { delete this[key]; } }); }; // patch global console object and export module.exports = console.file = file; To use it, do something like: require("./console-file"); console.file("/path/to.log"); console.log("write to file!"); console.error("also write to file!"); console.file(); // go back to writing to stdout
For simple cases, we could redirect the Standard Out (STDOUT) and Standard Error (STDERR) streams directly to a file(say, test.log) using '>' and '2>&1' Example: // test.js (function() { // Below outputs are sent to Standard Out (STDOUT) stream console.log("Hello Log"); console.info("Hello Info"); // Below outputs are sent to Standard Error (STDERR) stream console.error("Hello Error"); console.warn("Hello Warning"); })(); node test.js > test.log 2>&1 As per the POSIX standard, 'input', 'output' and 'error' streams are identified by the positive integer file descriptors (0, 1, 2). i.e., stdin is 0, stdout is 1, and stderr is 2. Step 1: '2>&1' will redirect from 2 (stderr) to 1 (stdout) Step 2: '>' will redirect from 1 (stdout) to file (test.log)
If this is for an application, you're probably better off using a logging module. It'll give you more flexibility. Some suggestions. winston https://github.com/winstonjs/winston log4js https://github.com/nomiddlename/log4js-node
Straight from nodejs's API docs on Console const output = fs.createWriteStream('./stdout.log'); const errorOutput = fs.createWriteStream('./stderr.log'); // custom simple logger const logger = new Console(output, errorOutput); // use it like console const count = 5; logger.log('count: %d', count); // in stdout.log: count 5
Another solution not mentioned yet is by hooking the Writable streams in process.stdout and process.stderr. This way you don't need to override all the console functions that output to stdout and stderr. This implementation redirects both stdout and stderr to a log file: var log_file = require('fs').createWriteStream(__dirname + '/log.txt', {flags : 'w'}) function hook_stream(stream, callback) { var old_write = stream.write stream.write = (function(write) { return function(string, encoding, fd) { write.apply(stream, arguments) // comments this line if you don't want output in the console callback(string, encoding, fd) } })(stream.write) return function() { stream.write = old_write } } console.log('a') console.error('b') var unhook_stdout = hook_stream(process.stdout, function(string, encoding, fd) { log_file.write(string, encoding) }) var unhook_stderr = hook_stream(process.stderr, function(string, encoding, fd) { log_file.write(string, encoding) }) console.log('c') console.error('d') unhook_stdout() unhook_stderr() console.log('e') console.error('f') It should print in the console a b c d e f and in the log file: c d For more info, check this gist.
Overwriting console.log is the way to go. But for it to work in required modules, you also need to export it. module.exports = console; To save yourself the trouble of writing log files, rotating and stuff, you might consider using a simple logger module like winston: // Include the logger module var winston = require('winston'); // Set up log file. (you can also define size, rotation etc.) winston.add(winston.transports.File, { filename: 'somefile.log' }); // Overwrite some of the build-in console functions console.error = winston.error; console.log = winston.info; console.info = winston.info; console.debug = winston.debug; console.warn = winston.warn; module.exports = console;
If you're using linux, you can also use output redirection. Not sure about Windows. node server.js >> file.log 2>> file.log >> file.log to redirect stdout to the file 2>> file.log to redirect stderr to the file others use the shorthand &>> for both stdout and stderr but it's not accepted by both my mac and ubuntu :( extra: > overwrites, while >> appends. By the way, regarding NodeJS loggers, I use pino + pino-pretty logger
METHOD STDOUT AND STDERR This approach can help you (I use something similar in my projects) and works for all methods including console.log, console.warn, console.error, console.info This method write the bytes written in stdout and stderr to file. Is better than changing console.log, console.warn, console.error, console.info methods, because output will be exact the same as this methods output var fs= require("fs") var os= require("os") var HOME= os.homedir() var stdout_r = fs.createWriteStream(HOME + '/node.stdout.log', { flags: 'a' }) var stderr_r = fs.createWriteStream(HOME + '/node.stderr.log', { flags: 'a' }) var attachToLog= function(std, std_new){ var originalwrite= std.write std.write= function(data,enc){ try{ var d= data if(!Buffer.isBuffer(d)) d= Buffer.from(data, (typeof enc === 'string') ? enc : "utf8") std_new.write.apply(std_new, d) }catch(e){} return originalwrite.apply(std, arguments) } } attachToLog(process.stdout, stdout_r) attachToLog(process.stderr, stderr_r) // recommended catch error on stdout_r and stderr_r // stdout_r.on("error", yourfunction) // stderr_r.on("error", yourfunction)
Adding to the answer above, a lit bit of an expansion to the short and efficient code overriding console.log. Minor additions: set filename with date, wrapper function, also do the original console.logging to keep the console active with the info. Usage: in the beginning of your code, run setConsoleLogToFile([FILENAME]). const fs = require("fs"), util = require('util'); const getPrettyDate = ()=> new Date().toString().replace(":","-").replace(/00\s\(.*\)/, "").replace(` ${new Date().getFullYear()}`, ",").replace(/:\d\d\s/, " "); module.exports.getPrettyDate = getPrettyDate; module.exports.setConsoleLogToFile = (filename) => { const log_file = fs.createWriteStream(`${__dirname}/${filename} - ${getPrettyDate()}.log`, { flags: 'w' }), log_stdout = process.stdout; const origConsole = console.log; console.log = (d) => { origConsole(d); log_file.write(util.format(d) + '\n'); log_stdout.write(util.format(d) + '\n'); }; }
Most logger is overkill and does not support the build in console.log correctly. Hence I create console-log-to-file: import { consoleLogToFile } from "console-log-to-file"; // or `const { consoleLogToFile } = require("console-log-to-file/dist/index.cjs.js")` consoleLogToFile({ logFilePath: "/log/default.log", }); // all of your console.log/warn/error/info will work as it does and save to file now.
If you are looking for a solution without modifying any code, here is a simple solution. It requires pm2, just add it to your node modules and start you app with pm2 start server.js And you are done, console.logs are now automatically registered under home/.pm2/logs/server-out.log.
Improve on Andres Riofrio , to handle any number of arguments var fs = require('fs'); var util = require('util'); var log_file = fs.createWriteStream(__dirname + '/debug.log', {flags : 'w'}); var log_stdout = process.stdout; console.log = function(...args) { var output = args.join(' '); log_file.write(util.format(output) + '\r\n'); log_stdout.write(util.format(output) + '\r\n'); };
You can now use Caterpillar which is a streams based logging system, allowing you to log to it, then pipe the output off to different transforms and locations. Outputting to a file is as easy as: var logger = new (require('./').Logger)(); logger.pipe(require('fs').createWriteStream('./debug.log')); logger.log('your log message'); Complete example on the Caterpillar Website
You can also have a look at this npm module: https://www.npmjs.com/package/noogger noogger simple and straight forward...
For future users. #keshavDulal answer doesn't work for latest version. And I couldn't find a proper fix for the issues that are reporting in the latest version 3.3.3. Anyway I finally fixed it after researching a bit. Here is the solution for winston version 3.3.3 Install winston and winston-daily-rotate-file npm install winston npm install winston-daily-rotate-file Create a new file utils/logger.js const winston = require('winston'); const winstonRotator = require('winston-daily-rotate-file'); var logger = new winston.createLogger({ transports: [ new (winston.transports.DailyRotateFile)({ name: 'access-file', level: 'info', filename: './logs/access.log', json: false, datePattern: 'yyyy-MM-DD', prepend: true, maxFiles: 10 }), new (winston.transports.DailyRotateFile)({ name: 'error-file', level: 'error', filename: './logs/error.log', json: false, datePattern: 'yyyy-MM-DD', prepend: true, maxFiles: 10 }) ] }); module.exports = { logger }; Then in any file where you want to use logging import the module like const logger = require('./utils/logger').logger; Use logger like the following: logger.info('Info service started'); logger.error('Service crashed');
if you are using forever to keep your node app running, then typing forever list will show you the path to the log file that console.log is writing too
You can use the nodejs Console constructor const mylog = new console.Console( fs.createWriteStream("log/logger.log"), fs.createWriteStream("log/error.log") ); And then you can use it just like the normal console class, eg: mylog.log("Ok!"); // Will be written into 'log/logger.log' mylog.error("Bad!"); // Will be written into 'log/error.log'
I took on the idea of swapping the output stream to a my stream. const LogLater = require ('./loglater.js'); var logfile=new LogLater( 'log'+( new Date().toISOString().replace(/[^a-zA-Z0-9]/g,'-') )+'.txt' ); var PassThrough = require('stream').PassThrough; var myout= new PassThrough(); var wasout=console._stdout; myout.on('data',(data)=>{logfile.dateline("\r\n"+data);wasout.write(data);}); console._stdout=myout; var myerr= new PassThrough(); var waserr=console._stderr; myerr.on('data',(data)=>{logfile.dateline("\r\n"+data);waserr.write(data);}); console._stderr=myerr; loglater.js: const fs = require('fs'); function LogLater(filename, noduplicates, interval) { this.filename = filename || "loglater.txt"; this.arr = []; this.timeout = false; this.interval = interval || 1000; this.noduplicates = noduplicates || true; this.onsavetimeout_bind = this.onsavetimeout.bind(this); this.lasttext = ""; process.on('exit',()=>{ if(this.timeout)clearTimeout(this.timeout);this.timeout=false; this.save(); }) } LogLater.prototype = { _log: function _log(text) { this.arr.push(text); if (!this.timeout) this.timeout = setTimeout(this.onsavetimeout_bind, this.interval); }, text: function log(text, loglastline) { if (this.noduplicates) { if (this.lasttext === text) return; this.lastline = text; } this._log(text); }, line: function log(text, loglastline) { if (this.noduplicates) { if (this.lasttext === text) return; this.lastline = text; } this._log(text + '\r\n'); }, dateline: function dateline(text) { if (this.noduplicates) { if (this.lasttext === text) return; this.lastline = text; } this._log(((new Date()).toISOString()) + '\t' + text + '\r\n'); }, onsavetimeout: function onsavetimeout() { this.timeout = false; this.save(); }, save: function save() { fs.appendFile(this.filename, this.arr.splice(0, this.arr.length).join(''), function(err) { if (err) console.log(err.stack) }); } } module.exports = LogLater;
I just build a pack to do this, hope you like it ;) https://www.npmjs.com/package/writelog
I for myself simply took the example from winston and added the log(...) method (because winston names it info(..): Console.js: "use strict" // Include the logger module const winston = require('winston'); const logger = winston.createLogger({ level: 'info', format: winston.format.json(), transports: [ // // - Write to all logs with level `info` and below to `combined.log` // - Write all logs error (and below) to `error.log`. // new winston.transports.File({ filename: 'error.log', level: 'error' }), new winston.transports.File({ filename: 'combined.log' }) ] }); // // If we're not in production then log to the `console` with the format: // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` // if (process.env.NODE_ENV !== 'production') { logger.add(new winston.transports.Console({ format: winston.format.simple() })); } // Add log command logger.log=logger.info; module.exports = logger; Then simply use in your code: const console = require('Console') Now you can simply use the normal log functions in your file and it will create a file AND log it to your console (while debugging/developing). Because of if (process.env.NODE_ENV !== 'production') { (in case you want it also in production)...
Create a utils/logger.js file with: var fs = require('fs'); var util = require('util'); var log_file = fs.createWriteStream(__dirname + '/../logs/server.log', { flags: 'w' }); var log_stdout = process.stdout; console.log = function () { // [...arguments].forEach(element => { log_file.write(util.format(element) + '\n'); log_stdout.write(util.format(element) + '\n'); }); }; module.exports = { console } Include the logger.js file from any file where you want to console.log like: const console = require('./utils/logger').console; Create a logs folder and create an empty server.log file in it and run your app :)
Rudy Huynh's solution worked really well for me. I added a little bit to have it spit out files with today's date and time. var dateNow = new Date(); var timeNow = dateNow.getHours() + '-' + dateNow.getMinutes(); var logPath = "log/" + dateNow.toDateString() + ' -' + ' Start Time - ' + timeNow + ".log" consoleLogToFile({ logFilePath: logPath }); It's not very elegant but this way it'll save different, easy to read, log files instead of just updating the same "default.log" file.
Based on multiparameter version by Clément, just without color codes for the text file var fs = require('fs'); var util = require('util'); var logFile = fs.createWriteStream('log.txt', { flags: 'a' }); // Or 'w' to truncate the file every time the process starts. var logStdout = process.stdout; console.log = function () { // Storing without color codes logFile.write(util.format.apply(null,arguments).replace(/\033\[[0-9;]*m/g,"") + '\n'); // Display normally, with colors to Stdout logStdout.write(util.format.apply(null, arguments) + '\n'); } Note: Answering since I couldn't comment