diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 7f97f5f210..7db4ecc4df 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -3,7 +3,7 @@ require('source-map-support').install(); import * as Koa from 'koa'; import * as fs from 'fs-extra'; -import Logger, { LoggerWrapper, TargetType } from '@joplin/utils/Logger'; +import Logger, { LogLevel, LoggerWrapper, TargetType } from '@joplin/utils/Logger'; import config, { fullVersionString, initConfig, runningInDocker } from './config'; import { migrateLatest, waitForConnection, sqliteDefaultDir, latestMigration, needsMigration, migrateList } from './db'; import { AppContext, Env, KoaNext } from './utils/types'; @@ -224,8 +224,10 @@ async function main() { const globalLogger = new Logger(); const instancePrefix = config().INSTANCE_NAME ? `${config().INSTANCE_NAME}: ` : ''; globalLogger.addTarget(TargetType.Console, { - format: `%(date_time)s: ${instancePrefix}[%(level)s] %(prefix)s: %(message)s`, - formatInfo: `%(date_time)s: ${instancePrefix}%(prefix)s: %(message)s`, + format: (level: LogLevel, _prefix: string) => { + if (level === LogLevel.Info) return `%(date_time)s: ${instancePrefix}%(prefix)s: %(message)s`; + return `%(date_time)s: ${instancePrefix}[%(level)s] %(prefix)s: %(message)s`; + }, }); Logger.initializeGlobalLogger(globalLogger); initLib(globalLogger); diff --git a/packages/utils/Logger.ts b/packages/utils/Logger.ts index 7471d42ee8..36d10df0c3 100644 --- a/packages/utils/Logger.ts +++ b/packages/utils/Logger.ts @@ -18,6 +18,8 @@ export enum LogLevel { Debug = 40, } +type FormatFunction = (level: LogLevel, targetPrefix?: string)=> string; + interface TargetOptions { level?: LogLevel; database?: any; @@ -27,10 +29,7 @@ interface TargetOptions { source?: string; // Default message format - format?: string; - - // If specified, will use this as format if it's an info message - formatInfo?: string; + format?: string | FormatFunction; } interface Target extends TargetOptions { @@ -228,7 +227,7 @@ class Logger { let items: any[] = []; if (target.format) { - const format = level === LogLevel.Info && target.formatInfo ? target.formatInfo : target.format; + const format = typeof target.format === 'string' ? target.format : target.format(level, targetPrefix); const s = sprintf(format, { date_time: moment().format('YYYY-MM-DD HH:mm:ss'),