question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

writing in both files

See original GitHub issue

This is writing in both log file success and error.log if i called with log.fail I would expect it should write in error-date.log file. I don’t know what’s wrong here

const fs = require('fs');
var winston = require('winston');
const env = process.env.NODE_ENV;
const logDir = 'logs';

if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const now = new Date();
var logger = winston.createLogger({
    transports: [
        new (require('winston-daily-rotate-file'))({
            filename: `${logDir}/success-%DATE%.log`,
            timestamp: now,
            datePattern: 'DD-MM-yyyy',
            prepend: true,
            json: false,
            level: 'info',
        }),
        new (require('winston-daily-rotate-file'))({
            filename: `${logDir}/error-%DATE%.log`,
            timestamp: now,
            datePattern: 'DD-MM-yyyy',
            prepend: true,
            json: false,
            level: 'error',
        }),
    ],
    exitOnError: false,
});
const log = {
    fail: function (ee) {
        logger.error(`${now.toISOString()} - ${JSON.stringify(ee)}`);
    },
    success: function (ee) {
        logger.info(`${now.toISOString()} - ${JSON.stringify(ee)}`);
    },
};
module.exports = log;

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mattberthercommented, Aug 2, 2020

This is not really how levels work in log mechanisms. What you could do, if this is a requirement, is to have your log module contain 2 loggers, each with a separate transport configured. For example:

const fs = require('fs');
var winston = require('winston');
const env = process.env.NODE_ENV;
const logDir = 'logs';

if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const now = new Date();
var errorLogger = winston.createLogger({
    transports: [
        new (require('winston-daily-rotate-file'))({
            filename: `${logDir}/error-%DATE%.log`,
            timestamp: now,
            datePattern: 'DD-MM-yyyy',
            prepend: true,
            json: false,
            level: 'error',
        }),
    ],
    exitOnError: false,
});

var successLogger = winston.createLogger({
    transports: [
        new (require('winston-daily-rotate-file'))({
            filename: `${logDir}/success-%DATE%.log`,
            timestamp: now,
            datePattern: 'DD-MM-yyyy',
            prepend: true,
            json: false,
            level: 'info',
        }),
    ],
    exitOnError: false,
});

const log = {
    fail: function (ee) {
        errorLogger.error(`${now.toISOString()} - ${JSON.stringify(ee)}`);
    },
    success: function (ee) {
        successLogger.info(`${now.toISOString()} - ${JSON.stringify(ee)}`);
    },
};
module.exports = log;
0reactions
indraraj26commented, Aug 2, 2020

Hi @mattberther , Is there any way not to write error i.e level: error log in info log, Basically i would like to create two separate log based on success and error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python - Writing from a file to two files
My current script creates both files. The DOB.txt file is populated but makes duplicate lines. The POSTCODE.txt file is empty. Any help will...
Read more >
How to write the difference between two files into a file
If you want the difference between the two files, use diff rather than comm . e.g. diff -u a.txt b.txt > c.txt.
Read more >
Reading and Writing to text files in Python
There are two types of files that can be handled in python, normal text ... Read and Write ('r+'): Open the file for...
Read more >
Reading and Writing in Text Files with multiple programs ...
Reading and Writing in Text Files with multiple programs accessing it simultaneously · Read – Gives read access to the file. · ReadWrite...
Read more >
Reading and Writing Files in Python (Guide)
In this tutorial, you'll learn about reading and writing files in Python. You'll cover everything from what a file is made up of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found