Adding custom colored logging
This commit is contained in:
89
src/base.ts
89
src/base.ts
@ -126,26 +126,13 @@ export class LoggingBase {
|
||||
this.message(LoggingTypes.Error, message);
|
||||
}
|
||||
|
||||
private message(type: LoggingTypes, message: any[] | string, caller?: { file: string, line: number }) {
|
||||
private message(type: LoggingTypes, message: any[], caller?: { file: string, line: number }) {
|
||||
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||
|
||||
let file_raw = caller || getCallerFile();
|
||||
let file = `${file_raw.file}:${String(file_raw.line).padEnd(3, " ")}`;
|
||||
|
||||
let mb = "";
|
||||
if (typeof message === "string") {
|
||||
mb = message;
|
||||
} else {
|
||||
message.forEach((e, i) => {
|
||||
if (typeof e !== "string") e = inspect(e, { colors: true, showHidden: true, depth: 3 });
|
||||
if (e.endsWith("\n") || i === message.length - 1) {
|
||||
mb += e;
|
||||
} else {
|
||||
mb += e + " ";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let lines = removeColors(mb).split("\n");
|
||||
|
||||
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
|
||||
let type_format: Format[] = [];
|
||||
switch (type) {
|
||||
case LoggingTypes.Log:
|
||||
@ -162,12 +149,7 @@ export class LoggingBase {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
||||
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
|
||||
|
||||
const prefix: FormattedText[] = [];
|
||||
|
||||
const a = (text: string, formats: Format[] = []) => {
|
||||
prefix.push({
|
||||
text,
|
||||
@ -185,10 +167,49 @@ export class LoggingBase {
|
||||
}
|
||||
a("]: ");
|
||||
|
||||
let formatted: FormattedLine[] = lines.map<FormattedText[]>(line => [...prefix, {
|
||||
text: line,
|
||||
formats: []
|
||||
}]);
|
||||
|
||||
let mb = "";
|
||||
|
||||
const formatted: FormattedLine[] = [];
|
||||
let line: FormattedLine;
|
||||
const newLine = () => {
|
||||
if (line && line.length > 0)
|
||||
formatted.push(line);
|
||||
line = [...prefix];
|
||||
}
|
||||
newLine();
|
||||
|
||||
message.forEach((e, i) => {
|
||||
let formats: Format[] = [];
|
||||
if (typeof e !== "string") {
|
||||
if (typeof e === "object") {
|
||||
if (e[colorSymbol]) {
|
||||
formats.push({
|
||||
type: FormatTypes.COLOR,
|
||||
color: e[colorSymbol]
|
||||
})
|
||||
e = e.value;
|
||||
}
|
||||
}
|
||||
if (typeof e !== "string")
|
||||
e = inspect(e, { colors: true, showHidden: true, depth: 3 }) as string;
|
||||
}
|
||||
|
||||
removeColors(e).split("\n").map((text, index, { length }) => {
|
||||
line.push({ text, formats });
|
||||
if (index < length - 1) {
|
||||
newLine();
|
||||
}
|
||||
})
|
||||
|
||||
if (!e.endsWith("\n") && i < message.length - 1) {
|
||||
line.push({ text: " ", formats: [] });
|
||||
}
|
||||
});
|
||||
|
||||
newLine();
|
||||
|
||||
let raw = removeColors(mb).split("\n");
|
||||
|
||||
|
||||
let msg: Message = {
|
||||
@ -196,7 +217,7 @@ export class LoggingBase {
|
||||
file,
|
||||
name: this._name,
|
||||
text: {
|
||||
raw: lines,
|
||||
raw,
|
||||
formatted
|
||||
},
|
||||
type
|
||||
@ -206,6 +227,20 @@ export class LoggingBase {
|
||||
}
|
||||
}
|
||||
|
||||
const colorSymbol = Symbol("color");
|
||||
|
||||
export interface ColorFormat {
|
||||
[colorSymbol]: Colors;
|
||||
value: any;
|
||||
}
|
||||
|
||||
export function withColor(color: Colors, value: any): ColorFormat {
|
||||
return {
|
||||
[colorSymbol]: color,
|
||||
value
|
||||
}
|
||||
}
|
||||
|
||||
function getStack() {
|
||||
// Save original Error.prepareStackTrace
|
||||
let origPrepareStackTrace = (<any>Error).prepareStackTrace;
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { LoggingBase } from "./base";
|
||||
export { ConsoleAdapter } from "./consolewriter";
|
||||
export { LoggingBase, LoggingBaseOptions, removeColors } from "./base";
|
||||
export { LoggingBase, LoggingBaseOptions, removeColors, withColor } from "./base";
|
||||
export {
|
||||
Adapter,
|
||||
LoggingTypes,
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Logging, LoggingBase } from ".";
|
||||
import { Logging, LoggingBase, Colors, withColor } from ".";
|
||||
|
||||
Logging.log("test")
|
||||
Logging.log("i", "am", { a: "an" }, 1000);
|
||||
@ -8,6 +8,9 @@ Logging.errorMessage("i", "am", "an", "error");
|
||||
|
||||
Logging.log("\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m\x1b[31m TEST \x1b[31m\x1b[31m\x1b[31m")
|
||||
|
||||
Logging.log(withColor(Colors.MAGENTA, "This text should be magenta!"), "This not!")
|
||||
Logging.log(withColor(Colors.MAGENTA, { somekey: "Some value" }))
|
||||
|
||||
let err = new Error()
|
||||
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
|
||||
|
||||
|
@ -43,7 +43,6 @@ export enum FormatTypes {
|
||||
BLINK
|
||||
}
|
||||
|
||||
|
||||
export enum Colors {
|
||||
NONE,
|
||||
RED,
|
||||
|
Reference in New Issue
Block a user