2015-04-06 72 views
1

如果您閱讀log4js ,您將看到SMTP傳輸的示例,它甚至不適用於最新的nodemailer(我在寫作時使用nodemailer 1.3.0)。如何在log4js smtp appender中使用非SMTP傳輸?

文檔顯示這種惡劣的配置爲例:

{ "appenders": [ 
    { 
     "type": "smtp", 
     "recipients": "[email protected]", 
     "sendInterval": 60, 
     "transport": "SMTP", 
     "SMTP": { 
      "host": "smtp.gmail.com", 
      "secureConnection": true, 
      "port": 465, 
      "auth": { 
       "user": "[email protected]", 
       "pass": "bar_foo" 
      } 
     } 
    } 
] } 

如何更改配置使用其他交通工具比SMTP?我想要的是配置log4js使用SendGrid。

回答

2

經過幾個小時的試錯,我找到了解決方案。下面是例子log4js配置與控制檯和SMTP追加程序:

sendgridTransport = require('nodemailer-sendgrid-transport'); 

log4js.configure({ 
    appenders: [{ 
     type: 'console' 
    }, { 
     type: 'logLevelFilter', 
     level: 'ERROR', 
     appender: { 
      type: 'smtp', 
      recipients: '[email protected]', 
      sender: '[email protected]', 
      sendInterval: 60, 
      transport: sendgridTransport({ 
       auth: { 
        api_user: config.smtpUsername, 
        api_key: config.smtpPassword 
       } 
      }) 
     } 
    }] 
}); 

您可以用類似的方式使用任何其他交通工具。支持者傳輸是所有由nodemailer支持的傳輸(例如:AWS SES,SMTP,...)。請參閱您要使用的傳輸文檔以瞭解如何實例化它。