1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Chore: All: Change Logger to accept formatter function (#8702)

This commit is contained in:
pedr 2023-08-28 10:30:56 -03:00 committed by GitHub
parent 9430dccb61
commit ddc74af3d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 8 deletions

View File

@ -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);

View File

@ -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'),