1

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, so if I understood you correctly, something like what I want does not exist yet, so I'll have to write it myself. Thanks, will try.
It may, but I didn't search google/npm for you. We're talking about ~20 lines of code max in a single function. Shouldn't be a big deal to write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.