Skip to content

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

fix(backend:logger): restore cyan prefixes with pino-pretty 13.2

johaven19cd35bf9d5c
diff --git a/backend/nest-cli.json b/backend/nest-cli.json
index bca55a39..8d295978 100755
--- a/backend/nest-cli.json
+++ b/backend/nest-cli.json
@@ -18,6 +18,9 @@
},
{
"include": "./common/fonts"
+ },
+ {
+ "include": "./configuration/config.logger.transport.cjs"
}
]
}
diff --git a/backend/package.json b/backend/package.json
index 6dc3f914..98f895c8 100755
--- a/backend/package.json
+++ b/backend/package.json
@@ -76,7 +76,8 @@
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
- "pino-pretty": "^13.1.3",
+ "pino-abstract-transport": "^3.0.0",
+ "pino-pretty": "^13.2.0",
"qrcode-generator": "^2.0.4",
"redis": "^5.12.1",
"safe-regex2": "^5.1.1",
diff --git a/backend/src/configuration/config.logger.transport.cjs b/backend/src/configuration/config.logger.transport.cjs
new file mode 100644
index 00000000..03292a47
--- /dev/null
+++ b/backend/src/configuration/config.logger.transport.cjs
@@ -0,0 +1,48 @@
+'use strict'
+
+const build = require('pino-abstract-transport')
+const { Transform, pipeline } = require('node:stream')
+const { prettyFactory } = require('pino-pretty')
+
+const DEFAULT_ERROR_LIKE_OBJECT_KEYS = ['err', 'error']
+const WHITE = '\u001B[37m'
+
+module.exports = function buildPinoPrettyPrefixTransport(options) {
+ const { messageColorMarker, ...prettyOptions } = options
+
+ if (typeof messageColorMarker !== 'string' || messageColorMarker.length === 0) {
+ throw new TypeError('messageColorMarker must be a non-empty string')
+ }
+
+ return build(
+ (source) => {
+ const runtimeOptions = {
+ ...prettyOptions,
+ messageKey: source.messageKey,
+ errorLikeObjectKeys: Array.from(
+ new Set([...(prettyOptions.errorLikeObjectKeys || DEFAULT_ERROR_LIKE_OBJECT_KEYS), source.errorKey])
+ ),
+ customLevels: source.levels.values
+ }
+ const prettify = prettyFactory(runtimeOptions)
+ const messageColor = runtimeOptions.colorize ? WHITE : ''
+ const transform = new Transform({
+ objectMode: true,
+ autoDestroy: true,
+ transform(log, _encoding, callback) {
+ try {
+ const line = prettify(log)
+ callback(null, line?.replace(messageColorMarker, messageColor))
+ } catch (error) {
+ callback(error)
+ }
+ }
+ })
+
+ // Pino Pretty sanitizes the formatted line before this trusted color code is inserted.
+ pipeline(source, transform, () => {})
+ return transform
+ },
+ { enablePipelining: true, expectPinoConfig: true, metadata: false }
+ )
+}
diff --git a/backend/src/configuration/config.logger.ts b/backend/src/configuration/config.logger.ts
index aaf67b97..7a3886d6 100644
--- a/backend/src/configuration/config.logger.ts
+++ b/backend/src/configuration/config.logger.ts
@@ -1,4 +1,5 @@
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'
@@ -6,6 +7,53 @@ const AVAILABILITY_ROUTE_PREFIX = `${AVAILABILITY_ROUTE.BASE}/`
const PINO_WARN_LEVEL = 40
const NEST_APPLICATION_CONTEXT = 'NestApplication'
const MUTED_NEST_STARTUP_CONTEXTS = new Set(['NestFactory', 'InstanceLoader', 'RoutesResolver', 'RouterExplorer', NEST_APPLICATION_CONTEXT])
+const MESSAGE_COLOR_MARKER = '\uE000SYNC_IN_WHITE\uE001'
+const MESSAGE_FORMAT =
+ '[{context}]{if tag} [{tag}] {end}{if user} <{user}> {end} {msg}{if res} ({res.contentLength} bytes in {responseTime}ms) {userAgent}{end}{if reqId} | {reqId}{end}'
+const COLORIZED_MESSAGE_FORMAT = MESSAGE_FORMAT.replace('{msg}', `${MESSAGE_COLOR_MARKER}{msg}`)
+
+const createPrettyTransport = (loggerConfig: LoggerConfig) => {
+ const prettyOptions = {
+ ignore: 'hostname,context,reqId,req,res,user,userAgent,responseTime,tag',
+ hideObject: false,
+ singleLine: false,
+ colorize: loggerConfig.colorize,
+ colorizeObjects: false,
+ translateTime: 'SYS:yyyy-mm-dd HH:MM:ss',
+ messageFormat: loggerConfig.colorize ? COLORIZED_MESSAGE_FORMAT : MESSAGE_FORMAT
+ }
+ const destinationOptions = {
+ destination: loggerConfig.stdout ? 1 : loggerConfig.filePath,
+ mkdir: true,
+ sync: false
+ }
+
+ if (!loggerConfig.colorize) {
+ return {
+ target: 'pino-pretty',
+ options: {
+ ...prettyOptions,
+ ...destinationOptions
+ }
+ }
+ }
+
+ return {
+ pipeline: [
+ {
+ target: join(__dirname, 'config.logger.transport.cjs'),
+ options: {
+ messageColorMarker: MESSAGE_COLOR_MARKER,
+ ...prettyOptions
+ }
+ },
+ {
+ target: 'pino/file',
+ options: destinationOptions
+ }
+ ]
+ }
+}
export const configLogger = (loggerConfig: LoggerConfig) => {
let isNestBootstrapping = true
@@ -71,24 +119,6 @@ export const configLogger = (loggerConfig: LoggerConfig) => {
return undefined
}
},
- transport: loggerConfig.jsonOutput
- ? null
- : {
- target: 'pino-pretty',
- options: {
- ignore: 'hostname,context,reqId,req,res,user,userAgent,responseTime,tag',
- hideObject: false,
- singleLine: false,
- colorize: loggerConfig.colorize,
- colorizeObjects: false,
- translateTime: 'SYS:yyyy-mm-dd HH:MM:ss',
- messageFormat: `[{context}]{if tag} [{tag}] {end}{if user} <{user}> {end} ${
- loggerConfig.colorize ? '\x1b[37m' : ''
- }{msg}{if res} ({res.contentLength} bytes in {responseTime}ms) {userAgent}{end}{if reqId} | {reqId}{end}`,
- destination: loggerConfig.stdout ? 1 : loggerConfig.filePath,
- mkdir: true,
- sync: false
- }
- }
+ transport: loggerConfig.jsonOutput ? null : createPrettyTransport(loggerConfig)
} satisfies Options
}