0

I've created Node js with express and currently I use the console.log to log message and morgan for expresss...for production use what is recommended approach to use for error handling and logging ,there is recommended modules to use? Examples will be very useful!

I try with the following

module.exports = function () {
    var logger = new winston.Logger({
        levels: {
            info: 1
        },
        transports: [
            new (winston.transports.File)({
                level: 'info',
                filename: path.join(process.cwd(), '/logs/log.json'),
            })
        ]
    });
}
1
  • For production i use loggly.com/docs/nodejs-logs or another external service. Commented Jul 18, 2015 at 19:10

1 Answer 1

1

I have used winston in the past quite effectively. In the below excerpt we are creating a custom log level called info such that we can call logger.info to log messages. I believe there are numerous default levels defined on winston which are well documented.

The second part is to define a transport. In winston this is essentially a storage device for your logs. You can define multiple transports in the array including Console logging, File logging, Rotated file logging, etc... These are all well documented here. Im my example I have created a file transport where the log file is located under log/logs.json within the root of the application. Every time I now call logger.info('blah blah blah') I will see a new log entry in the file.

var winston = require('winston'),
  , path = require('path')

// Log to file.
var logger = new winston.Logger({
  levels: {
    info: 1
  },

  transports: [
    new (winston.transports.File)({
      level: 'info',
      filename: path.join(process.cwd(), '/log/logs.json'),
    })
  ]
});

// Write to log.
logger.info("something to log");
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks,Can you please elaborate in the answer how it works?2.what is transport & and file name ?
Thanks voted up!What do you mean by storage ?inside my project like txt file ?
storage meaning just where the output for your logs will be stored. This could be a file like a .txt (and could be inside your project or anywhere else on the filesystem) or could simply be the console (where there would be no permanent storage of the logs).
Can you please provide example how to put the file inside my project?which path should I use asssume that Im creating new folder under the root and I call it logs and put there json file which is called logs.json ,thanks in advance!
I have updated the answer further to use the current working directory (cwd) of the application to save the log file under log/logs.json. Hope that helps
|

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.