0

Not a node expert, and this is the first time I'm using log4js-node.

I am trying to get my ERROR logs and any of my console logs to write to a log_file.log file with log4js on a nodejs server running Express. Here is my config file:`

{
  "replaceConsole": true,
  "appenders": [
  {
    "type": "file",
    "filename":"log_file.log",
    "maxLogSize":20480,
    "backups": 3,
    "category":"relative-logger"
  },
  {
  "type":"logLevelFilter",
  "level":"ERROR",
  "appender":{
    "type":"file",
    "filename":"log_file.log"
    }
  },
  {

    "appender": {
      "type": "smtp",
      "recipients": "myemail@gmail.com",
      "sender": "myemailadd@gmail.com",
      "sendInterval": 60,
      "transport": "SMTP",
      "SMTP": {
        "host": "localhost",
        "port": 25
      }
    }
  }]
}`

And here is how I'm requiring the application in my app.js file:

var log4js = require("log4js");
log4js.configure("log_config.json")
logger = log4js.getLogger();

I'm sending manual errors to log4js with this (I can get this to log to the console fine, just can't get the log_file written):

logger.error('A mandrill error occurred: ' + e.name + ' - ' + e.message);

And I'm hoping jog4js catches the application's normal ERROR messages.

How do I get log4js to log to the log_file.log them send me an email of that log? I have installed nodemailer 0.7, fyi, to handle smtp.

adamgedney
  • 81
  • 1
  • 10
  • As Allenzzzxd suggests, remove the "category". That solved the issue for me, anyway. If I'm not mistaken, the category is for when you want to select different logging mechanisms for different occasions, so you can then do log4js.getLogger().log() or similar. – Bet Lamed Jan 26 '16 at 09:49

2 Answers2

1

maybe you could remove "category":"relative-logger" in your file appender.

Allenzzzxd
  • 28
  • 4
0

Yes remove "category":"relative-logger" it somehow blocks the data transfer into your log file.. Or try something like this:

// Setup Logging
log4js.configure({
    appenders: [
        { type: 'console' },
        { type: 'file', filename: '.\\logs\\PesaFastaArchiveData.log' }
    ]
});

The path is of-course the windows path.

Coder
  • 1
  • 1