diff --git a/package-lock.json b/package-lock.json index a10857dc..9bf48a46 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 3c7a37c1..f88e71ea 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/backend/Logger.ts b/src/backend/Logger.ts index c0633aee..2fa4a0e4 100644 --- a/src/backend/Logger.ts +++ b/src/backend/Logger.ts @@ -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); + } +} diff --git a/src/backend/model/ObjectManagers.ts b/src/backend/model/ObjectManagers.ts index 01408465..c22335da 100644 --- a/src/backend/model/ObjectManagers.ts +++ b/src/backend/model/ObjectManagers.ts @@ -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'); } } diff --git a/src/backend/model/threading/ThreadPool.ts b/src/backend/model/threading/ThreadPool.ts index f3e49867..2dbf2905 100644 --- a/src/backend/model/threading/ThreadPool.ts +++ b/src/backend/model/threading/ThreadPool.ts @@ -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 { @@ -13,6 +13,8 @@ interface WorkerWrapper { poolTask: TaskQueEntry; } +const LOG_TAG = '[ThreadPool]'; + export class ThreadPool { public static WorkerCount = 0; @@ -20,7 +22,7 @@ export class ThreadPool { private taskQue = new TaskQue(); 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 { 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(); }); diff --git a/src/backend/model/threading/Worker.ts b/src/backend/model/threading/Worker.ts index 1e879a5c..7391971f 100644 --- a/src/backend/model/threading/Worker.ts +++ b/src/backend/model/threading/Worker.ts @@ -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)>(): 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;