Skip to content

Instantly share code, notes, and snippets.

@bloodf
Created July 22, 2019 13:57
Show Gist options
  • Save bloodf/001ee948a38229c9f42b257592e5e2fb to your computer and use it in GitHub Desktop.
Save bloodf/001ee948a38229c9f42b257592e5e2fb to your computer and use it in GitHub Desktop.
PrettyLog
export enum LogLevelName {
Info = 'info',
Warn = 'warn',
Error = 'error',
Method = 'method',
All = 'all',
None = 'none',
}
export enum LogLevelStyle {
Info = 'background:#215ace ; padding: 2px; border-radius: 2px; color: #fff;',
Warn = 'background:#e8c82c ; padding: 2px; border-radius: 2px; color: #000;',
Error = 'background:#c92112 ; padding: 2px; border-radius: 2px; color: #fff;',
Method = 'background:#6d0cb2 ; padding: 2px; border-radius: 2px; color: #fff;',
}
const detectNode:
boolean = Object.prototype.toString.call(typeof process !== 'undefined' ? process : 0) === '[object process]';
/**
* Abstract class to be used for log
*/
const log: (
name: string,
style: string,
className: string,
classStyle: string,
message: any | any[],
) => void = (
name: string,
style: string,
className: string,
classStyle: string,
message: any | any[],
): void => {
if (detectNode) {
// tslint:disable-next-line:no-console
console.log(`${ className }[${ name }]: ${ Array.isArray(message) ? message.join(' | ') : message }`);
} else {
// tslint:disable-next-line:no-console
console.log(`%c ${ name } %c ${ className } %c ${
Array.isArray(message) ? message.join(' | ') : message
}`, style, classStyle, 'background: transparent;');
}
};
const logError: (
name: string,
style: string,
className: string,
classStyle: string,
message: string | Error,
) => void = (
name: string,
style: string,
className: string,
classStyle: string,
message: string | Error,
): void => {
if (detectNode) {
// tslint:disable-next-line:no-console
console.error(`${ className }[${ name }]: `, message);
} else {
// tslint:disable-next-line:no-console
console.error(`%c ${ name } %c ${ className } %c ${ message }`, style, classStyle, 'background: transparent;');
}
};
/**
* Abstract LogManager Class
*/
export abstract class PrettyLog {
/**
* Class name for using with the log
*/
protected className: string = 'PrettyLog';
/**
* Log style to be used when the log is called
*/
protected logStyle: string = 'background:#44AB53 ; padding: 2px; border-radius: 2px; color: #fff;';
/**
* Show the logs on the console
*/
protected showLogs: LogLevelName = LogLevelName.None;
protected get verboseMode(): boolean {
return this.showLogs === LogLevelName.Info || this.showLogs === LogLevelName.All;
}
/**
* Log an info event
*/
protected logError(error: Error | string): void {
if (this.showLogs === LogLevelName.Error || this.showLogs === LogLevelName.All) {
if (error instanceof Error && error.message) {
logError(
LogLevelName.Error,
LogLevelStyle.Error,
this.className,
this.logStyle,
error.message,
);
}
logError(
LogLevelName.Error,
LogLevelStyle.Error,
this.className,
this.logStyle,
Error(`${ typeof error === 'object' ? JSON.stringify(error) : error }`),
);
}
}
/**
* Log an info event
*/
protected logInfo(...message: any | any[]): void {
if (
this.showLogs === LogLevelName.Info
|| this.showLogs === LogLevelName.All
) {
log(
LogLevelName.Info,
LogLevelStyle.Info,
this.className,
this.logStyle,
message,
);
}
}
/**
* Log an method call
*/
protected logMethod(method: string, args?: string, ...params: any[]): void {
if (
this.showLogs === LogLevelName.Method
|| this.showLogs === LogLevelName.All
) {
log(
LogLevelName.Method,
LogLevelStyle.Method,
this.className,
this.logStyle,
`Call Method: ${ method }(${ args ? args : '' }) ${ (params) ? `=> ${ JSON.stringify(params) }` : '' }`,
);
}
}
/**
* Log an info event
*/
protected logWarn(...warn: any | any[]): void {
if (this.showLogs === LogLevelName.Warn
|| this.showLogs === LogLevelName.All
) {
log(
LogLevelName.Warn,
LogLevelStyle.Warn,
this.className,
this.logStyle,
warn,
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment