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": "[email protected]",
      "sender": "[email protected]",
      "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.

1
  • 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(<category>).log(<msg>) or similar. Commented Jan 26, 2016 at 9:49

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Please elaborate why that might solve the issue by editing your answer.
Thanks for the hint. I had the same problem, I tried removing the "category", and it solved the same problem for me.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.