adds code

An effective logging solution is crucial to the success of any application. Winston is a versatile logging library and a popular logging solution available for Node.js applications. Winston’s features include support for multiple storage options, log levels, log queries, and a built-in profiler. In this tutorial, you will use Winston to log a Node/Express application that you’ll create as part of this process. You’ll also see how to combine Winston with Morgan, another popular HTTP request middleware logger for Node.js, to consolidate HTTP request data logs with other information. Useful links vps/vds - Commands used First npm install express-generator -g express myApp npm install nodemon -g cd myApp npm install ufw allow 3000 nodemon bin/www Second nano ~/myApp/app.js Third cd ~/myApp npm install winston mkdir ~/myApp/config mkdir ~/myApp/logs npm install app-root-path --save nano ~/myApp/config/winston.js WRITE IN const appRoot = require("app-root-path"); const winston = require("winston"); // define the custom settings for each transport (file, console) const options = { file: { level: "info", filename: `${appRoot}/logs/app.log`, handleExceptions: true, maxsize: 5242880, // 5MB maxFiles: 5, format: winston.format.combine( winston.format.timestamp(), winston.format.json() ), }, console: { level: "debug", handleExceptions: true, format: winston.format.combine( winston.format.colorize(), winston.format.simple() ), }, }; // instantiate a new Winston Logger with the settings defined above const logger = winston.createLogger({ transports: [ new winston.transports.File(options.file), new winston.transports.Console(options.console), ], exitOnError: false, // do not exit on handled exceptions }); // create a stream object with a 'write' function that will be used by `morgan` = { write: function (message, encoding) { // use the 'info' log level so the output will be picked up by both // transports (file and console); }, }; module.exports = logger; The Fourth nano ~/myApp/app.js tail ~/myApp/logs/app.log nano ~/myApp/app.js tail ~/myApp/logs/app.log
Previous Post Next Post