smartlog-destination-local/ts/classes.destinationlocal.ts

136 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

2024-04-24 16:30:06 +00:00
import * as plugins from './plugins.js';
import type { ILogDestination, ILogPackage, TLogLevel } from '@push.rocks/smartlog-interfaces';
2018-03-03 12:57:55 +00:00
// other beautylog classes
2024-04-24 16:30:06 +00:00
import { type TColorName } from '@push.rocks/consolecolor';
2018-03-03 12:57:55 +00:00
export class DestinationLocal implements ILogDestination {
/**
* handles a log according to the smartlog standard
* @param logPackage
*/
2020-06-11 10:35:20 +00:00
public async handleLog(logPackage: ILogPackage) {
2018-11-04 12:22:45 +00:00
this.logToConsole(logPackage);
2018-03-03 12:57:55 +00:00
}
/**
* creates a new empty line
* @param linesArg
* @returns void
*/
2018-11-04 12:22:45 +00:00
public newLine(linesArg: number = 1) {
2018-03-03 12:57:55 +00:00
for (let i = 0; i < linesArg; i++) {
console.log('\n');
}
}
/**
* logs a reduced log that only logs changes of consequential log messages
*/
2018-11-04 12:22:45 +00:00
public logReduced(logTextArg: string, repeatEveryTimesArg: number = 0) {
2018-03-03 12:57:55 +00:00
if (
logTextArg === this.previousMessage &&
(repeatEveryTimesArg === 0 || this.sameMessageCounter !== repeatEveryTimesArg)
) {
this.sameMessageCounter++;
} else {
this.sameMessageCounter = 0;
this.previousMessage = logTextArg;
this.logToConsole({
2018-11-04 17:23:41 +00:00
timestamp: Date.now(),
2018-11-04 12:22:45 +00:00
type: 'log',
level: 'info',
context: {
company: 'undefined',
companyunit: 'undefined',
containerName: 'undefined',
environment: 'test',
runtime: 'node',
2022-10-26 16:41:04 +00:00
zone: 'undefined',
2018-11-04 12:22:45 +00:00
},
2020-06-05 02:01:12 +00:00
message: logTextArg,
2020-06-05 09:35:42 +00:00
correlation: {
id: 'none',
2022-10-26 16:41:04 +00:00
type: 'none',
},
2018-03-03 12:57:55 +00:00
});
}
}
private previousMessage: string = '';
private sameMessageCounter: number = 0;
// default logging
private logToConsole(logPackageArg: ILogPackage) {
2018-11-04 12:22:45 +00:00
let logString: string;
2018-03-03 12:57:55 +00:00
try {
2018-11-04 12:22:45 +00:00
logString =
this.localBl[logPackageArg.level].prefix +
plugins.consolecolor.coloredString(
logPackageArg.message,
this.localBl[logPackageArg.level].textColor
);
2018-03-03 12:57:55 +00:00
console.log(logString);
return true;
} catch (error) {
console.log(
this.localBl.errorPrefix + 'You seem to have tried logging something strange' + error
);
return false;
}
}
2018-11-04 12:22:45 +00:00
private localBl: {
[key: string]: {
prefix: string;
textColor: TColorName;
};
} = {
silly: {
prefix: plugins.consolecolor.coloredString(' silly ', 'white', 'blue') + ' ',
2022-10-26 16:41:04 +00:00
textColor: 'blue',
2018-11-04 12:22:45 +00:00
},
error: {
prefix:
plugins.consolecolor.coloredString(' ', 'red', 'red') +
plugins.consolecolor.coloredString(' ERROR! ', 'red', 'black') +
' ',
2022-10-26 16:41:04 +00:00
textColor: 'red',
2018-11-04 12:22:45 +00:00
},
info: {
prefix:
plugins.consolecolor.coloredString(' ', 'blue', 'blue') +
plugins.consolecolor.coloredString(' info: ', 'blue', 'black') +
' ',
2022-10-26 16:41:04 +00:00
textColor: 'white',
2018-11-04 12:22:45 +00:00
},
note: {
prefix:
plugins.consolecolor.coloredString(' ', 'pink', 'pink') +
plugins.consolecolor.coloredString(' note -> ', 'pink', 'black') +
' ',
2022-10-26 16:41:04 +00:00
textColor: 'pink',
2018-11-04 12:22:45 +00:00
},
ok: {
prefix:
plugins.consolecolor.coloredString(' ', 'green', 'green') +
plugins.consolecolor.coloredString(' ok ', 'green', 'black') +
' ',
2022-10-26 16:41:04 +00:00
textColor: 'green',
2018-11-04 12:22:45 +00:00
},
success: {
prefix:
plugins.consolecolor.coloredString(' ', 'green', 'green') +
plugins.consolecolor.coloredString(' SUCCESS! ', 'green', 'black') +
' ',
2022-10-26 16:41:04 +00:00
textColor: 'green',
2018-11-04 12:22:45 +00:00
},
warn: {
prefix:
plugins.consolecolor.coloredString(' ', 'orange', 'orange') +
plugins.consolecolor.coloredString(' WARN -> ', 'orange', 'black') +
' ',
2022-10-26 16:41:04 +00:00
textColor: 'orange',
},
2018-03-03 12:57:55 +00:00
};
}