How it works...

After building the project, I ran the Winston logging code to catch all the produced logs. I tried it out with a sequence of calls, simulated with curl; we'll be looking at how to do this in more complex tasks in later sections in this chapter:

> curl localhost:8080/ 
Winston server!
> curl localhost:8080/
Winston server!
> curl localhost:8080/invented
NOT FOUND
> curl localhost:8080/
Winston server!
> curl localhost:8080/xyzzy
INTERNAL SERVER ERROR
> curl localhost:8080/
Winston server!
> curl localhost:8080/
Winston server!

The output on the console can be seen in the following screenshot. The normal lines are in green (yes, hard to see in a black and white book—sorry about that!), the warnings are yellow, and the errors are in red. The request for the non-existing /invented path ended in a warning, and the one for /xyzzy produced an error, since we tried to call a non-existent function:

 Winston's console output for a few dummy requests

What got logged to the different log files? According to our specification, only the warning and the error messages were stored. The text file is basically the same as the console output, and that makes sense because the format specification we selected for those two transports is exactly the same:

2018-05-28T00:29:06.651Z [serv] warn UNKNOWN ROUTE /invented
2018-05-28T00:29:11.214Z [serv] error GENERAL ERROR res.say_xyzzy is not a function
TypeError: res.say_xyzzy is not a function
at app.get (/home/fkereki/MODERNJS/chapter05/out/winston_server.js:60:9)
at Layer.handle [as handle_request] (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/layer.js:95:5)
at next (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/layer.js:95:5)
at /home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/index.js:335:12)
at next (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/index.js:275:10)
at app.use (/home/fkereki/MODERNJS/chapter05/out/winston_server.js:47:5)
at Layer.handle [as handle_request] (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/layer.js:95:5)

The JSON file, on the other hand, is a bit reduced: each line includes an object with the message and level attributes, because we didn't specify that anything in particular should be added. However, you can change that: read Winston's documentation at https://github.com/winstonjs/winston/blob/master/README.md, and you'll have plenty of available possibilities:

{"message":"UNKNOWN ROUTE /invented","level":"warn"}
{"message":"GENERAL ERROR res.say_xyzzy is not a function TypeError: res.say_xyzzy is not a function at app.get (/home/fkereki/MODERNJS/chapter05/out/winston_server.js:60:9) at Layer.handle [as handle_request] ...part of the text snipped out...
(/home/fkereki/MODERNJS/chapter05/out/winston_server.js:47:5) at Layer.handle [as handle_request] (/home/fkereki/MODERNJS/chapter05/node_modules/express/lib/router/layer.js:95:5)","level":"error"}

So, we have a flexible way to log just about whatever we want to, but our HTTP logging was, in particular, a bit too skimpy, and that's a good reason to include Morgan, as we'll see.

..................Content has been hidden....................

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