9

What kind of a logging solution could be a good fit in SvelteKit if server-side rendering must be enabled?

  • Configurable logging levels

  • Logs to a file/stdout when server-side rendering is run with Node.js adapter

  • Logs to a console if any logging statements are encountered in client-side processing

  • Log level filters, timestamp, coloring and such features that are useful for dianogtics (otherwise console.log would be sufficient)

I am aware of multiple JavaScript logging solutions like Winston, but I am not sure if they are very good fit for SvelteKit model.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 2
    I use `debug` for this – Patrick Lee Scott Dec 16 '21 at 20:55
  • Could you elaborate in your question as to why do you think some logging solutions are not a good fit? SvelteKit is build on top of Vite so you've got access to import.meta.env.SSR and import.meta.env.DEV to initialize these logger with the appropriate settings. https://vitejs.dev/guide/env-and-mode.html – Bob Fanger Apr 22 '22 at 20:19

1 Answers1

2

You can use any agnostic logging library you wish at SvelteKit. Then you can use hooks to log events for both server and client side.

Reference:

Jan Cássio
  • 2,076
  • 29
  • 41
  • I believe hooks can only deal with exceptions. Can you clarify how this logging messages with hooks is done? – Mikko Ohtamaa Dec 26 '22 at 08:40
  • Server side hooks acts as middleware during pre rendering, rendering, fetch and error phases. Client hooks at error phases. Since both are just functions, you can choose any agnostic logging lib that fits better on your needs. In my case I use Sentry. Other thing you should keep in mind is, if you want to log things like clicks, inputs, system or other kind of events, you should do it at the place it happens in your code. – Jan Cássio Dec 26 '22 at 16:16
  • @JanCássio maybe you can update your original answer to integrate your last comment wich is the complete answer IMHO. thanks for it. – webofmars Mar 30 '23 at 07:44