fix logging default config

This commit is contained in:
Siddharth Singh 2025-04-29 23:25:38 +05:30
parent 49e509e420
commit e2ce3f32c3
No known key found for this signature in database
2 changed files with 14 additions and 2 deletions

View file

@ -11,6 +11,8 @@ import { getEcdsaPublicKey } from './utils/crypto.ts';
import { optionalBooleanSchema, optionalNumberSchema } from './utils/schema.ts'; import { optionalBooleanSchema, optionalNumberSchema } from './utils/schema.ts';
import { mergeURLPath } from './utils/url.ts'; import { mergeURLPath } from './utils/url.ts';
import { VALID_LOG_TYPES } from '../ditto/utils/logi.ts';
/** Ditto application-wide configuration. */ /** Ditto application-wide configuration. */
export class DittoConf { export class DittoConf {
constructor(private env: { get(key: string): string | undefined }) { constructor(private env: { get(key: string): string | undefined }) {
@ -250,9 +252,17 @@ export class DittoConf {
level: string; level: string;
scopes: 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 { return {
fmt: fmt === 'jsonl' ? fmt : 'pretty', fmt: (fmt ?? 'jsonl') as 'jsonl' | 'pretty',
level, level,
scopes: scopes.split(',').filter(Boolean), scopes: scopes.split(',').filter(Boolean),
}; };

View file

@ -55,6 +55,8 @@ const pair = (key: string, value: LogiValue | undefined) => {
return `${key}: ${prettyPrint(value || '')}`; return `${key}: ${prettyPrint(value || '')}`;
}; };
export const VALID_LOG_TYPES = ['jsonl', 'pretty'];
export const createLogiHandler = (conf: DittoConf, defaultHandler: LogiHandler) => (log: LogiLog) => { export const createLogiHandler = (conf: DittoConf, defaultHandler: LogiHandler) => (log: LogiLog) => {
const { fmt, level, scopes } = conf.logConfig; const { fmt, level, scopes } = conf.logConfig;
if (fmt === 'jsonl') return defaultHandler(log); if (fmt === 'jsonl') return defaultHandler(log);