1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

Implementing custom Logger, removing deprecated winston dependency

Winston had a major API refactor since v2, so update was not straight forward. Pigallery also uses simple logging, so no need for the complex feature set of winston.
This commit is contained in:
Patrik J. Braun 2021-05-16 11:11:53 +02:00
parent f59ba6eb98
commit 2fa1c8f87f
6 changed files with 77 additions and 46 deletions

13
package-lock.json generated
View File

@ -5837,7 +5837,8 @@
"colors": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.0.3.tgz",
"integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs="
"integrity": "sha1-BDP0TYCWgP3rYO0mDxsMJi6CpAs=",
"dev": true
},
"combined-stream": {
"version": "1.0.7",
@ -6936,7 +6937,8 @@
"cycle": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz",
"integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI="
"integrity": "sha1-IegLK+hYD5i0aPN5QwZisEbDStI=",
"dev": true
},
"cyclist": {
"version": "1.0.1",
@ -8323,7 +8325,8 @@
"eyes": {
"version": "0.1.8",
"resolved": "https://registry.npmjs.org/eyes/-/eyes-0.1.8.tgz",
"integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A="
"integrity": "sha1-Ys8SAjTGg3hdkCNIqADvPgzCC8A=",
"dev": true
},
"fancy-log": {
"version": "1.3.2",
@ -18459,7 +18462,8 @@
"stack-trace": {
"version": "0.0.10",
"resolved": "https://registry.npmjs.org/stack-trace/-/stack-trace-0.0.10.tgz",
"integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA="
"integrity": "sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA=",
"dev": true
},
"static-extend": {
"version": "0.1.2",
@ -21401,6 +21405,7 @@
"version": "2.4.5",
"resolved": "https://registry.npmjs.org/winston/-/winston-2.4.5.tgz",
"integrity": "sha512-TWoamHt5yYvsMarGlGEQE59SbJHqGsZV8/lwC+iCcGeAe0vUaOh+Lv6SYM17ouzC/a/LB1/hz/7sxFBtlu1l4A==",
"dev": true,
"requires": {
"async": "~1.0.0",
"colors": "1.0.x",

View File

@ -53,8 +53,7 @@
"ts-exif-parser": "0.2.2",
"ts-node-iptc": "1.0.11",
"typeconfig": "2.0.15",
"typeorm": "0.2.32",
"winston": "2.4.5"
"typeorm": "0.2.32"
},
"devDependencies": {
"@angular-devkit/build-angular": "0.1102.12",

View File

@ -1,42 +1,65 @@
import * as winston from 'winston';
import {Config} from '../common/config/private/Config';
import {LogLevel} from '../common/config/private/PrivateConfig';
export type logFN = (...args: (string | number)[]) => void;
const forcedDebug = process.env.NODE_ENV === 'debug';
if (forcedDebug === true) {
console.log('NODE_ENV environmental variable is set to debug, forcing all logs to print');
}
export const winstonSettings = {
transports: [
new winston.transports.Console({
level: forcedDebug === true ? LogLevel[LogLevel.silly] : LogLevel[Config.Server.Log.level],
handleExceptions: true,
json: false,
colorize: true,
timestamp(): string {
return (new Date()).toLocaleString();
},
label: 'innerLabel',
formatter: (options: any) => {
// Return string will be passed to logger.
return options.timestamp() + '[' + (winston as any).config.colorize(options.level, options.level.toUpperCase()) + '] ' +
(undefined !== options.message ? options.message : '') +
(options.meta && Object.keys(options.meta).length ? '\n\t' + JSON.stringify(options.meta) : '');
},
debugStdout: true
} as any)
],
exitOnError: false
};
export type logFN = (...args: (string | number)[]) => {};
export class Logger {
public static silly(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.silly) {
return;
}
Logger.log(`[\x1b[35mSILLY\x1b[0m]`, ...args);
}
export const Logger: {
error: logFN,
warn: logFN,
info: logFN,
verbose: logFN,
debug: logFN,
silly: logFN
} = new (winston as any).Logger(winstonSettings);
public static debug(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.debug) {
return;
}
Logger.log(`[\x1b[34mDEBUG\x1b[0m]`, ...args);
}
public static verbose(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.verbose) {
return;
}
Logger.log(`[\x1b[36mVERBS\x1b[0m]`, ...args);
}
public static info(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.info) {
return;
}
Logger.log(`[\x1b[32mINFO_\x1b[0m]`, ...args);
}
public static warn(...args: (string | number)[]): void {
if (!forcedDebug && Config.Server.Log.level < LogLevel.warn) {
return;
}
Logger.log(`[\x1b[33mWARN_\x1b[0m]`, ...args);
}
public static error(...args: (string | number)[]): void {
Logger.log(`[\x1b[31mERROR\x1b[0m]`, ...args);
}
private static log(tag: string, ...args: (string | number)[]): void {
const date = (new Date()).toLocaleString();
let LOG_TAG = '';
if (args.length > 0 && typeof args[0] === 'string' && args[0].startsWith('[') && args[0].endsWith(']')) {
LOG_TAG = args[0];
args.shift();
}
console.log(date + tag + LOG_TAG, ...args);
}
}

View File

@ -10,6 +10,8 @@ import {IVersionManager} from './database/interfaces/IVersionManager';
import {IJobManager} from './database/interfaces/IJobManager';
import {LocationManager} from './database/LocationManager';
const LOG_TAG = '[ObjectManagers]';
export class ObjectManagers {
private static instance: ObjectManagers = null;
@ -162,7 +164,7 @@ export class ObjectManagers {
ObjectManagers.getInstance().VersionManager = new VersionManager();
ObjectManagers.getInstance().LocationManager = new LocationManager();
this.InitCommonManagers();
Logger.debug('SQL DB inited');
Logger.debug(LOG_TAG, 'SQL DB inited');
}
}

View File

@ -5,7 +5,7 @@ import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
import {RendererInput} from './PhotoWorker';
import {TaskQue, TaskQueEntry} from './TaskQue';
import {ITaskExecuter} from './TaskExecuter';
import {DirectoryScanSettings, DiskMangerWorker} from './DiskMangerWorker';
import {DirectoryScanSettings} from './DiskMangerWorker';
interface WorkerWrapper<O> {
@ -13,6 +13,8 @@ interface WorkerWrapper<O> {
poolTask: TaskQueEntry<WorkerTask, O>;
}
const LOG_TAG = '[ThreadPool]';
export class ThreadPool<O> {
public static WorkerCount = 0;
@ -20,7 +22,7 @@ export class ThreadPool<O> {
private taskQue = new TaskQue<WorkerTask, O>();
constructor(private size: number) {
Logger.silly('Creating thread pool with', size, 'workers');
Logger.silly(LOG_TAG, 'Creating thread pool with', size, 'workers');
for (let i = 0; i < size; i++) {
this.startWorker();
}
@ -60,13 +62,13 @@ export class ThreadPool<O> {
this.workers.push(worker);
worker.worker.on('online', (): void => {
ThreadPool.WorkerCount++;
Logger.debug('Worker ' + worker.worker.process.pid + ' is online, worker count:', ThreadPool.WorkerCount);
Logger.debug(LOG_TAG, 'Worker ' + worker.worker.process.pid + ' is online, worker count:', ThreadPool.WorkerCount);
});
worker.worker.on('exit', (code, signal): void => {
ThreadPool.WorkerCount--;
Logger.warn('Worker ' + worker.worker.process.pid + ' died with code: ' + code +
Logger.warn(LOG_TAG, 'Worker ' + worker.worker.process.pid + ' died with code: ' + code +
', and signal: ' + signal + ', worker count:', ThreadPool.WorkerCount);
Logger.debug('Starting a new worker');
Logger.debug(LOG_TAG, 'Starting a new worker');
this.startWorker();
});

View File

@ -7,10 +7,10 @@ import {DirectoryDTO} from '../../../common/entities/DirectoryDTO';
declare var process: NodeJS.Process;
declare var global: NodeJS.Global;
const LOG_TAG = '[Worker]';
export class Worker {
public static process<O extends (void | DirectoryDTO<MediaDTO>)>(): void {
Logger.debug('Worker is waiting for tasks');
Logger.debug(LOG_TAG, 'Worker is waiting for tasks');
process.on('message', async (task: WorkerTask) => {
try {
let result = null;