smartlog/ts/smartlog.classes.smartlog.ts

163 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-06-26 08:35:35 +00:00
import * as plugins from './smartlog.plugins.js';
2018-01-28 03:31:06 +00:00
2022-06-26 08:35:35 +00:00
import { LogRouter } from './smartlog.classes.logrouter.js';
import { LogGroup } from './smartlog.classes.loggroup.js';
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 {
2024-06-06 15:05:55 +00:00
// STATIC
public static createForCommitinfo(commitinfo: plugins.smartlogInterfaces.ILogContext['commitinfo']) {
return new Smartlog({
logContext: {
commitinfo
},
});
}
// INSTANCE
2022-07-26 19:03:39 +00:00
public logContext: plugins.smartlogInterfaces.ILogContext;
public 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;
2022-06-26 08:35:35 +00:00
this.minimumLogLevel = optionsArg.minimumLogLevel || 'silly';
2018-10-31 17:51:54 +00:00
}
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;
2024-05-17 23:15:16 +00:00
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
const originalStderrWrite = process.stderr.write.bind(process.stderr);
2021-07-06 17:26:35 +00:00
process.stdout.write = (...args: any) => {
2020-09-08 12:57:24 +00:00
const logString: string = args[0];
2024-05-17 23:15:16 +00:00
if (!logString || typeof logString !== 'string') {
// continue as planned
return originalStdoutWrite(...args);
}
if (!logString.startsWith('LOG')) {
if (logString.includes('Error:')) {
this.log('error', logString);
} else {
this.log('info', logString);
2020-09-08 12:57:24 +00:00
}
2022-06-26 08:35:35 +00:00
return true;
2020-09-08 12:57:24 +00:00
}
2024-05-17 23:15:16 +00:00
return originalStdoutWrite(...args);
2020-09-08 12:57:24 +00:00
};
2021-07-06 17:26:35 +00:00
process.stderr.write = (...args: any) => {
2024-05-17 23:15:16 +00:00
const logString: string = args[0];
if (!logString || typeof logString !== 'string' || !logString.startsWith('LOG')) {
this.log('error', logString);
2022-06-26 08:35:35 +00:00
return true;
2020-09-08 12:57:24 +00:00
}
2024-05-17 23:15:16 +00:00
return originalStderrWrite(...args);
2020-09-08 12:57:24 +00:00
};
}
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
}