diff --git a/packages/core/engine/kites-instance.ts b/packages/core/engine/kites-instance.ts index d8c5877..1094767 100644 --- a/packages/core/engine/kites-instance.ts +++ b/packages/core/engine/kites-instance.ts @@ -7,7 +7,7 @@ import { Logger, transports } from 'winston'; import { EventEmitter } from 'events'; import { DiscoverOptions, ExtensionsManager } from '../extensions/extensions-manager'; -import { createLogger } from '../logger'; +import { getLogger } from '../logger'; import { EventCollectionEmitter } from './event-collection'; import { Type } from '@kites/common'; @@ -88,7 +88,7 @@ export class KitesInstance extends EventEmitter implements IKites { this.iocContainer = new Container(); // properties - this.logger = createLogger(this.name, this.options.logger); + this.logger = getLogger(this.name, this.options.logger); this.fnAfterConfigLoaded = () => this; } diff --git a/packages/core/extensions/discover.spec.ts b/packages/core/extensions/discover.spec.ts index 50d247f..360cd11 100644 --- a/packages/core/extensions/discover.spec.ts +++ b/packages/core/extensions/discover.spec.ts @@ -1,17 +1,17 @@ import { expect } from 'chai'; import { join } from 'path'; -import { createLogger } from '../logger'; +import { getDebugLogger } from '../logger'; import { discover } from './discover'; describe('Discover extensions', () => { it('should load an extension', async () => { const location = join(__dirname, '../test'); - const logger = createLogger('discover'); + const logger = getDebugLogger('kites:discover'); let extensions: any = await discover({ logger, rootDirectory: [location] }); - logger.info('Discovery location: ' + location); + logger.info('Discovery location: ' + location); expect(extensions.length).eq(1); }); }); diff --git a/packages/core/extensions/fs.ts b/packages/core/extensions/fs.ts index aed00e2..24fa7fd 100644 --- a/packages/core/extensions/fs.ts +++ b/packages/core/extensions/fs.ts @@ -1,5 +1,8 @@ import * as fs from 'fs'; import * as path from 'path'; +import { getDebugLogger } from '../logger'; + +const logger = getDebugLogger('kites:discover:fs', { level: 'debug' }); /** * Delete file and folder @@ -85,10 +88,10 @@ export function walkSync(rootPath: string, fileName: string, exclude?: string | * @param exclude pattern to exclude searching */ export function walkSyncLevel(dirname: string[], filename: string, depth: number = 2, exclude?: string) { - // console.log('Start searching: ', dirname); + logger.info('Start searching: ' + dirname); function readFiles(candidate: string, level: number): string[] { - // console.log('Find in: ', candidate); + logger.debug('Find in: ' + candidate); let results: string[] = []; let list: string[]; diff --git a/packages/core/extensions/location-cache.spec.ts b/packages/core/extensions/location-cache.spec.ts index 402f28b..97de694 100644 --- a/packages/core/extensions/location-cache.spec.ts +++ b/packages/core/extensions/location-cache.spec.ts @@ -1,7 +1,7 @@ import { expect } from 'chai'; import { join } from 'path'; -import { createLogger } from '../logger'; +import { getDebugLogger } from '../logger'; import * as cache from './location-cache'; describe('Location cache', () => { @@ -12,11 +12,12 @@ describe('Location cache', () => { it('should get one and save it!', async () => { const rootDirectory = join(__dirname, '../test'); + const logger = getDebugLogger('location-cache'); let extensions: any = await cache.get({ - logger: createLogger('location-cache'), + logger, rootDirectory: [rootDirectory] }); - console.log('Found: ', extensions, rootDirectory); + logger.info('Found: ', extensions, rootDirectory); expect(extensions.length).eq(1); }); }); diff --git a/packages/core/logger/debug-transport.ts b/packages/core/logger/debug-transport.ts index 392bab5..d3ad0d2 100644 --- a/packages/core/logger/debug-transport.ts +++ b/packages/core/logger/debug-transport.ts @@ -8,12 +8,12 @@ export class DebugTransport extends Transport { constructor(options?: Transport.TransportStreamOptions, name?: string) { super(options); - this.debugger = debug('kites'); + this.debugger = debug(name || 'kites'); this.name = name || 'debug'; } public log(info, callback: Function) { - this.debugger(info); + this.debugger(`${info.level} ${info.message}`); callback(null, true); } } diff --git a/packages/core/logger/index.ts b/packages/core/logger/index.ts index d162439..a035f45 100644 --- a/packages/core/logger/index.ts +++ b/packages/core/logger/index.ts @@ -2,7 +2,13 @@ import * as path from 'path'; import { format, Logger, loggers } from 'winston'; import { DebugTransport } from './debug-transport'; -function createLogger(name: string, options?: any): Logger { +/** + * Get or create a logger with empty transport + * @param name + * @param options + */ +export function getLogger(name: string, options?: any): Logger { + // TODO: Refactor options for logger (not for transport) if (!loggers.has(name)) { // add default Debug transport? const defaultTransports = Object.keys(options || {}).length > 0 ? [] : [ @@ -49,7 +55,27 @@ function createLogger(name: string, options?: any): Logger { return loggers.get(name); } +/** + * Get or create logger with default `debug` transport + */ +export function getDebugLogger(name: string, options?: any) { + if (!loggers.has(name)) { + loggers.add(name, { + exitOnError: false, + level: 'info', + format: format.combine( + format.splat(), // formats level.message based on Node's util.format(). + format.label({ label: name }), + format.colorize(), + format.timestamp(), + format.printf(({ level, message, label, timestamp }) => `${timestamp} [${label}] ${level}: ${message}`) + ), + transports: [new DebugTransport(options, name)], + }); + } + return loggers.get(name); +} + export { - createLogger, DebugTransport };