Is it possible to have a Rolling File Logger and Console Logger at the same time?
Closed this issue · 9 comments
Hi there,
I am wanting to have a Rolling File Logger on all the time, and then use a command line arg to also turn on Console Logging at the same time. Is this possible and if so how do I configure Simple Node Logger to do this?
Thanks for your time, and a great product.
Regards,
Scott
yes, you can add new adapters for file, console, etc. see ./examples/category-logger.js
@darrylwest , Thanks for the reply.
I had a look at category-logger.js, I tried the following code:
manager.addAppender(manager.createRollingFileLogger(opts));
But I get the error:
manager.addAppender(manager.createRollingFileLogger(opts));
^
TypeError: manager.createRollingFileLogger is not a function
at Object.<anonymous> (C:\[path]\TestCategoryLogger.js:19:37)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:974:3
Can you provide an example of exactly how it is spost to work?
Thanks for your time.
sorry, I implemented the examples to run from the project root, so for mac/linux you would do this: node examples/category-logger.js
I'm guessing that for windows it would be node examples\category-logger.js but I have no way to test it.
@darrylwest Thanks again for the response.
The paths is not the problem. I can run the category script fine without error. I just need to update the require statements as shown below.
But if I modify it to try and use the rolling file logger it fails with the error I mentioned:
manager.addAppender(manager.createRollingFileLogger(opts));
In windows, once the module is installed, I do the require as follows, and this works fine:
var SimpleLogger = require('simple-node-logger'),
manager = new SimpleLogger();
Here is the full script that generated the error:
var SimpleLogger = require('simple-node-logger'),
log_manager = new SimpleLogger(),
opts = {
logDirectory: 'c:\users\[username]', // Replace with username of logged in user
fileNamePattern: 'SURPASSClientAssistApp-<DATE>.log',
dateFormat: 'YYYY.MM.DD'
},
log1,
log2;
log_manager.addAppender(log_manager.createConsoleAppender() );
log_manager.addAppender(log_manager.createRollingFileLogger(opts));
log1 = log_manager.createLogger( 'CategoryOne', 'trace' );
log2 = log_manager.createLogger( 'CategoryTwo', 'trace' );
log1.trace('this is a simple trace log statement (should not show)');
log1.debug('this is a simple debug log statement (should not show)');
log1.info('this is a simple info log statement/entry');
log2.warn('this is a simple warn log statement/entry');
log1.error('this is a simple error log statement/entry');
log2.fatal('this is a simple fatal log statement/entry');
log2.trace('this is a simple trace log statement (should show)');
log1.debug('this is a simple debug log statement (should show)');
var loggers = log_manager.getLoggers();
loggers.forEach(logger => {
console.log('stats: ', logger.getCategory(), logger.getStats());
});
Thanks again for your time.
I see whats happening--what you want to do is create an Appender, not a full logger (that's what the manager is. so, you should be invoking this:
log_manager.addAppender(log_manager.createRollingFileAppender(opts));
-- darryl
Cool that solved the problem, thanks very much for sharing the logger and the support!
@darrylwest, Actually I now have a new problem.
I have the following script:
var _log_opts = {
timestampFormat: 'YYYY-MM-DD HH:mm:ss:SSS'
},
_log_manager = new SimpleLogger({ level: 'all' });
_log_manager.addAppender(_log_manager.createConsoleAppender(_log_opts));
var _log = _log_manager.createLogger('TestCat');
_log.info('here is the test');
The problem is that it productions duplicate log entries in the console.
2017-06-19 11:30:07:704 INFO TestCat here is the test
2017-06-19 11:30:07:704 INFO TestCat here is the test
Thanks again for your time.
As a follow up I find that if I modify the script to the following I no longer get duplicates:
var _log_opts = {
timestampFormat: 'YYYY-MM-DD HH:mm:ss:SSS'
},
_log_manager = new SimpleLogger({ level: 'all' });
_log_manager.createConsoleAppender(_log_opts);
var _log = _log_manager.createLogger('TestCat');
_log.info('here is the test');
So just removing this the _log_manager.addAppender(
fixed it.
@codeowl1 Thanks, I had this exact problem. I assumed you'd need to add the appender after creating it. Thanks for following up, much appreciated