Jon Derring
Jon Derring

Reputation: 807

How to actually separate access log from error log in Node.js Express?

I've seen this question: Logging in express js to a output file?

But after reading documentation on logger middleware, still not sure how you can actually separate access log from error log, so that access log contains only url access data, and error log contains only errors.

Is there a way to do that?

Upvotes: 1

Views: 1554

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 145994

The built-in connect logger is dedicated to a single purpose: logging the basic details about incoming HTTP requests. You can control the exact format of the log messages with the configuration parameters, but fundamentally that middleware is for logging requests a la access.log. To log errors and have them go to a separate file, you'll need to write an error handling middleware with a signature of (error, req, res, next). You can wire that up with app.use and write your errors to a file or just stderr as you wish. To trigger it from your normal routes or middleware, just pass an Error instance to the next(error) callback.

Upvotes: 3

Related Questions