-1

How to create and save a log file in NodeJS for testing purpose?
The log file should contain the success/failure ratio of tests conducted.
I haven't tried anything yet as I am new to NodeJs.

3
  • success/failure ratio or success/failed messages ? Commented Nov 30, 2016 at 4:25
  • you can use loggers like winston Commented Nov 30, 2016 at 4:59
  • Close duplicate of stackoverflow.com/questions/8393636/… Commented Dec 1, 2016 at 9:16

2 Answers 2

12

Firstly do you know about npm?
If No, then read/research about it.
Else, then good. Look out for npm-modules suitable for logging task.
I shall recommend you winston-logger.

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.
log direcotry

I have used "accesslog" instead of "successlog" as a keyword in my project.
And I didn't get what you meant about 'success/failure ratio'. In order to get the ratio simply count the no. of times success and error logs are logged.

Sign up to request clarification or add additional context in comments.

3 Comments

I am using the exact configurations, but the log file is not generated. I see the logs in the console.
Is it possible to use it in ES Modules? I mean, with .mjs files
@MartínJF Well theoretically It should work if you map the require and module.exports correctly to import and export although I haven't worked on .mjs or .cjs based approaches. Try out and let us know. Thanks
2
require('mkdirp').sync('logs') // your log directory

var logger = require('log4js')

logger.configure({
    "appenders": [{
        "type": "console"
    }, {
        "type": "dateFile",
        "category": "log",
        "filename": "logs/exp",
        "pattern": "yyMMdd.log",
        "alwaysIncludePattern": true
    }],
    "replaceConsole": true
})

logger.info('success')
logger.error('fail')

1 Comment

try to add some explanation maybe? code-only answer are not very helpful in general. also, this doesn't answer OP question, it'll just forward everything to a file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.