2018-07-04 20:50:46 +00:00
|
|
|
import * as plugins from './sl.destlocal.plugins';
|
2018-11-04 12:22:45 +00:00
|
|
|
import { ILogDestination, ILogPackage, TLogLevel } from '@pushrocks/smartlog-interfaces';
|
2018-03-03 12:57:55 +00:00
|
|
|
|
|
|
|
// other beautylog classes
|
2018-11-04 12:22:45 +00:00
|
|
|
import { TColorName } from '@pushrocks/consolecolor';
|
2018-03-03 12:57:55 +00:00
|
|
|
|
2018-07-05 21:40:33 +00:00
|
|
|
export class DestinationLocal implements ILogDestination {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handles a log according to the smartlog standard
|
|
|
|
* @param logPackage
|
|
|
|
*/
|
2018-11-04 12:22:45 +00:00
|
|
|
public handleLog(logPackage: ILogPackage) {
|
|
|
|
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',
|
|
|
|
zone: 'undefined'
|
|
|
|
},
|
2020-06-05 02:01:12 +00:00
|
|
|
message: logTextArg,
|
|
|
|
correlationId: 'n/a'
|
2018-03-03 12:57:55 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private previousMessage: string = '';
|
|
|
|
private sameMessageCounter: number = 0;
|
|
|
|
|
|
|
|
// default logging
|
2019-06-07 10:00:58 +00:00
|
|
|
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') + ' ',
|
|
|
|
textColor: 'blue'
|
|
|
|
},
|
|
|
|
error: {
|
|
|
|
prefix:
|
|
|
|
plugins.consolecolor.coloredString(' ', 'red', 'red') +
|
|
|
|
plugins.consolecolor.coloredString(' ERROR! ', 'red', 'black') +
|
|
|
|
' ',
|
|
|
|
textColor: 'red'
|
|
|
|
},
|
|
|
|
info: {
|
|
|
|
prefix:
|
|
|
|
plugins.consolecolor.coloredString(' ', 'blue', 'blue') +
|
|
|
|
plugins.consolecolor.coloredString(' info: ', 'blue', 'black') +
|
|
|
|
' ',
|
|
|
|
textColor: 'white'
|
|
|
|
},
|
|
|
|
note: {
|
|
|
|
prefix:
|
|
|
|
plugins.consolecolor.coloredString(' ', 'pink', 'pink') +
|
|
|
|
plugins.consolecolor.coloredString(' note -> ', 'pink', 'black') +
|
|
|
|
' ',
|
|
|
|
textColor: 'pink'
|
|
|
|
},
|
|
|
|
ok: {
|
|
|
|
prefix:
|
|
|
|
plugins.consolecolor.coloredString(' ', 'green', 'green') +
|
|
|
|
plugins.consolecolor.coloredString(' ok ', 'green', 'black') +
|
|
|
|
' ',
|
|
|
|
textColor: 'green'
|
|
|
|
},
|
|
|
|
success: {
|
|
|
|
prefix:
|
|
|
|
plugins.consolecolor.coloredString(' ', 'green', 'green') +
|
|
|
|
plugins.consolecolor.coloredString(' SUCCESS! ', 'green', 'black') +
|
|
|
|
' ',
|
|
|
|
textColor: 'green'
|
|
|
|
},
|
|
|
|
warn: {
|
|
|
|
prefix:
|
|
|
|
plugins.consolecolor.coloredString(' ', 'orange', 'orange') +
|
|
|
|
plugins.consolecolor.coloredString(' WARN -> ', 'orange', 'black') +
|
|
|
|
' ',
|
|
|
|
textColor: 'orange'
|
|
|
|
}
|
2018-03-03 12:57:55 +00:00
|
|
|
};
|
|
|
|
}
|