Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Independent logger objects with different log levels #119

Open
vineyyadav opened this issue Jun 12, 2020 · 2 comments
Open

Independent logger objects with different log levels #119

vineyyadav opened this issue Jun 12, 2020 · 2 comments

Comments

@vineyyadav
Copy link

I want to use this library for a small project where I have two modules. I want to control the logging (primarily the log level) independently and on the fly for the two modules.

Is it possible to get multiple logger objects which are independent?

Something like:
module 1 has

const tracer = require('tracer');
const logger = tracer.console({ level: 'debug' });

function setLogLevel(level) {
    tracer.setLevel(level);
}

function m1foo() {
    logger.debug('m1foo:debug');
    logger.info('m1foo:info');
    logger.warn('m1foo:warn');
    logger.error('m1foo:error');
}

module.exports = {
    setLogLevel: setLogLevel,
    m1foo: m1foo
}

module 2 has

const tracer = require('tracer');
const logger = tracer.console({ level: 'debug' });

function setLogLevel(level) {
    tracer.setLevel(level);
}

function m2foo() {
    logger.debug('m2foo:debug');
    logger.info('m2foo:info');
    logger.warn('m2foo:warn');
    logger.error('m2foo:error');
}

module.exports = {
    setLogLevel: setLogLevel,
    m2foo: m2foo
}

And then using them like:

const m1 = require('./m1.js');
const m2 = require('./m2.js');

m1.setLogLevel('warn');

m1.m1foo();
m2.m2foo();

And expecting that the m1.setLogLevel does not change the log level for m2.

@baryon
Copy link
Owner

baryon commented Jun 13, 2020

setLevel method dynamically change the log level. It is a global settings, affect all output that are created via tracer.

a solution that write a config file, like:

const config = {
  module1_level: 'debug',
  module2_level: 'warn'
}

module.exports = config

then use it in module1 and module2

const config = require('./config')
const tracer = require('tracer');
const logger = tracer.console({ level: config.module1_level });

const config = require('./config')
const tracer = require('tracer');
const logger = tracer.console({ level: config.module2_level });

@vineyyadav
Copy link
Author

I tried it, but does not really help. When changing the log level for one module, it impacted the log level for the other one too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants