Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:logger): adapt response logging to nestjs-pino v5

johaven0d56a0b365d4
diff --git a/backend/package.json b/backend/package.json
index ab2da0ed..35434e79 100755
--- a/backend/package.json
+++ b/backend/package.json
@@ -70,13 +70,15 @@
"ldapts": "^9.0.0",
"mime-types": "^3.0.2",
"mysql2": "^3.23.1",
- "nestjs-pino": "^4.6.1",
+ "nestjs-pino": "^5.3.1",
"nodemailer": "^10.0.15",
"openid-client": "^6.8.4",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
+ "pino": "^10.4.0",
"pino-abstract-transport": "^3.0.0",
+ "pino-http": "^11.0.0",
"pino-pretty": "^13.2.0",
"qrcode-generator": "^2.0.4",
"redis": "^5.12.1",
diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts
index 99c07dec..2bd001b3 100755
--- a/backend/src/app.module.ts
+++ b/backend/src/app.module.ts
@@ -8,6 +8,7 @@ import { ApplicationsModule } from './applications/applications.module'
import { AuthModule } from './authentication/auth.module'
import { configuration, exportConfiguration } from './configuration/config.environment'
import { configLogger } from './configuration/config.logger'
+import type { FastifyLoggerRequest } from './configuration/interfaces/logger.interface'
import { AvailabilityModule } from './infrastructure/availability/availability.module'
import { CacheModule } from './infrastructure/cache/cache.module'
import { ContextModule } from './infrastructure/context/context.module'
@@ -18,7 +19,7 @@ import { SchedulerModule } from './infrastructure/scheduler/scheduler.module'
@Module({
imports: [
ConfigModule.forRoot({ load: [exportConfiguration], validatePredefined: false, ignoreEnvFile: true, isGlobal: true }),
- LoggerModule.forRootAsync({
+ LoggerModule.forRootAsync<FastifyLoggerRequest>({
useFactory: async () => ({
pinoHttp: configLogger(configuration.logger)
})
diff --git a/backend/src/configuration/config.logger.ts b/backend/src/configuration/config.logger.ts
index 7a3886d6..0d60ad6d 100644
--- a/backend/src/configuration/config.logger.ts
+++ b/backend/src/configuration/config.logger.ts
@@ -2,6 +2,7 @@ import type { Options } from 'pino-http'
import { join } from 'node:path'
import { AVAILABILITY_ROUTE } from '../infrastructure/availability/availability.constants'
import type { LoggerConfig } from './config.validation'
+import type { FastifyLoggerRequest, LoggerErrorObject, LoggerSerializedResponse } from './interfaces/logger.interface'
const AVAILABILITY_ROUTE_PREFIX = `${AVAILABILITY_ROUTE.BASE}/`
const PINO_WARN_LEVEL = 40
@@ -81,16 +82,16 @@ export const configLogger = (loggerConfig: LoggerConfig) => {
method.apply(this, args)
}
},
- customProps: (req: any) => ({
+ customProps: (req) => ({
context: 'HTTP',
user: req.user,
userAgent: req.headers['user-agent']
}),
- customSuccessMessage: (req: any, res: any) => {
- return `${req.method} ${req.url} (${req.protocol.toUpperCase()}/${req['httpVersion']} ${res.statusCode}) ${req.ip}`
+ customSuccessMessage: (req, res) => {
+ return `${req.method} ${req.url} (${req.protocol.toUpperCase()}/${req.httpVersion} ${res.statusCode}) ${req.ip}`
},
- customErrorMessage: (req: any, res: any) => {
- return `${req.method} ${req.url} (${req.protocol.toUpperCase()}/${req['httpVersion']} ${res.statusCode}) ${req.ip}`
+ customErrorMessage: (req, res) => {
+ return `${req.method} ${req.url} (${req.protocol.toUpperCase()}/${req.httpVersion} ${res.statusCode}) ${req.ip}`
},
customLogLevel: (req, res, err) => {
// Successful health checks are frequent and provide little value in logs.
@@ -105,14 +106,21 @@ export const configLogger = (loggerConfig: LoggerConfig) => {
}
return 'info'
},
- customErrorObject: (_req, _res, _error, val) => {
- // avoid logging object error for 404 status
- return val.res.statusCode === 404 ? null : val
+ customErrorObject: (_req, _res, _error, val: LoggerErrorObject) => {
+ // Keep response metadata for 404s without logging the associated error object.
+ if (val.res.statusCode === 404) {
+ return {
+ res: val.res,
+ responseTime: val.responseTime
+ }
+ }
+
+ return val
},
serializers: {
- res(reply) {
+ res(response: LoggerSerializedResponse) {
return {
- contentLength: reply.raw['_contentLength']
+ contentLength: response.headers['content-length'] || '0'
}
},
req() {
@@ -120,5 +128,5 @@ export const configLogger = (loggerConfig: LoggerConfig) => {
}
},
transport: loggerConfig.jsonOutput ? null : createPrettyTransport(loggerConfig)
- } satisfies Options
+ } satisfies Options<FastifyLoggerRequest>
}
diff --git a/backend/src/configuration/interfaces/logger.interface.ts b/backend/src/configuration/interfaces/logger.interface.ts
new file mode 100644
index 00000000..4c50f80c
--- /dev/null
+++ b/backend/src/configuration/interfaces/logger.interface.ts
@@ -0,0 +1,17 @@
+import type { IncomingMessage, OutgoingHttpHeaders, ServerResponse } from 'node:http'
+
+export interface FastifyLoggerRequest extends IncomingMessage {
+ readonly ip: string
+ readonly protocol: 'http' | 'https'
+ user?: string
+}
+
+export interface LoggerSerializedResponse {
+ headers: OutgoingHttpHeaders
+}
+
+export interface LoggerErrorObject {
+ err: Error
+ res: ServerResponse
+ responseTime: number
+}