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

middleware before email appender selected #3

Closed
PinkyRabbit opened this issue Sep 13, 2018 · 4 comments
Closed

middleware before email appender selected #3

PinkyRabbit opened this issue Sep 13, 2018 · 4 comments

Comments

@PinkyRabbit
Copy link

PinkyRabbit commented Sep 13, 2018

Hello. I got an issue.

I try to use log4js-node + smtp in public project. I created module for my logger:

const log4js = require('log4js');
const path = require('path');
const config = require('../config/config');

const FILENAME = path.resolve(__dirname, '../errors.log');

log4js.configure({
  appenders: {
    out: {
      type: 'stdout',
    },
    toFile: {
      type: 'file',
      filename: FILENAME,
    },
    email: {
      type: 'smtp',
      recipients: config.email.me,
      transport: 'SMTP',
      SMTP: {
        host: 'smtp.gmail.com',
        port: '465',
        secure: true,
        sender: config.email.login,
        auth: {
          user: config.email.login,
          pass: config.email.password,
        },
      },
    },
  },
  categories: {
    default: {
      appenders: ['out'],
      level: 'debug',
    },
    dev: {
      appenders: ['out'],
      level: 'debug',
    },
    forErrors: {
      appenders: ['toFile', 'email'],
      level: 'error',
    },
  },
});

const useLogger = config.env === 'production' ? 'forErrors' : 'dev';
const logger = log4js.getLogger(useLogger);

module.exports = logger;

The problem is: because this is public project I cant select recipients by hands. I need to make a request to my database ( I use mongodb/mongoose) to grab superusers emails. How I can do this? How I can get all superadmins before send errors with logger's smtp. Thanks!

@nomiddlename
Copy link
Contributor

You're going to have to get the email addresses from the database before you configure log4js. If you're concerned about missing errors before log4js is configured, or errors when trying to contact the database to get the emails, then configure log4js to log to console (or a default email address) and then do a log4js.shutdown followed by a log4js.configure.

@PinkyRabbit
Copy link
Author

@nomiddlename thank you for response. But superadmins can be changed. And if I'll configured it on module init, then I can change it only if config will be as separate object?

@nomiddlename
Copy link
Contributor

When super admins change you'll have to call log4js.shutdown and then log4js.configure again. At the moment, there's no other way of changing the email recipients.

@nomiddlename
Copy link
Contributor

Closing this one, as I don't think there's anything to be fixed here (and it would need to be done in log4js core anyway).

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