sidequest icon indicating copy to clipboard operation
sidequest copied to clipboard

Add ability to switch default winston logger with a user-defined logger

Open lexuzieel opened this issue 1 year ago • 11 comments

Thank you for the great queue worker! I'm currently in the process of integrating into my existing AdonisJS app, which uses pino logger as a built-in solution.

This results in two different style of logging in my app, here is the example:

[ info ] starting HTTP server...
[info] [2025-11-03 00:10:05] [Sidequest] [Engine] : Starting Sidequest using backend @sidequest/postgres-backend
[info] [2025-11-03 00:10:06] [Sidequest] [Worker] : Starting worker with provided configuration...
[00:10:06.892] INFO (30): Sidequest started! Dashboard: http://localhost:undefined
[00:10:06.916] INFO (30): started HTTP server on 0.0.0.0:3333

I would like to somehow swap winston logger with pino logger, but ideally sidequest should support any logger through some sort of adapter.

However I am not sure how to go about this currently, since current code heavily relies on winston logger throughout the codebase: https://github.com/sidequestjs/sidequest/blob/master/packages/core/src/logger.ts

Maybe we could implement an adapter API that is compatible with winston format and make the user write their own adapter? After all, most of the loggers have pretty similar API. For example, the core difference between winston and pino is the order of the arguments.

Which essentially boils down to:

Winston:

logger.info('hello', { message: 'world' })

Pino:

logger.info({ message: 'world' }, 'hello')

lexuzieel avatar Nov 03 '25 00:11 lexuzieel

Hey @lexuzieel. Thanks for the contribution. Yep, it seems like a great idea. I wouldn't be too hard to implement this actually. With an Adapter and a few interfaces, we could make it work :)

I will talk to @merencia about this.

GiovaniGuizzo avatar Nov 03 '25 12:11 GiovaniGuizzo

@GiovaniGuizzo I think that idea is a good addition to oss.

Do you know if the loggers are following any standard or should we add something to approach possible differences between them?

merencia avatar Nov 05 '25 12:11 merencia

I'd also be keen to see this; similar use case, pino via fastify

robcresswell avatar Nov 05 '25 21:11 robcresswell

One relatively simple way, backwards-compatible, low risk way to implement this would be to add an extra loggerInstance parameter to the sidequest setup, that accepts anything that implements a minimal logger interface (I'm guessing it needs something like child(), info(), error() etc etc.

That's how Fastify does it, and it seems to work fairly well: https://fastify.dev/docs/latest/Reference/Logging/#using-custom-loggers

robcresswell avatar Nov 05 '25 21:11 robcresswell

Yup, that's the answer. An Adapter Design Pattern :)

GiovaniGuizzo avatar Nov 05 '25 21:11 GiovaniGuizzo

Yup, for sure. I guess I meant more that rather than trying to make your existing logger config match it, you could add a new config key to keep the API change low risk.

robcresswell avatar Nov 05 '25 21:11 robcresswell

+1 to pino support

drewbaker avatar Nov 06 '25 01:11 drewbaker

Hi! I'd like to work on this.

One constraint I hit while reading the code: by default the engine runs in a forked process and jobs run in piscina worker threads, and the config reaches both over IPC / postMessage. A logger instance can't cross either boundary (the fork drops its methods, the thread throws DataCloneError), and most of the lines in the example above are logged from the fork. So a plain loggerInstance option would only work with fork: false + runner: "inline".

Plan:

  • Add a small SidequestLogger interface in @sidequest/core (debug/info/warn/error, called as (message, ...meta)).
  • Load it from a module path, the same way backend.driver and jobsFilePath work, so the main process, the fork and every thread import it: logger: { module: "./sidequest.logger.js" }.
  • Keep winston inside and send its entries to the custom logger through a transport. logger(), the exported Logger type and all call sites stay the same, and nothing changes when the option isn't set.
  • Tests for the transport and for a job's logger("MyJob") call reaching the custom logger. Docs: a pino adapter example in the logging guide.

Does the module-path approach work for you? The alternative is to accept an instance and forward entries from the fork and threads back to the main process over IPC. That works with app.log directly, but adds more moving parts to the engine.

bhupinder759 avatar Oct 01 '26 19:10 bhupinder759

Hey @bhupinder759 I think dynamically loading a logger adapter works. However, I would refrain from trying to load a logger form a specific file path, this has caused a lot of problems for us when loading jobs. Instead, something like the backend driver (an importable dpendency) would work fine.

Alternatively, we can have a bunch of adapters in a registry and not do it dynamic, just load it statically with all the available adapters: logger: { adapter: "pino" | "winston" }

GiovaniGuizzo avatar Oct 01 '26 19:10 GiovaniGuizzo

Thanks, that makes sense. Let's go with the registry then: logger: { adapter: "winston" | "pino" }, with winston as the default so nothing changes for existing users.

Plan:

  • adapter: "pino" creates the pino logger inside Sidequest (in the main process, the fork and each thread), so nothing has to cross the process boundary. An optional logger.options is passed to pino() for things like base, redact or transport, and logger.level still sets the level.
  • logger() and its call sites stay the same. Entries are forwarded from winston to the selected adapter through a transport.
  • Tests for the pino adapter (message, scope, metadata, errors, levels), plus docs in the logging guide and the configuration table.

One question: should pino be a regular dependency of @sidequest/core, or an optional peer dependency that users install themselves, like the backend drivers? I'd lean towards the optional peer so the default install stays the same.

bhupinder759 avatar Oct 01 '26 20:10 bhupinder759

@bhupinder759 Yeah, optional peer dependency is better. Remember to document it thoroughly, like we did for the backend drivers (emphasizing that you must install pino if you want to use pino adapter).

Side note: this is a perfect example of the strategy design pattern, so let's see your designing skills here :)

GiovaniGuizzo avatar Oct 01 '26 20:10 GiovaniGuizzo