Skip to content

Commit

Permalink
add logger transports configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
vunb committed Aug 1, 2019
1 parent b7eb65d commit 1175ed7
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions packages/core/engine/kites-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as fs from 'fs';
import * as _ from 'lodash';
// import * as nconf from 'nconf';
import * as path from 'path';
import { Logger } from 'winston';
import { Logger, transports } from 'winston';
import Transport from 'winston-transport';

import { EventEmitter } from 'events';
import { ExtensionsManager } from '../extensions/extensions-manager';
Expand Down Expand Up @@ -279,7 +280,7 @@ export class KitesInstance extends EventEmitter implements IKites {
this.fnAfterConfigLoaded(this);
}

// return this._configureWinstonTransports(this.options.logger);
return this._configureWinstonTransports(this.options.logger);
}

private _silentLogs(logger: Logger) {
Expand Down Expand Up @@ -328,4 +329,76 @@ export class KitesInstance extends EventEmitter implements IKites {
this.options = nconf.get();
}

private _configureWinstonTransports(options: any) {
options = options || {};

var knownTransports: any = {
console: transports.Console,
file: transports.File,
http: transports.Http,
stream: transports.Stream,
};

var knownOptions = ['transport', 'module', 'enabled'];

// tslint:disable-next-line:forin
for (let trName in options) {
var tranOpts = options[trName];
if (!tranOpts || typeof tranOpts !== 'object' || _.isArray(tranOpts)) {
continue;
}

if (!tranOpts.transport || typeof tranOpts.transport !== 'string') {
throw new Error(`invalid option for transport object ${trName}, option "transport" is not specified or has an incorrect value, must be a string with a valid value. check your "logger" config`);
}

if (!tranOpts.level || typeof tranOpts.level !== 'string') {
throw new Error(`invalid option for transport object ${trName}, option "level" is not specified or has an incorrect value, must be a string with a valid value. check your "logger" config`);
}

if (tranOpts.enabled === false) {
continue;
}

// add transport
if (knownTransports[tranOpts.transport]) {
const transport = knownTransports[tranOpts.transport] as any;
const opts = _.extend(_.omit(tranOpts, knownOptions), { name: trName });
this.logger.add(new transport(opts));
} else {
if (typeof tranOpts.module !== 'string') {
throw new Error(`invalid option for transport object "${trName}", option "module" has an incorrect value, must be a string with a module name. check your "logger" config`);
}

try {
let transportModule = require(tranOpts.module);
let winstonTransport: any = transports;
if (typeof winstonTransport[tranOpts.transport] === 'function') {
transportModule = winstonTransport[tranOpts.transport];
} else if (typeof transportModule[tranOpts.transport] === 'function') {
transportModule = transportModule[tranOpts.transport];
}

if (typeof transportModule !== 'function') {
throw new Error(`invalid option for transport object "${trName}", option module "${tranOpts.module}" does not export a valid transport. check your "logger" config`);
}

const opts = _.extend(_.omit(tranOpts, knownOptions), { name: trName });

this.logger.add(new transportModule(opts));

} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
throw new Error(
`invalid option for transport object "${trName}", module "${tranOpts.module}" in "module" option could not be found. are you sure that you have installed it?. check your "logger" config`);
}

throw err;
}
}

}

}

}

0 comments on commit 1175ed7

Please sign in to comment.