smartlog/ts/smartlog.classes.smartlog.ts

85 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2018-06-05 18:48:14 +00:00
import * as plugins from './smartlog.plugins';
2018-01-28 03:31:06 +00:00
2018-03-03 12:57:55 +00:00
// interfaces
2018-11-04 17:21:18 +00:00
import { TLogType, TEnvironment, ILogContext, TLogLevel, TRuntime, ILogDestination, ILogPackage } from '@pushrocks/smartlog-interfaces';
2018-01-28 03:31:06 +00:00
2018-10-30 17:56:26 +00:00
import { LogRouter } from './smartlog.classes.logrouter';
2018-10-31 17:51:54 +00:00
export interface ISmartlogContructorOptions {
logContext: ILogContext;
minimumLogLevel?: TLogLevel;
}
2018-01-28 03:31:06 +00:00
export class Smartlog {
2018-03-03 12:57:55 +00:00
private logContext: ILogContext;
2018-10-31 17:51:54 +00:00
private minimumLogLevel: TLogLevel;
2018-06-05 18:48:14 +00:00
private consoleEnabled: boolean;
2018-10-31 17:51:54 +00:00
2018-11-04 13:19:31 +00:00
private logRouter = new LogRouter();
2018-10-30 17:56:26 +00:00
2018-11-04 14:26:40 +00:00
public addLogDestination (logDestinationArg: ILogDestination) {
this.logRouter.addLogDestination(logDestinationArg);
}
2018-10-30 17:56:26 +00:00
2018-10-31 17:51:54 +00:00
constructor(optionsArg: ISmartlogContructorOptions) {
this.logContext = optionsArg.logContext;
this.minimumLogLevel = optionsArg.minimumLogLevel;
}
2018-10-30 17:56:26 +00:00
2018-01-28 03:31:06 +00:00
// ============
// Logger Setup
// ============
/**
* enables console logging
*/
2018-06-05 18:48:14 +00:00
enableConsole() {
this.consoleEnabled = true;
2018-01-28 03:31:06 +00:00
}
// =============
// log functions
// =============
/**
2018-11-04 17:21:18 +00:00
* main log method
* @param logLevelArg - the log level
* @param logMessageArg - the log message
* @param logDataArg - any additional log data
2018-01-28 03:31:06 +00:00
*/
2018-11-04 17:21:18 +00:00
public log(logLevelArg: TLogLevel, logMessageArg: string, logDataArg?: any) {
2018-07-10 20:36:53 +00:00
if (this.consoleEnabled) {
2018-11-03 22:19:15 +00:00
console.log(`LOG: ${logLevelArg}: ${logMessageArg}`);
2018-07-10 20:36:53 +00:00
}
2018-11-04 17:21:18 +00:00
const logPackage: ILogPackage = {
timestamp: Date.now(),
2018-11-03 22:19:15 +00:00
type: 'log',
context: this.logContext,
level: logLevelArg,
2018-10-30 17:56:26 +00:00
message: logMessageArg
2018-11-04 17:21:18 +00:00
};
if(logDataArg) {
logPackage.data = logDataArg;
}
this.logRouter.routeLog(logPackage);
2018-07-10 20:36:53 +00:00
}
2018-01-28 03:31:06 +00:00
2018-11-03 22:19:15 +00:00
public increment(logLevelArg: TLogLevel, logMessageArg) {
if (this.consoleEnabled) {
console.log(`INCREMENT: ${logLevelArg}: ${logMessageArg}`);
}
this.logRouter.routeLog({
2018-11-04 17:21:18 +00:00
timestamp: Date.now(),
2018-11-03 22:19:15 +00:00
type: 'increment',
context: this.logContext,
level: logLevelArg,
message: logMessageArg
});
2018-01-28 03:31:06 +00:00
}
2018-11-11 00:40:08 +00:00
public handleLogPackage(logPackageArg: ILogPackage) {
this.logRouter.routeLog(logPackageArg);
}
2018-01-28 03:31:06 +00:00
}