Compare commits

..

No commits in common. "6864756573f61c031b8c63bd80470d2f0b2d757f" and "fd77f1d288d385df3aa7acd80470da475560b1e9" have entirely different histories.

7 changed files with 54 additions and 312 deletions

14
package-lock.json generated
View File

@ -1,13 +1,13 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "2.0.0", "version": "1.1.0",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@hibas123/utils": { "@hibas123/utils": {
"version": "2.0.5", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.5.tgz", "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.0.2.tgz",
"integrity": "sha512-LI1Xw0QArpm0JqCUmuxRG7dgmjvyLPmJehcNRqHrWuCwmDRoPFe0PwmZsp8/saz6pCL2A4u0utVHWG2PG4lSyg==" "integrity": "sha512-5yrpuuUv0YlL2BixiTQLta4EmPLMNZ9Waorymu6dbERiSIcBxDs8mcJDUDW7CGiewpHHQUMsqLycLUYPDx0LWw=="
}, },
"abbrev": { "abbrev": {
"version": "1.1.1", "version": "1.1.1",
@ -2782,9 +2782,9 @@
"dev": true "dev": true
}, },
"typescript": { "typescript": {
"version": "3.4.1", "version": "3.3.4000",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.4.1.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.3.4000.tgz",
"integrity": "sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q==", "integrity": "sha512-jjOcCZvpkl2+z7JFn0yBOoLQyLoIkNZAs/fYJkUG6VKy6zLPHJGfQJYFHzibB6GJaF/8QrcECtlQ5cpvRHSMEA==",
"dev": true "dev": true
}, },
"undefsafe": { "undefsafe": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@hibas123/logging", "name": "@hibas123/logging",
"version": "2.0.1", "version": "1.1.1",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
@ -21,9 +21,9 @@
"devDependencies": { "devDependencies": {
"concurrently": "^4.1.0", "concurrently": "^4.1.0",
"nodemon": "^1.18.10", "nodemon": "^1.18.10",
"typescript": "^3.4.1" "typescript": "^3.3.4000"
}, },
"dependencies": { "dependencies": {
"@hibas123/utils": "^2.0.5" "@hibas123/utils": "^2.0.2"
} }
} }

View File

@ -1,19 +1,37 @@
import { Observable } from "@hibas123/utils"; import { Observable } from "@hibas123/utils";
import { ConsoleWriter } from "./consolewriter"; import { ConsoleWriter } from "./consolewriter";
import inspect from "./inspect"; import inspect from "./inspect";
import { Adapter, LoggingTypes, Message, FormatConfig, DefaultFormatConfig, Format, FormatTypes, Colors, FormattedText, FormattedLine } from "./types"; import { Adapter, LoggingTypes, Message } from "./types";
export function removeColors(text: string) { export const Colors = {
text = text.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ""); Reset: "\x1b[0m",
Bright: "\x1b[1m",
Dim: "\x1b[2m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
// let index = text.indexOf("\x1b"); FgBlack: "\x1b[30m",
// while (index >= 0) { FgRed: "\x1b[31m",
// text = text.substring(0, index) + text.substring(index + 5, text.length); FgGreen: "\x1b[32m",
// index = text.indexOf("\x1b"); FgYellow: "\x1b[33m",
// } FgBlue: "\x1b[34m",
return text; FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m"
} }
export interface LoggingBaseOptions { export interface LoggingBaseOptions {
/** /**
* Name will be prefixed on Console output and added to logfiles, if not specified here * Name will be prefixed on Console output and added to logfiles, if not specified here
@ -26,14 +44,6 @@ export interface LoggingBaseOptions {
} }
export class LoggingBase { export class LoggingBase {
private _formatMap: FormatConfig = new DefaultFormatConfig();
public set formatMap(value: FormatConfig) {
this._formatMap = value;
}
private adapter: Adapter[] = []; private adapter: Adapter[] = [];
private adapter_init: Promise<void>[] = []; private adapter_init: Promise<void>[] = [];
@ -88,10 +98,6 @@ export class LoggingBase {
} }
} }
public close() {
this.adapter.forEach(adapter => adapter.close ? adapter.close() : undefined);
}
public waitForSetup() { public waitForSetup() {
return Promise.all(this.adapter_init); return Promise.all(this.adapter_init);
} }
@ -108,10 +114,6 @@ export class LoggingBase {
this.message(LoggingTypes.Warning, message); this.message(LoggingTypes.Warning, message);
} }
warn(...message: any[]) {
this.message(LoggingTypes.Warning, message);
}
error(error: Error | string) { error(error: Error | string) {
if (!error) error = "Empty ERROR was passed, so no informations available"; if (!error) error = "Empty ERROR was passed, so no informations available";
if (typeof error === "string") { if (typeof error === "string") {
@ -144,52 +146,29 @@ export class LoggingBase {
}); });
} }
let lines = removeColors(mb).split("\n"); let lines = mb.split("\n");
let type_format: Format[] = []; let color = Colors.Reset;
switch (type) { switch (type) {
case LoggingTypes.Log: case LoggingTypes.Log:
type_format = this._formatMap.log; //m += FgWhite + BgBlack;
break; break;
case LoggingTypes.Error: case LoggingTypes.Error:
type_format = this._formatMap.error; color += Colors.FgRed;//FgWhite + BgRed + FgWhite;
break; break;
case LoggingTypes.Debug: case LoggingTypes.Debug:
type_format = this._formatMap.debug; color += Colors.FgCyan;
break; break;
case LoggingTypes.Warning: case LoggingTypes.Warning:
type_format = this._formatMap.warning; color += Colors.FgYellow;
break; break;
} }
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''); let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " "); let prefix = `[${date}][${color}${LoggingTypes[type].toUpperCase().padEnd(5, " ")}${Colors.Reset}][${file}]: `;
const prefix: FormattedText[] = [];
const a = (text: string, formats: Format[] = []) => {
prefix.push({
text,
formats
})
}
a("[");
a(date, this._formatMap.date);
a("][");
a(type_str, type_format);
if (file_raw.file) {
a("][");
a(file, this._formatMap.file);
}
a("]: ");
let formatted: FormattedLine[] = lines.map<FormattedText[]>(line => [...prefix, {
text: line,
formats: []
}]);
let formatted = lines.map(line => prefix + line);
let msg: Message = { let msg: Message = {
date: new Date(), date: new Date(),

View File

@ -1,8 +1,7 @@
import { ObservableInterface } from "@hibas123/utils"; import { ObservableInterface } from "@hibas123/utils";
import { Colors } from "./index"; import { Colors } from "./index";
import { Adapter, Message, FormattedLine, TerminalFormats, FormatTypes } from "./types"; import { Adapter, LoggingTypes, Message } from "./types";
const browser = typeof window !== "undefined";
export class ConsoleWriter implements Adapter { export class ConsoleWriter implements Adapter {
init(observable: ObservableInterface<Message>) { init(observable: ObservableInterface<Message>) {
@ -11,137 +10,10 @@ export class ConsoleWriter implements Adapter {
flush() { } flush() { }
// TODO: Check if required!
// private escape(text: string): string {
// return text
// .replace(/%s/g, "%%s")
// .replace(/%c/g, "%%c")
// }
private formatLine(line: FormattedLine): [string, string[] | undefined] {
let text = "";
let style_formats: string[] = [];
if (!browser) {
for (let part of line) {
let formats = "";
for (let format of part.formats) {
switch (format.type) {
case FormatTypes.BOLD:
formats += TerminalFormats.Bold;
break;
case FormatTypes.UNDERSCORE:
formats += TerminalFormats.Underscore;
break;
case FormatTypes.BLINK:
formats += TerminalFormats.Blink;
break;
case FormatTypes.COLOR:
switch (format.color) {
case Colors.RED:
formats += TerminalFormats.FgRed;
break;
case Colors.GREEN:
formats += TerminalFormats.FgGreen;
break;
case Colors.YELLOW:
formats += TerminalFormats.FgRed;
break;
case Colors.BLUE:
formats += TerminalFormats.FgBlue;
break;
case Colors.MAGENTA:
formats += TerminalFormats.FgMagenta;
break;
case Colors.CYAN:
formats += TerminalFormats.FgCyan;
break;
case Colors.WHITE:
formats += TerminalFormats.FgWhite;
break;
}
break;
}
}
text += formats + part.text + TerminalFormats.Reset;
}
} else {
for (let part of line) {
let styles: string[] = [];
let resetStyles: string[] = [];
for (let format of part.formats) {
switch (format.type) {
case FormatTypes.BOLD:
styles.push("font-weight: bold;");
resetStyles.push("font-weight: unset");
break;
case FormatTypes.UNDERSCORE:
styles.push("text-decoration: underline");
resetStyles.push("text-decoration: unset");
break;
case FormatTypes.BLINK:
styles.push("text-decoration: blink");
resetStyles.push("text-decoration: unset");
break;
case FormatTypes.COLOR:
let color = "";
switch (format.color) {
case Colors.RED:
color = "red";
break;
case Colors.GREEN:
color = "green";
break;
case Colors.YELLOW:
color = "gold";
break;
case Colors.BLUE:
color = "blue";
break;
case Colors.MAGENTA:
color = "magenta";
break;
case Colors.CYAN:
color = "cyan";
break;
case Colors.WHITE:
color = "white";
break;
}
styles.push("color: " + color);
resetStyles.push("color: unset");
break;
}
}
text += "%c" + part.text.replace(/%c/g, "%%c") + "%c";
style_formats.push(styles.join(";"), resetStyles.join(";"));
}
}
return [text, style_formats];
}
onMessage(message: Message) { onMessage(message: Message) {
let lines = message.text.formatted; let lines = message.text.formatted;
let name = "";
let prefix = ""; if (message.name) name = `[${message.name}]=>`;
if (message.name) prefix = `[${message.name}]=>`; lines.forEach(line => console.log(name + line + Colors.Reset))
if (browser) {
let text = "";
let formats: string[] = [];
lines.forEach(line => {
let [t, fmts] = this.formatLine(line);
text += t + "\n";
formats.push(...fmts);
})
// console.log(formats);
console.log(text, ...formats);
} else {
lines.forEach(line => {
let [text] = this.formatLine(line);
console.log(prefix + text);
})
}
} }
} }

View File

@ -1,20 +1,6 @@
import { LoggingBase } from "./base"; import { LoggingBase } from "./base";
export { Colors, LoggingBase, LoggingBaseOptions } from "./base";
export { LoggingBase, LoggingBaseOptions, removeColors } from "./base"; export { Adapter, LoggingTypes, Message } from "./types";
export {
Adapter,
LoggingTypes,
Message,
FormatConfig,
FormattedLine,
DefaultFormatConfig as DefaultColorMap,
FormattedText,
Colors,
Format,
FormatTypes,
TerminalFormats
} from "./types";
export { ObservableInterface } from "@hibas123/utils"; export { ObservableInterface } from "@hibas123/utils";
export let Logging: LoggingBase = undefined; export let Logging: LoggingBase = undefined;

View File

@ -13,7 +13,6 @@ if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
let cus = new LoggingBase({ name: "test" }); let cus = new LoggingBase({ name: "test" });
cus.log("Hello from custom Logger") cus.log("Hello from custom Logger")
cus.log("This has some %c symbols inside of it!");
let cus2 = new LoggingBase("test2"); let cus2 = new LoggingBase("test2");
cus2.log("Hello from custom Logger 2") cus2.log("Hello from custom Logger 2")

View File

@ -7,103 +7,12 @@ export enum LoggingTypes {
Debug Debug
} }
export const TerminalFormats = {
Reset: "\x1b[0m",
Bold: "\x1b[1m",
Underscore: "\x1b[4m",
Blink: "\x1b[5m",
Reverse: "\x1b[7m",
Hidden: "\x1b[8m",
FgBlack: "\x1b[30m",
FgRed: "\x1b[31m",
FgGreen: "\x1b[32m",
FgYellow: "\x1b[33m",
FgBlue: "\x1b[34m",
FgMagenta: "\x1b[35m",
FgCyan: "\x1b[36m",
FgWhite: "\x1b[37m",
BgBlack: "\x1b[40m",
BgRed: "\x1b[41m",
BgGreen: "\x1b[42m",
BgYellow: "\x1b[43m",
BgBlue: "\x1b[44m",
BgMagenta: "\x1b[45m",
BgCyan: "\x1b[46m",
BgWhite: "\x1b[47m"
}
export enum FormatTypes {
COLOR,
BOLD,
UNDERSCORE,
BLINK
}
export enum Colors {
NONE,
RED,
GREEN,
YELLOW,
BLUE,
MAGENTA,
CYAN,
WHITE
}
export interface FormatConfig {
error: Format[];
warning: Format[];
log: Format[];
debug: Format[];
date: Format[];
file: Format[];
}
function colorFormat(color: Colors) {
return {
type: FormatTypes.COLOR,
color
}
}
const boldFormat = {
type: FormatTypes.BOLD
};
export class DefaultFormatConfig implements FormatConfig {
error = [colorFormat(Colors.RED), boldFormat];
warning = [colorFormat(Colors.YELLOW), boldFormat];
log = [colorFormat(Colors.NONE), boldFormat];
debug = [colorFormat(Colors.CYAN), boldFormat];
date = [colorFormat(Colors.NONE)];
file = [colorFormat(Colors.NONE)];
}
export interface Format {
type: FormatTypes;
color?: Colors;
}
export interface FormattedText {
text: string;
formats: Format[];
}
export type FormattedLine = FormattedText[];
export interface Message { export interface Message {
type: LoggingTypes; type: LoggingTypes;
name?: string; name?: string;
text: { text: {
raw: string[], raw: string[],
formatted: FormattedLine[] formatted: string[]
}; };
date: Date; date: Date;
file: string; file: string;
@ -114,7 +23,4 @@ export interface Adapter {
flush(sync: true): void; flush(sync: true): void;
flush(sync: false): void | Promise<void>; flush(sync: false): void | Promise<void>;
close?(): void;
} }