#Need server Logging or logger for nuxt 3 latest

1 messages · Page 1 of 1 (latest)

primal breach
#

i want a way to log what is happening in server side of the nuxt, just like how laravel has log file.

opaque wedgeBOT
#

Hi @primal breach. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Certainly! Nuxt 3 provides several options for server-side logging. Here are a few approaches you can use:

  1. Built-in Logging:
    Nuxt 3 has a built-in logging system that you can use in your server-side code. You can access it using the useLogger function from @nuxt/kit. Here's an example:

    import { defineNuxtModule, useLogger } from '@nuxt/kit'
    
    export default defineNuxtModule({
      setup(options, nuxt) {
        const logger = useLogger('my-module')
        logger.info('Hello from my module!')
      }
    })
    

    This logger is based on consola and provides various log levels and formatting options. Source

  2. Server Middleware Logging:
    You can create a server middleware to log requests. Here's an example:

    // server/middleware/log.ts
    export default defineEventHandler((event) => {
      console.log('New request: ' + getRequestURL(event))
    })
    

    This will log every incoming request to your server. Source

  3. Nitro Plugins:
    You can create a Nitro plugin to add more advanced logging capabilities:

    // server/plugins/nitroPlugin.ts
    export default defineNitroPlugin((nitroApp) => {
      console.log('Nitro plugin', nitroApp)
      // Add your custom logging logic here
    })
    

    This allows you to hook into Nitro's lifecycle events and add custom logging. Source

#
  1. Development Logs Feature:
    Nuxt 3 has a feature that records all console events on the server (both Nitro runtime and Vue runtime) and streams them to the client. This can be useful for debugging. You can enable this feature by setting features.devLogs in your Nuxt configuration. Source

While these methods don't create a log file like Laravel, they provide ways to log server-side events. If you need to write logs to a file, you might need to implement a custom solution or use a third-party logging library that supports file output.

Remember, for production environments, you should consider using a more robust logging solution that can handle high volumes of logs and provide features like log rotation and centralized log management.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@primal breach Kapa.ai is still learning and improving, please let me know how I did by reacting below.

wintry prawn
#

it is built in and on by default, you would need to capture the output though, which depends on your hosting setup

primal breach
#

if my site is hosted on aws using nginx what would be the best way to capture the outputs?

wintry prawn
#

how are you deploying? generate?

#

you won't get logs from static files, only if you are running a built nuxt server

#

which you can't really "run" with nginx, you would really want pm2 or something similar

primal breach
#

We have setup of pm2 @wintry prawn

wintry prawn
#

then you can use pm2 to view your logs

#
# Display all apps logs in realtime
pm2 logs

# Display only `api` application logs
pm2 logs api

# Display new logs in json
pm2 logs --json

# Display 1000 lines of api log file
pm2 logs big-api --lines 1000
You can also check logs with the CLI dashboard:

primal breach
#

Is this a normal practice?

#

@wintry prawn

wintry prawn
#

yes… there’s also a whole web interface they have for it

mortal pecan
#

I'm kind of in the same boat - I need to completely override Nuxt's default logging.

It appears that the behavior is different when in dev mode vs build / preview. I'm only concerned about build / preview / deployed.

Is there a way to capture all default Nuxt / Nitro logging and erroring to not output by default so that I can create my own custom format?

primal breach
#

I will check it

primal breach
#

@mortal pecan

primal breach
#

Anything that we can use pm2 logs to view in dashboard style logs for monitoring purpose in real time?

wintry prawn
#

did you research pm2 at all ?

https://pm2.keymetrics.io/

Advanced process manager for production Node.js applications. Load balancer, logs facility, startup script, micro service management, at a glance.

#

there's a whole web application they have that gives you a lot more than logs

primal breach
#

Alright I will research in deep on it.

#

Just glanced over it

wintry prawn
#

it's running your apps! haha i would take some time and look at what it can do 🙂