smartlog/ts/smartlog.classes.smartlog.ts

85 lines
1.8 KiB
TypeScript
Raw 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-07-08 20:30:21 +00:00
import { TEnvironment, ILogContext, TLogLevel, TRuntime } 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-10-30 17:56:26 +00:00
public logRouter = new LogRouter();
public addLogDestination = this.logRouter.addLogDestination;
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
}
/**
* set a minimum serverity level to log
2018-01-28 03:31:44 +00:00
* @param levelArg
2018-01-28 03:31:06 +00:00
*/
2018-06-05 18:48:14 +00:00
level(levelArg: TLogLevel) {}
2018-01-28 03:31:06 +00:00
// =============
// log functions
// =============
/**
* log stuff
* @param logLevelArg
2018-01-28 03:31:44 +00:00
* @param logMessageArg
2018-01-28 03:31:06 +00:00
*/
2018-10-30 17:56:26 +00:00
public log(logLevelArg: TLogLevel, logMessageArg: string) {
2018-07-10 20:36:53 +00:00
if (this.consoleEnabled) {
2018-10-30 17:56:26 +00:00
console.log(`${logLevelArg}: ${logMessageArg}`);
2018-07-10 20:36:53 +00:00
}
2018-10-30 17:56:26 +00:00
this.logRouter.routeLog({
logContext: this.logContext,
logLevel: logLevelArg,
message: logMessageArg
});
2018-07-10 20:36:53 +00:00
}
2018-01-28 03:31:06 +00:00
2018-10-30 17:56:26 +00:00
public silly(logMessageArg: string) {
2018-06-05 18:48:14 +00:00
this.log('silly', logMessageArg);
2018-01-28 03:31:06 +00:00
}
2018-10-30 17:56:26 +00:00
public debug(logMessageArg) {
2018-06-05 18:48:14 +00:00
this.log('debug', logMessageArg);
2018-01-28 03:31:06 +00:00
}
2018-10-30 17:56:26 +00:00
public info(logMessageArg: string) {
2018-06-05 18:48:14 +00:00
this.log('info', logMessageArg);
2018-01-28 03:31:06 +00:00
}
2018-10-30 17:56:26 +00:00
public warn(logMessageArg) {
2018-06-05 18:48:14 +00:00
this.log('warn', logMessageArg);
2018-01-28 03:31:06 +00:00
}
2018-10-30 17:56:26 +00:00
public error(logMessageArg) {
2018-06-05 18:48:14 +00:00
this.log('error', logMessageArg);
2018-01-28 03:31:06 +00:00
}
}