Adding logging with Winston

Let's start with a simple, basic need: logging. Having solid, correct logging set up can help you find problems quickly, while incomplete or otherwise lacking logging can make you look for hours for what could be a simple, trivial problem. A basic rule for any application is to ensure that proper login is set up so that you can be confident that any situation that comes up will at least be recognized and recorded for future analysis.

The first idea you could have is to just use the console family of functions, such as console.log(), console.warn(), console.info(), and more. (For a complete reference, check out https://developer.mozilla.org/en-US/docs/Web/API/console.) While these are good for quick debugging, they just don't do it for application-level logging. You should be able to select what kind of logs you want (everything? Errors only?) to decide what logs you see depending on your environment (for example, you'd want to see some kinds of logs in development but not in production), or even to enable or disable logging. And, finally, we want to have some control over the provided information:

  • Timestamping, to learn at what time each log was written
  • Text formatting, so logging can be understandable by humans, but also parseable by applications
  • Level setting, usually on a scale ranging from error (the most serious) through warning, informative, verbose, and ending with debugging and silly (yes, truly!)
  • Destination picking, like stdout or stderr, the filesystem, and so on

If you look around in the npm listings, you'll find many modules that can do logging: some are generic tools, and other are more specific ones. In our case, we'll use Winston for generic, application-level logging, and we'll turn to a different tool, Morgan, which is specifically tailored for HTTP traffic logging, as we'll see in the next section.

You can learn more about Winston at https://github.com/winstonjs/winston.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.246