simple-node-logger icon indicating copy to clipboard operation
simple-node-logger copied to clipboard

Can I disable console output and only leave file output?

Open amirsaranBIH opened this issue 4 years ago • 3 comments

I am trying to create a system where in development mode I write errors to log files and also to the console, but in production I only want to write to log file.

This is how my logger looks like:

export const Log = simpleNodeLogger.createRollingFileLogger({
    errorEventName: 'error',
    logDirectory: 'logs',
    fileNamePattern: 'log-<DATE>.log',
    dateFormat: 'YYYY-MM-DD'
});

if (process.env.NODE_ENV === 'production') {
    // disable console output, only leave file write
}

I haven't found any config parameters for disabling console logging. I also tried using appenders, but I don't understand them at all and I didn't have luck.

amirsaranBIH avatar Nov 04 '20 21:11 amirsaranBIH

Yes, i need this too. Please anyone can solve this problem?

ashafizullah avatar Jan 26 '21 01:01 ashafizullah

I started using this lib today and needed this function as well, and after a quick look through the source this can simply be accomplished by:

import SimpleLogger from 'simple-node-logger'; // const SimpleLogger = require('simple-node-logger');

function createLogger(enableConsole, opts) { // opts is the normal opts you'd pass in like logFilePath or timestampFormat
  const manager = new SimpleLogger(opts);
  if (enableConsole) {
    manager.createConsoleAppender(opts);
  }
  if (opts.logFilePath) {
    manager.createFileAppender(opts);
  }
  return manager.createLogger();
}

...

const myLogger = createLogger(false, {
  logFilePath: path.join(__dirname, `logs/MyLog.log`),
  timestampFormat: 'YY-MM-DD HH:mm:ss.SSS'
});

myLogger.info('Test log out string');

IT-MikeS avatar Apr 01 '21 15:04 IT-MikeS

Hello, facing the same problem, no need to create a custom function, just use

createSimpleFileLogger

const logger = require('simple-node-logger').createSimpleFileLogger('./logs/success.log');
const logger2 = require('simple-node-logger').createSimpleFileLogger('./logs/errors.log');

mauriblint avatar Nov 01 '23 11:11 mauriblint