Skip to content

Commit

Permalink
Merge pull request #27 from kitesjs/dev-logger-debug-tranport
Browse files Browse the repository at this point in the history
add api createDebugLogger
  • Loading branch information
vunb authored Sep 14, 2019
2 parents 66c6ab1 + 2670ef1 commit e5f6b9f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/core/engine/kites-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

}
Expand Down
6 changes: 3 additions & 3 deletions packages/core/extensions/discover.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
7 changes: 5 additions & 2 deletions packages/core/extensions/fs.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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[];
Expand Down
7 changes: 4 additions & 3 deletions packages/core/extensions/location-cache.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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);
});
});
4 changes: 2 additions & 2 deletions packages/core/logger/debug-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
30 changes: 28 additions & 2 deletions packages/core/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? [] : [
Expand Down Expand Up @@ -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
};

0 comments on commit e5f6b9f

Please sign in to comment.