smartlog/ts/smartlog.classes.smartlog.ts

122 lines
3.4 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-10-30 17:56:26 +00:00
import { LogRouter } from './smartlog.classes.logrouter';
2018-10-31 17:51:54 +00:00
export interface ISmartlogContructorOptions {
2019-10-22 13:04:15 +00:00
logContext: plugins.smartlogInterfaces.ILogContext;
minimumLogLevel?: plugins.smartlogInterfaces.TLogLevel;
2018-10-31 17:51:54 +00:00
}
2020-06-05 01:53:09 +00:00
export class Smartlog implements plugins.smartlogInterfaces.ILogDestination {
2019-10-22 13:04:15 +00:00
private logContext: plugins.smartlogInterfaces.ILogContext;
private minimumLogLevel: plugins.smartlogInterfaces.TLogLevel;
2019-01-15 23:01:19 +00:00
2018-06-05 18:48:14 +00:00
private consoleEnabled: boolean;
2019-01-15 23:01:19 +00:00
2018-11-04 13:19:31 +00:00
private logRouter = new LogRouter();
2018-10-30 17:56:26 +00:00
2019-10-22 13:04:15 +00:00
public addLogDestination(logDestinationArg: plugins.smartlogInterfaces.ILogDestination) {
2018-11-04 14:26:40 +00:00
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-01-28 03:31:06 +00:00
// ============
// Logger Setup
// ============
/**
* enables console logging
*/
2020-06-07 13:23:56 +00:00
public enableConsole(optionsArg?: { captureAll: boolean }) {
2020-06-05 01:53:09 +00:00
if (optionsArg && optionsArg.captureAll) {
const write = process.stdout.write;
/* import * as fs from 'fs';
const fileStream = fs.createWriteStream(plugins.path.join(paths.nogitDir, 'output.txt'), {
flags: 'a+'
}); */
process.stdout.write = (...args) => {
if (!args[0].startsWith('LOG')) {
this.log('info', args[0]);
return;
}
// fileStream.write(args[0]);
write.apply(process.stdout, args);
return true;
};
}
2018-06-05 18:48:14 +00:00
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
2020-06-05 09:25:28 +00:00
* @param correlationArg - info about corrleations
2018-01-28 03:31:06 +00:00
*/
2019-10-22 13:11:40 +00:00
public log(
logLevelArg: plugins.smartlogInterfaces.TLogLevel,
logMessageArg: string,
2020-06-05 01:53:09 +00:00
logDataArg?: any,
2020-06-05 09:25:28 +00:00
correlationArg: plugins.smartlogInterfaces.ILogCorrelation = {
id: plugins.isounique.uni(),
type: 'none'
}
2019-10-22 13:11:40 +00:00
) {
2018-07-10 20:36:53 +00:00
if (this.consoleEnabled) {
2020-06-07 13:23:56 +00:00
this.safeConsoleLog(`${logLevelArg}: ${logMessageArg}`);
2018-07-10 20:36:53 +00:00
}
2019-10-22 13:04:15 +00:00
const logPackage: plugins.smartlogInterfaces.ILogPackage = {
2018-11-04 17:21:18 +00:00
timestamp: Date.now(),
2018-11-03 22:19:15 +00:00
type: 'log',
context: this.logContext,
level: logLevelArg,
2020-06-05 09:25:28 +00:00
correlation: correlationArg,
2018-10-30 17:56:26 +00:00
message: logMessageArg
2018-11-04 17:21:18 +00:00
};
2019-01-15 23:01:19 +00:00
if (logDataArg) {
2018-11-04 17:21:18 +00:00
logPackage.data = logDataArg;
}
this.logRouter.routeLog(logPackage);
2018-07-10 20:36:53 +00:00
}
2018-01-28 03:31:06 +00:00
2020-06-05 01:53:09 +00:00
public increment(
logLevelArg: plugins.smartlogInterfaces.TLogLevel,
logMessageArg: string,
logDataArg?: any,
2020-06-05 09:25:28 +00:00
correlationArg: plugins.smartlogInterfaces.ILogCorrelation = {
id: plugins.isounique.uni(),
type: 'none'
}
2020-06-05 01:53:09 +00:00
) {
2018-11-03 22:19:15 +00:00
if (this.consoleEnabled) {
2020-06-05 01:53:09 +00:00
this.safeConsoleLog(`INCREMENT: ${logLevelArg}: ${logMessageArg}`);
2018-11-03 22:19:15 +00:00
}
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,
2020-06-05 01:53:09 +00:00
message: logMessageArg,
2020-06-05 09:25:28 +00:00
correlation: correlationArg
2018-11-03 22:19:15 +00:00
});
2018-01-28 03:31:06 +00:00
}
2018-11-11 00:40:08 +00:00
2020-06-05 01:53:09 +00:00
public handleLog(logPackageArg: plugins.smartlogInterfaces.ILogPackage) {
2018-11-11 00:40:08 +00:00
this.logRouter.routeLog(logPackageArg);
}
2020-06-05 01:53:09 +00:00
private safeConsoleLog(logLine: string) {
2020-06-07 13:23:56 +00:00
console.log(
`LOG => ${new Date().getHours()}:${new Date().getMinutes()}:${new Date().getSeconds()} => ${logLine}`
);
2020-06-05 01:53:09 +00:00
}
2018-01-28 03:31:06 +00:00
}