From e2ce3f32c3e1029c01644ba717ad92185b80c52d Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 29 Apr 2025 23:25:38 +0530 Subject: [PATCH 1/3] fix logging default config --- packages/conf/DittoConf.ts | 14 ++++++++++++-- packages/ditto/utils/logi.ts | 2 ++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/conf/DittoConf.ts b/packages/conf/DittoConf.ts index 1420bdf5..d6559cca 100644 --- a/packages/conf/DittoConf.ts +++ b/packages/conf/DittoConf.ts @@ -11,6 +11,8 @@ import { getEcdsaPublicKey } from './utils/crypto.ts'; import { optionalBooleanSchema, optionalNumberSchema } from './utils/schema.ts'; import { mergeURLPath } from './utils/url.ts'; +import { VALID_LOG_TYPES } from '../ditto/utils/logi.ts'; + /** Ditto application-wide configuration. */ export class DittoConf { constructor(private env: { get(key: string): string | undefined }) { @@ -250,9 +252,17 @@ export class DittoConf { level: string; scopes: string[]; } { - const [fmt = 'jsonl', level = 'debug', scopes = ''] = (this.env.get('LOG_CONFIG') || '').split(':').filter(Boolean); + let [fmt, level, scopes] = (this.env.get('LOG_CONFIG') || '').split(':'); + fmt ||= 'jsonl'; + level ||= 'debug'; + scopes ||= ''; + + if (fmt && !VALID_LOG_TYPES.includes(fmt)) { + throw new Error(`Invalid log type supplied: Valid types are [${VALID_LOG_TYPES.join('|')}].`); + } + return { - fmt: fmt === 'jsonl' ? fmt : 'pretty', + fmt: (fmt ?? 'jsonl') as 'jsonl' | 'pretty', level, scopes: scopes.split(',').filter(Boolean), }; diff --git a/packages/ditto/utils/logi.ts b/packages/ditto/utils/logi.ts index 4b764201..0d071254 100644 --- a/packages/ditto/utils/logi.ts +++ b/packages/ditto/utils/logi.ts @@ -55,6 +55,8 @@ const pair = (key: string, value: LogiValue | undefined) => { return `${key}: ${prettyPrint(value || '')}`; }; +export const VALID_LOG_TYPES = ['jsonl', 'pretty']; + export const createLogiHandler = (conf: DittoConf, defaultHandler: LogiHandler) => (log: LogiLog) => { const { fmt, level, scopes } = conf.logConfig; if (fmt === 'jsonl') return defaultHandler(log); From 319620a414bb41b81e8276eb7a35a38688e1e76d Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 29 Apr 2025 23:32:37 +0530 Subject: [PATCH 2/3] remove relative import from ditto/utils in DittoConf --- packages/conf/DittoConf.ts | 8 ++------ packages/ditto/utils/logi.ts | 2 -- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/packages/conf/DittoConf.ts b/packages/conf/DittoConf.ts index d6559cca..05e7d19f 100644 --- a/packages/conf/DittoConf.ts +++ b/packages/conf/DittoConf.ts @@ -11,8 +11,6 @@ import { getEcdsaPublicKey } from './utils/crypto.ts'; import { optionalBooleanSchema, optionalNumberSchema } from './utils/schema.ts'; import { mergeURLPath } from './utils/url.ts'; -import { VALID_LOG_TYPES } from '../ditto/utils/logi.ts'; - /** Ditto application-wide configuration. */ export class DittoConf { constructor(private env: { get(key: string): string | undefined }) { @@ -257,12 +255,10 @@ export class DittoConf { level ||= 'debug'; scopes ||= ''; - if (fmt && !VALID_LOG_TYPES.includes(fmt)) { - throw new Error(`Invalid log type supplied: Valid types are [${VALID_LOG_TYPES.join('|')}].`); - } + if (fmt !== 'jsonl' && fmt !== 'pretty') fmt = 'jsonl'; return { - fmt: (fmt ?? 'jsonl') as 'jsonl' | 'pretty', + fmt: fmt as 'jsonl' | 'pretty', level, scopes: scopes.split(',').filter(Boolean), }; diff --git a/packages/ditto/utils/logi.ts b/packages/ditto/utils/logi.ts index 0d071254..4b764201 100644 --- a/packages/ditto/utils/logi.ts +++ b/packages/ditto/utils/logi.ts @@ -55,8 +55,6 @@ const pair = (key: string, value: LogiValue | undefined) => { return `${key}: ${prettyPrint(value || '')}`; }; -export const VALID_LOG_TYPES = ['jsonl', 'pretty']; - export const createLogiHandler = (conf: DittoConf, defaultHandler: LogiHandler) => (log: LogiLog) => { const { fmt, level, scopes } = conf.logConfig; if (fmt === 'jsonl') return defaultHandler(log); From b75cc0e3724fae92f8011a169e000e832cd23a8a Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Tue, 29 Apr 2025 23:34:46 +0530 Subject: [PATCH 3/3] fix early return in jsonl path for log level --- packages/ditto/utils/logi.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/ditto/utils/logi.ts b/packages/ditto/utils/logi.ts index 4b764201..67217c7f 100644 --- a/packages/ditto/utils/logi.ts +++ b/packages/ditto/utils/logi.ts @@ -57,10 +57,11 @@ const pair = (key: string, value: LogiValue | undefined) => { export const createLogiHandler = (conf: DittoConf, defaultHandler: LogiHandler) => (log: LogiLog) => { const { fmt, level, scopes } = conf.logConfig; - if (fmt === 'jsonl') return defaultHandler(log); if (!isLevel(level)) throw new Error(`Invalid log level ${level} specified`); if (!lowerLevels[level].includes(log.level)) return; if (scopes.length && !scopes.some((scope) => scope.startsWith(log.ns))) return; + if (fmt === 'jsonl') return defaultHandler(log); + const message = prettyPrint(log.message || log.msg || ''); const remaining = Object.entries(log) .filter(([key]) => !['ns', 'level', 'message', 'msg'].includes(key));