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';
|
2020-06-08 20:39:40 +00:00
|
|
|
import { LogGroup } from '.';
|
2018-10-30 17:56:26 +00:00
|
|
|
|
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
|
|
|
|
2020-06-08 20:39:40 +00:00
|
|
|
public uniInstanceId: string = plugins.isounique.uni();
|
2020-06-08 16:16:54 +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-09-08 12:57:24 +00:00
|
|
|
public enableConsole(optionsArg?: { captureAll: boolean }) {
|
|
|
|
if (globalThis.process && optionsArg && optionsArg.captureAll) {
|
|
|
|
const process = globalThis.process;
|
|
|
|
const write = process.stdout.write;
|
|
|
|
process.stdout.write = (...args) => {
|
|
|
|
const logString: string = args[0];
|
|
|
|
if (!logString.startsWith('LOG') && typeof logString === 'string') {
|
|
|
|
switch (true) {
|
|
|
|
case logString.substr(0, 20).includes('Error:'):
|
|
|
|
this.log('error', logString);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.log('info', logString);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fileStream.write(args[0]);
|
|
|
|
write.apply(process.stdout, args);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
process.stderr.write = (...args) => {
|
|
|
|
if (!args[0].startsWith('LOG')) {
|
|
|
|
this.log('error', args[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// fileStream.write(args[0]);
|
|
|
|
write.apply(process.stderr, 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
|
|
|
*/
|
2020-06-11 10:11:37 +00:00
|
|
|
public async log(
|
2019-10-22 13:11:40 +00:00
|
|
|
logLevelArg: plugins.smartlogInterfaces.TLogLevel,
|
|
|
|
logMessageArg: string,
|
2020-06-05 01:53:09 +00:00
|
|
|
logDataArg?: any,
|
2020-06-08 16:16:54 +00:00
|
|
|
correlationArg?: plugins.smartlogInterfaces.ILogCorrelation
|
2019-10-22 13:11:40 +00:00
|
|
|
) {
|
2020-06-08 16:16:54 +00:00
|
|
|
correlationArg = {
|
|
|
|
...{
|
|
|
|
id: plugins.isounique.uni(),
|
|
|
|
type: 'none',
|
2020-09-07 21:30:02 +00:00
|
|
|
instance: this.uniInstanceId,
|
2020-06-08 16:16:54 +00:00
|
|
|
},
|
2020-09-07 21:30:02 +00:00
|
|
|
...correlationArg,
|
2020-06-08 16:16:54 +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
|
|
|
}
|
2020-06-08 16:16:54 +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,
|
2020-09-07 21:30:02 +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;
|
|
|
|
}
|
2020-06-11 10:11:37 +00:00
|
|
|
await 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(),
|
2020-09-07 21:30:02 +00:00
|
|
|
type: 'none',
|
2020-06-05 09:25:28 +00:00
|
|
|
}
|
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-09-07 21:30:02 +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-11 10:47:05 +00:00
|
|
|
public async handleLog(logPackageArg: plugins.smartlogInterfaces.ILogPackage) {
|
|
|
|
await this.logRouter.routeLog(logPackageArg);
|
2018-11-11 00:40:08 +00:00
|
|
|
}
|
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
|
|
|
}
|
2020-06-08 20:39:40 +00:00
|
|
|
|
|
|
|
public createLogGroup(transactionId: string = 'none') {
|
|
|
|
return new LogGroup(this, transactionId);
|
|
|
|
}
|
2018-01-28 03:31:06 +00:00
|
|
|
}
|