dependencies, first working version
This commit is contained in:
@ -1,3 +1 @@
|
||||
import * as plugins from './logcontext.plugins'
|
||||
|
||||
export let standardExport = 'Hi there! :) This is a exported string'
|
||||
export * from './logcontext.classes.logger';
|
||||
|
124
ts/logcontext.classes.logger.ts
Normal file
124
ts/logcontext.classes.logger.ts
Normal file
@ -0,0 +1,124 @@
|
||||
import * as plugins from './logcontext.plugins';
|
||||
import { LogMap } from './logcontext.classes.logmap';
|
||||
|
||||
export class Logger {
|
||||
namespaceString: string;
|
||||
clsNameSpace: plugins.smartcls.Namespace;
|
||||
logmap: LogMap;
|
||||
thirdPartyLogger: any;
|
||||
child: any;
|
||||
settings = {
|
||||
enableScope: () => {
|
||||
this.settingsParams.scope = true;
|
||||
},
|
||||
disableScope: () => {
|
||||
this.settingsParams.scope = false;
|
||||
},
|
||||
enableAddData: () => {
|
||||
this.settingsParams.addData = true;
|
||||
},
|
||||
disableAddData: () => {
|
||||
this.settingsParams.addData = false;
|
||||
}
|
||||
};
|
||||
private settingsParams: { scope: boolean; addData: boolean } = {
|
||||
scope: true,
|
||||
addData: true
|
||||
};
|
||||
|
||||
constructor(namespaceArg: string = plugins.shortid()) {
|
||||
this.namespaceString = namespaceArg;
|
||||
this.clsNameSpace = plugins.smartcls.createNamespace(this.namespaceString);
|
||||
this.logmap = new LogMap(this.clsNameSpace);
|
||||
}
|
||||
|
||||
addData(paramNameArg: string, dataArg: any) {
|
||||
if (this.settingsParams.addData) {
|
||||
this.logmap.addData(paramNameArg, dataArg);
|
||||
}
|
||||
}
|
||||
|
||||
addThirdPartyLogger(thirdPartyLoggerArg) {
|
||||
this.thirdPartyLogger = thirdPartyLoggerArg;
|
||||
}
|
||||
|
||||
/**
|
||||
* debug
|
||||
* @param logMessageArg
|
||||
*/
|
||||
debug(logMessageArg) {
|
||||
this.routeLog('debug', logMessageArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* log
|
||||
* @param logMessageArg
|
||||
*/
|
||||
log(logMessageArg) {
|
||||
this.routeLog('log', logMessageArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* info
|
||||
* @param logObjectArg
|
||||
*/
|
||||
info(logObjectArg) {
|
||||
this.routeLog('info', logObjectArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* error
|
||||
* @param logMessageArg
|
||||
* @param args
|
||||
*/
|
||||
error(logMessageArg, ...args) {
|
||||
this.routeLog('error', logMessageArg, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* warn
|
||||
* @param logMessageArg
|
||||
* @param args
|
||||
*/
|
||||
warn(logMessageArg, ...args) {
|
||||
this.routeLog('warn', logMessageArg, ...args);
|
||||
}
|
||||
|
||||
/**
|
||||
* fatal
|
||||
* @param logMessageArg
|
||||
* @param args
|
||||
*/
|
||||
fatal(logMessageArg, ...args) {
|
||||
this.routeLog('fatal', logMessageArg, ...args);
|
||||
}
|
||||
|
||||
// creates a new async scope
|
||||
scope(funcArg: any) {
|
||||
// create node continuation scope
|
||||
if (this.settingsParams.scope) {
|
||||
this.clsNameSpace.run(funcArg);
|
||||
} else {
|
||||
funcArg();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* routes the log according to whats available in the environment
|
||||
* @param {string} logMethod
|
||||
* @param {any} message
|
||||
* @param {any[]} ...args
|
||||
*/
|
||||
private routeLog(logMethod, message, ...args) {
|
||||
let logObject = {
|
||||
message: message,
|
||||
type: logMethod,
|
||||
logContext: this.logmap.getAllData()
|
||||
};
|
||||
if (this.thirdPartyLogger && this.thirdPartyLogger[logMethod]) {
|
||||
this.thirdPartyLogger[logMethod](logObject, ...args);
|
||||
} else {
|
||||
console.log(logObject);
|
||||
}
|
||||
}
|
||||
}
|
34
ts/logcontext.classes.logmap.ts
Normal file
34
ts/logcontext.classes.logmap.ts
Normal file
@ -0,0 +1,34 @@
|
||||
import * as plugins from './logcontext.plugins';
|
||||
|
||||
import { Namespace } from 'smartcls';
|
||||
import { Stringmap } from 'lik';
|
||||
|
||||
export class LogMap {
|
||||
clsNamespace: Namespace;
|
||||
paramMap = new plugins.lik.Stringmap();
|
||||
|
||||
constructor(clsNamespaceArg: Namespace) {
|
||||
this.clsNamespace = clsNamespaceArg;
|
||||
}
|
||||
|
||||
addData(paramName: string, logData) {
|
||||
this.paramMap.addString(paramName);
|
||||
this.clsNamespace.set(paramName, logData);
|
||||
}
|
||||
|
||||
deleteData(paramName: string) {
|
||||
this.clsNamespace.set(paramName, null);
|
||||
}
|
||||
|
||||
getData(paramName: string) {
|
||||
return this.clsNamespace.get(paramName);
|
||||
}
|
||||
|
||||
getAllData() {
|
||||
let returnObject = {};
|
||||
for (let stringArg of this.paramMap.getStringArray()) {
|
||||
returnObject[stringArg] = this.clsNamespace.get(stringArg);
|
||||
}
|
||||
return returnObject;
|
||||
}
|
||||
}
|
@ -1 +1,7 @@
|
||||
import 'typings-global'
|
||||
import 'typings-global';
|
||||
|
||||
import * as lik from 'lik';
|
||||
import * as smartcls from 'smartcls';
|
||||
import * as shortid from 'shortid';
|
||||
|
||||
export { lik, smartcls, shortid };
|
||||
|
Reference in New Issue
Block a user