T
TanStack8mo ago
flat-fuchsia

Logging on server

I've been looking into how I can handle logging all server-side errors using pino (primiarly in the defaultStreamHandler as I have errors that are not serialized in console)... I want to use standard logging format so GCP Cloud Run can pick up on it but at the moment everything is automatically console.<insert-method>. Are there plans to allow injecting a logger (similar to Fastify => https://fastify.dev/docs/latest/Reference/Server/#loggerinstance).
6 Replies
flat-fuchsia
flat-fuchsiaOP8mo ago
In short term to solve the readable issue, I added this to SSR.tsx
console.error = (_x, _y, z) => {
console.log(JSON.stringify(z.componentStack.split('\n'), null, 2));
};
console.error = (_x, _y, z) => {
console.log(JSON.stringify(z.componentStack.split('\n'), null, 2));
};
I figured out how to debug/find issue.
rare-sapphire
rare-sapphire8mo ago
that's a good idea. can you please create a github issue for the logger injection?
flat-fuchsia
flat-fuchsiaOP8mo ago
👍
flat-fuchsia
flat-fuchsiaOP8mo ago
GitHub
RFC: Custom logger on the start server · TanStack router · Discussi...
Problem Currently all of the server-side logging is done via good ole' console but I'd like to be able to use a dedicated logger across the board. This would allow developers to hook into l...
rival-black
rival-black4mo ago
@Zac in your rfc, you mention this could just be a nitro plugin. do you mean something like: services/logger.ts
import createLogger from 'pino';
const logger = createLogger();
const log = logger.info.bind(logger);
const error = logger.error.bind(logger);
const info = logger.info.bind(logger);
const debug = logger.debug.bind(logger);
const warn = logger.warn.bind(logger);
export { log, error, info, debug, warn }
import createLogger from 'pino';
const logger = createLogger();
const log = logger.info.bind(logger);
const error = logger.error.bind(logger);
const info = logger.info.bind(logger);
const debug = logger.debug.bind(logger);
const warn = logger.warn.bind(logger);
export { log, error, info, debug, warn }
plugins/nitro.logger.ts
import * as logger from './services/logger.ts';

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("request", (event) => {
logger.info("on request", event.path);
});

nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
logger.info("on response", event.path, { body });
});

nitroApp.hooks.hook("afterResponse", (event, { body }) => {
logger.info("on after response", event.path, { body });
});
});
import * as logger from './services/logger.ts';

export default defineNitroPlugin((nitroApp) => {
nitroApp.hooks.hook("request", (event) => {
logger.info("on request", event.path);
});

nitroApp.hooks.hook("beforeResponse", (event, { body }) => {
logger.info("on response", event.path, { body });
});

nitroApp.hooks.hook("afterResponse", (event, { body }) => {
logger.info("on after response", event.path, { body });
});
});
flat-fuchsia
flat-fuchsiaOP4mo ago
yes but I'm not super familiar with nitro and I know there are rumbles about removing it I think for now we are holding until start is a bit futher along to tackler take on*

Did you find this page helpful?