chore(logger): get correct error in logs
This commit is contained in:
@@ -83,7 +83,16 @@ const startServer = async () => {
|
||||
done();
|
||||
});
|
||||
|
||||
const ignoreLog = ['/healthcheck', '/metrics', '/misc'];
|
||||
const ignoreMethods = ['OPTIONS'];
|
||||
|
||||
fastify.addHook('onRequest', (request, reply, done) => {
|
||||
if (ignoreMethods.includes(request.method)) {
|
||||
return done();
|
||||
}
|
||||
if (ignoreLog.some((path) => request.url.startsWith(path))) {
|
||||
return done();
|
||||
}
|
||||
if (request.url.includes('trpc')) {
|
||||
request.log.info('request incoming', {
|
||||
url: request.url.split('?')[0],
|
||||
@@ -100,6 +109,12 @@ const startServer = async () => {
|
||||
});
|
||||
|
||||
fastify.addHook('onResponse', (request, reply, done) => {
|
||||
if (ignoreMethods.includes(request.method)) {
|
||||
return done();
|
||||
}
|
||||
if (ignoreLog.some((path) => request.url.startsWith(path))) {
|
||||
return done();
|
||||
}
|
||||
if (request.url.includes('trpc')) {
|
||||
request.log.info('request done', {
|
||||
url: request.url.split('?')[0],
|
||||
|
||||
@@ -12,9 +12,32 @@ const logLevel = process.env.LOG_LEVEL ?? 'info';
|
||||
export function createLogger({ name }: { name: string }): ILogger {
|
||||
const service = `${name}-${process.env.NODE_ENV ?? 'dev'}`;
|
||||
|
||||
const format = process.env.HYPERDX_API_KEY
|
||||
? winston.format.json()
|
||||
: winston.format.prettyPrint();
|
||||
const prettyError = (error: Error) => ({
|
||||
...error,
|
||||
stack: error.stack,
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
const errorFormatter = winston.format((info) => {
|
||||
if (info instanceof Error) {
|
||||
return {
|
||||
...info,
|
||||
...prettyError(info),
|
||||
};
|
||||
}
|
||||
if (info.error) {
|
||||
return {
|
||||
...info,
|
||||
error: prettyError(info.error),
|
||||
};
|
||||
}
|
||||
return info;
|
||||
});
|
||||
|
||||
const format = winston.format.combine(
|
||||
errorFormatter(),
|
||||
winston.format.json(),
|
||||
);
|
||||
|
||||
const transports: winston.transport[] = [new winston.transports.Console()];
|
||||
if (process.env.HYPERDX_API_KEY) {
|
||||
|
||||
Reference in New Issue
Block a user