Files
smartlog/ts/smartlog.classes.smartlog.ts

87 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

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