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

How to add new appenders dynamically. #1415

Closed
mai1x9 opened this issue Jan 19, 2024 · 3 comments
Closed

How to add new appenders dynamically. #1415

mai1x9 opened this issue Jan 19, 2024 · 3 comments

Comments

@mai1x9
Copy link

mai1x9 commented Jan 19, 2024

Programatically how can we add new appenders to log to new file. I want to make general function where i can create new logger with proper names and write to its own file.

But log4js seems to be called only once.

I wrote a general code,


class Logger{
  /**
   * General logger class
   * name: Name of the logger.
   * options is JSON with keys
   * filename|level|maxsize|backups
   */
  constructor(name="", options={}) {
    options.filename = options.filename || "errors.log"

    let __log4js = require("log4js")
    __log4js.configure({
      appenders: {
          default: {
            type: 'file',
            filename: '/home/ubuntu/default-errors.log',
            maxLogSize: '5M',
            backups: 6,
            compress: true
          },
          console: { type: 'console' },
          [name]:  {
            type: "file",
            filename: path.join(__logdir, options.filename),
            maxLogSize: options.maxsize || process.env.LOG_MAX_SIZE || "5M",
            backups: options.backups || +process.env.LOG_MAX_BACKUPS || 6,
            compress: true,
          }
      },
      categories: { 
          default: { 
              appenders: [ 'default' ], level: 'debug' 
          },
          [name]: {
            appenders: [name],
            level: options.level || process.env.LOG_LEVEL || "debug",
          }
      }
    })

    this.logger = __log4js.getLogger(name);
    debug(`Created new logger instance with the log name: ${name}, logs will be saved at: ${__logdir ? path.join(__logdir, options.filename) : path.join(homedir, ".logs", options.filename)}`)
  }
}

Using this class providing log name, and file path, i wanted to create multiple log instances and log to sep files dynamically. Different files may have different log methods.

But this does not seem working as some times it does not get logged. I do not know why ?

@lamweili
Copy link
Contributor

lamweili commented Jan 24, 2024

If you maintain a copy of your configuration, you can modify it and reload log4js by log4js.configure(config).

var log4js = require('log4js');
var logger = log4js.getLogger();

// load config
var config = {
 appenders: {
   out1: { type: 'stdout' },
 },
 categories: {
   default: { 
     appenders: [ 'out1' ],
     level: 'all'
   }
 }
};
log4js.configure(config);

logger.info("hello should appear once");

// adding new appenders
config.appenders.out2 = { type: 'stdout' };
config.categories.default.appenders.push('out2');

// reload config
log4js.configure(config);

logger.info("hello should appear twice");

@mai1x9
Copy link
Author

mai1x9 commented Jan 24, 2024

If you maintain a copy of your configuration, you can modify it and reload log4js but log4js.configure(config).

var log4js = require('log4js');
var logger = log4js.getLogger();

// load config
var config = {
 appenders: {
   out1: { type: 'stdout' },
 },
 categories: {
   default: { 
     appenders: [ 'out1' ],
     level: 'all'
   }
 }
};
log4js.configure(config);

logger.info("hello should appear once");

// adding new appenders
config.appenders.out2 = { type: 'stdout' };
config.categories.default.appenders.push('out2');

// reload config
log4js.configure(config);

logger.info("hello should appear twice");

Thanks @lamweili infact I have done same way. maintain copy of configurations and reload everytime when dynamically required.
That works. Thanks for reply.

@lamweili
Copy link
Contributor

Please close the issue if it has been resolved. Thanks!

@mai1x9 mai1x9 closed this as completed Jan 24, 2024
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