How it works...

Let's give this a whirl, using the same examples that we used for winston. Since we set the console output to show only warnings and errors, we'll just see an added pair of lines. Displaying [http] instead of [serv] helps finding them, among the rest of the console output:

.
.
.
2018-05-28T19:27:19.232Z [http] GET /invented (404) 9 - 0.886 ms
.
.
.
2018-05-28T19:27:23.771Z [http] GET /xyzzy (500) 21 - 0.925 ms
.
.
.

The (complete) HTTP log went into a file, and is just a list of all of the requests:

2018-05-28T19:27:16.871Z [http] GET /
2018-05-28T19:27:17.827Z [http] GET /
2018-05-28T19:27:19.231Z [http] GET /invented
2018-05-28T19:27:20.677Z [http] GET /
2018-05-28T19:27:23.770Z [http] GET /xyzzy
2018-05-28T19:27:25.296Z [http] GET /

Note that we opted to do an immediate logging, which means that all requests—even those that might cause everything to crash—get logged, but the outcome itself of the request is then not available. If you wish to also get that information—but, say, only for requests that caused some error—you might add a third morgan destination, sharing the same file stream, but only for errors, as shown in the following code snippet:

app.use(
morgan(
`:date[iso] [http] ` +
`:method :url (:status) :res[content-length] - :response-time ms`,
{
skip: (req, res) => res.statusCode < 400,
stream: morganStream
}
)
);

Using this, the log would then include more data, but only for the requests you picked: 

2018-05-28T19:36:54.968Z [http] GET /
2018-05-28T19:36:55.453Z [http] GET /
2018-05-28T19:36:56.011Z [http] GET /
2018-05-28T19:36:58.149Z [http] GET /invented
2018-05-28T19:36:58.151Z [http] GET /invented (404) 9 - 1.230 ms
2018-05-28T19:36:59.528Z [http] GET /
2018-05-28T19:37:00.033Z [http] GET /
2018-05-28T19:37:01.886Z [http] GET /xyzzy
2018-05-28T19:37:01.888Z [http] GET /xyzzy (500) 21 - 1.115 ms
2018-05-28T19:37:03.060Z [http] GET /
2018-05-28T19:37:03.445Z [http] GET /
2018-05-28T19:37:03.903Z [http] GET /
..................Content has been hidden....................

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