Nuxt SSR: Logging with response time into access.log and error.log

Viewed 578

I use Nuxt with SSR and would like to have something similar to apaches access.log and error.log

Especially I'm interested in the response time for each call.

I couldn't find anything in the nuxt documentation. Do I have to implement this feature on my own?

1 Answers

You can add a custom server middleware that uses morgan:

At its simplest, the middleware would look like:


    // server-middleware/logging.js
    import morgan from 'morgan';
    export default morgan('combined');

In your Nuxt config:


    // nuxt.config.js
    export default {
      serverMiddleware: [
        '~/server-middleware/logging'
      ]
    };

This configuration will result in "combined" format logging to stdout. Consult the morgan package's documentation to see how to tailor the output to include response time, and to log to files instead of stdout.

Related