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

149 lines
3.9 KiB
TypeScript
Raw Normal View History

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
import { Ora } from './sl.destlocal.classes.ora';
2018-11-04 12:22:45 +00:00
import { TColorName } from '@pushrocks/consolecolor';
2018-03-03 12:57:55 +00:00
export class DestinationLocal implements ILogDestination {
2018-03-03 12:57:55 +00:00
private oraInstance: Ora;
/**
* 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
}
get ora(): Ora {
if (!this.oraInstance) {
this.oraInstance = new Ora(this);
}
return this.oraInstance;
}
/**
* 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'
},
message: logTextArg
2018-03-03 12:57:55 +00:00
});
}
}
private previousMessage: string = '';
private sameMessageCounter: number = 0;
// default logging
2018-11-04 12:22:45 +00:00
logToConsole(logPackageArg: ILogPackage) {
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
);
if (this.ora.state === 'running') {
this.ora.pause();
}
2018-03-03 12:57:55 +00:00
console.log(logString);
if (this.ora.state === 'paused') {
this.ora.start();
}
2018-03-03 12:57:55 +00:00
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
};
}