You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-07-03 23:50:33 +02:00
Chore: All: Change Logger to accept formatter function (#8702)
This commit is contained in:
@ -3,7 +3,7 @@ require('source-map-support').install();
|
|||||||
|
|
||||||
import * as Koa from 'koa';
|
import * as Koa from 'koa';
|
||||||
import * as fs from 'fs-extra';
|
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 config, { fullVersionString, initConfig, runningInDocker } from './config';
|
||||||
import { migrateLatest, waitForConnection, sqliteDefaultDir, latestMigration, needsMigration, migrateList } from './db';
|
import { migrateLatest, waitForConnection, sqliteDefaultDir, latestMigration, needsMigration, migrateList } from './db';
|
||||||
import { AppContext, Env, KoaNext } from './utils/types';
|
import { AppContext, Env, KoaNext } from './utils/types';
|
||||||
@ -224,8 +224,10 @@ async function main() {
|
|||||||
const globalLogger = new Logger();
|
const globalLogger = new Logger();
|
||||||
const instancePrefix = config().INSTANCE_NAME ? `${config().INSTANCE_NAME}: ` : '';
|
const instancePrefix = config().INSTANCE_NAME ? `${config().INSTANCE_NAME}: ` : '';
|
||||||
globalLogger.addTarget(TargetType.Console, {
|
globalLogger.addTarget(TargetType.Console, {
|
||||||
format: `%(date_time)s: ${instancePrefix}[%(level)s] %(prefix)s: %(message)s`,
|
format: (level: LogLevel, _prefix: string) => {
|
||||||
formatInfo: `%(date_time)s: ${instancePrefix}%(prefix)s: %(message)s`,
|
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);
|
Logger.initializeGlobalLogger(globalLogger);
|
||||||
initLib(globalLogger);
|
initLib(globalLogger);
|
||||||
|
@ -18,6 +18,8 @@ export enum LogLevel {
|
|||||||
Debug = 40,
|
Debug = 40,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FormatFunction = (level: LogLevel, targetPrefix?: string)=> string;
|
||||||
|
|
||||||
interface TargetOptions {
|
interface TargetOptions {
|
||||||
level?: LogLevel;
|
level?: LogLevel;
|
||||||
database?: any;
|
database?: any;
|
||||||
@ -27,10 +29,7 @@ interface TargetOptions {
|
|||||||
source?: string;
|
source?: string;
|
||||||
|
|
||||||
// Default message format
|
// Default message format
|
||||||
format?: string;
|
format?: string | FormatFunction;
|
||||||
|
|
||||||
// If specified, will use this as format if it's an info message
|
|
||||||
formatInfo?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Target extends TargetOptions {
|
interface Target extends TargetOptions {
|
||||||
@ -228,7 +227,7 @@ class Logger {
|
|||||||
let items: any[] = [];
|
let items: any[] = [];
|
||||||
|
|
||||||
if (target.format) {
|
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, {
|
const s = sprintf(format, {
|
||||||
date_time: moment().format('YYYY-MM-DD HH:mm:ss'),
|
date_time: moment().format('YYYY-MM-DD HH:mm:ss'),
|
||||||
|
Reference in New Issue
Block a user