diff --git a/package-lock.json b/package-lock.json index 0aa784b..0a13e0e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,9 +5,9 @@ "requires": true, "dependencies": { "@hibas123/utils": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.1.0.tgz", - "integrity": "sha512-rS0SMENN7ta6pv9bNmIls8lvEOHbbDQXpiDI3u3z0Vx8B43ngEmEXbu5vKBA58ekYLr1WdHH6iIiySDChoi/RQ==" + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/@hibas123/utils/-/utils-2.1.1.tgz", + "integrity": "sha512-A5yQfPsG+B5Vg6TsfJPqRi0tbjfobsTEoiTSFS+r+FFGStTL+B29zN5XI7D1RafL+dveKhblqSZ1Pi1YtEMAVw==" }, "abbrev": { "version": "1.1.1", @@ -283,9 +283,9 @@ } }, "chokidar": { - "version": "2.1.6", - "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.1.6.tgz", - "integrity": "sha512-V2jUo67OKkc6ySiRpJrjlpJKl9kDuG+Xb8VgsGzb+aEouhgS1D0weyPU4lEzdAcsCAvrih2J2BqyXqHWvVLw5g==", + "version": "2.1.8", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.1.8.tgz", + "integrity": "sha512-ZmZUazfOzf0Nve7duiCKD23PFSCs4JPoYyccjUFF3aQkQadqBhfzhjkwBH2mNOG9cTBwhamM37EIsIkZw3nRgg==", "dev": true, "requires": { "anymatch": "^2.0.0", @@ -392,20 +392,20 @@ "dev": true }, "concurrently": { - "version": "4.1.1", - "resolved": "https://registry.npmjs.org/concurrently/-/concurrently-4.1.1.tgz", - "integrity": "sha512-48+FE5RJ0qc8azwKv4keVQWlni1hZeSjcWr8shBelOBtBHcKj1aJFM9lHRiSc1x7lq416pkvsqfBMhSRja+Lhw==", + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/concurrently/-/concurrently-5.0.0.tgz", + "integrity": "sha512-1yDvK8mduTIdxIxV9C60KoiOySUl/lfekpdbI+U5GXaPrgdffEavFa9QZB3vh68oWOpbCC+TuvxXV9YRPMvUrA==", "dev": true, "requires": { - "chalk": "^2.4.1", - "date-fns": "^1.23.0", - "lodash": "^4.17.10", + "chalk": "^2.4.2", + "date-fns": "^2.0.1", + "lodash": "^4.17.15", "read-pkg": "^4.0.1", - "rxjs": "^6.3.3", + "rxjs": "^6.5.2", "spawn-command": "^0.0.2-1", "supports-color": "^4.5.0", - "tree-kill": "^1.1.0", - "yargs": "^12.0.1" + "tree-kill": "^1.2.1", + "yargs": "^12.0.5" } }, "configstore": { @@ -463,9 +463,9 @@ "dev": true }, "date-fns": { - "version": "1.30.1", - "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-1.30.1.tgz", - "integrity": "sha512-hBSVCvSmWC+QypYObzwGOd9wqdDpOt+0wl0KbU+R+uuZBS1jN8VsD1ss3irQDknRj5NvxiTF6oj/nDRnN/UQNw==", + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/date-fns/-/date-fns-2.7.0.tgz", + "integrity": "sha512-wxYp2PGoUDN5ZEACc61aOtYFvSsJUylIvCjpjDOqM1UDaKIIuMJ9fAnMYFHV3TQaDpfTVxhwNK/GiCaHKuemTA==", "dev": true }, "debug": { @@ -560,9 +560,9 @@ "dev": true }, "end-of-stream": { - "version": "1.4.1", - "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.1.tgz", - "integrity": "sha512-1MkrZNvWTKCaigbn+W15elq2BB/L22nqrSY5DKlo3X6+vclJm8Bb5djXJBmEX6fS3+zCh/F4VBK5Z2KxJt4s2Q==", + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", + "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", "dev": true, "requires": { "once": "^1.4.0" @@ -1402,9 +1402,9 @@ } }, "graceful-fs": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.0.tgz", - "integrity": "sha512-jpSvDPV4Cq/bgtpndIWbI5hmYxhQGHPC4d4cqBPb4DLniCfhJokdXhwhaDuLBGLQdvvRum/UiX6ECVIPvDXqdg==", + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.3.tgz", + "integrity": "sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==", "dev": true }, "has-flag": { @@ -1446,9 +1446,9 @@ } }, "hosted-git-info": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.7.1.tgz", - "integrity": "sha512-7T/BxH19zbcCTa8XkMlbK5lTo1WtgkFi3GvdWEyNuc4Vex7/9Dqbnpsf4JMydcfj9HCg4zUWFTL3Za6lapg5/w==", + "version": "2.8.5", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.5.tgz", + "integrity": "sha512-kssjab8CvdXfcXMXVcvsXum4Hwdq9XGtRD3TteMEvEbq0LXyiNQr6AprqKqfeaDXze7SxWvRxdpwE6ku7ikLkg==", "dev": true }, "ignore-by-default": { @@ -1670,9 +1670,9 @@ "dev": true }, "is-retry-allowed": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/is-retry-allowed/-/is-retry-allowed-1.1.0.tgz", - "integrity": "sha1-EaBgVotnM5REAz0BJaYaINVk+zQ=", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/is-retry-allowed/-/is-retry-allowed-1.2.0.tgz", + "integrity": "sha512-RUbUeKwvm3XG2VYamhJL1xFktgjvPzL0Hq8C+6yrWIswDy3BIXGqCxhxkc30N9jqK311gVU137K8Ei55/zVJRg==", "dev": true }, "is-stream": { @@ -1746,9 +1746,9 @@ } }, "lodash": { - "version": "4.17.14", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.14.tgz", - "integrity": "sha512-mmKYbW3GLuJeX+iGP+Y7Gp1AiGHGbXHCOh/jZmrawMmsE7MS4znI3RL2FsjbqOyMayHInjOeykW7PEajUk1/xw==", + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==", "dev": true }, "lowercase-keys": { @@ -1913,18 +1913,18 @@ "dev": true }, "nodemon": { - "version": "1.19.1", - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-1.19.1.tgz", - "integrity": "sha512-/DXLzd/GhiaDXXbGId5BzxP1GlsqtMGM9zTmkWrgXtSqjKmGSbLicM/oAy4FR0YWm14jCHRwnR31AHS2dYFHrg==", + "version": "1.19.4", + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-1.19.4.tgz", + "integrity": "sha512-VGPaqQBNk193lrJFotBU8nvWZPqEZY2eIzymy2jjY0fJ9qIsxA0sxQ8ATPl0gZC645gijYEc1jtZvpS8QWzJGQ==", "dev": true, "requires": { - "chokidar": "^2.1.5", - "debug": "^3.1.0", + "chokidar": "^2.1.8", + "debug": "^3.2.6", "ignore-by-default": "^1.0.1", "minimatch": "^3.0.4", - "pstree.remy": "^1.1.6", - "semver": "^5.5.0", - "supports-color": "^5.2.0", + "pstree.remy": "^1.1.7", + "semver": "^5.7.1", + "supports-color": "^5.5.0", "touch": "^3.1.0", "undefsafe": "^2.0.2", "update-notifier": "^2.5.0" @@ -2071,9 +2071,9 @@ "dev": true }, "p-limit": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.0.tgz", - "integrity": "sha512-pZbTJpoUsCzV48Mc9Nh51VbwO0X9cuPFE8gYwx9BTCt9SF8/b7Zljd2fVgOxhIF/HDTKgpVzs+GPhyKfjLLFRQ==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-2.2.1.tgz", + "integrity": "sha512-85Tk+90UCVWvbDavCLKPOLC9vvY8OwEX/RtKF+/1OADJMVlFfEHOiMTPVyxg7mk/dKa+ipdHm0OUkTvCpMTuwg==", "dev": true, "requires": { "p-try": "^2.0.0" @@ -2313,9 +2313,9 @@ "dev": true }, "resolve": { - "version": "1.11.1", - "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.11.1.tgz", - "integrity": "sha512-vIpgF6wfuJOZI7KKKSP+HmiKggadPQAdsp5HiC1mvqnfp0gF1vdwgBWZIdrVft9pgqoMFQN+R7BSWZiBxx+BBw==", + "version": "1.12.0", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.0.tgz", + "integrity": "sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==", "dev": true, "requires": { "path-parse": "^1.0.6" @@ -2334,9 +2334,9 @@ "dev": true }, "rxjs": { - "version": "6.5.2", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.2.tgz", - "integrity": "sha512-HUb7j3kvb7p7eCUHE3FqjoDsC1xfZQ4AHFWfTKSpZ+sAhhz5X1WX0ZuUqWbzB2QhSLp3DoLUG+hMdEDKqWo2Zg==", + "version": "6.5.3", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.3.tgz", + "integrity": "sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==", "dev": true, "requires": { "tslib": "^1.9.0" @@ -2358,9 +2358,9 @@ } }, "semver": { - "version": "5.7.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", - "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", "dev": true }, "semver-diff": { @@ -2801,9 +2801,9 @@ "dev": true }, "typescript": { - "version": "3.5.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.5.3.tgz", - "integrity": "sha512-ACzBtm/PhXBDId6a6sDJfroT2pOWt/oOnk4/dElG5G33ZL776N3Y6/6bKZJBFpd+b05F3Ct9qDjMeJmRWtE2/g==", + "version": "3.7.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.2.tgz", + "integrity": "sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ==", "dev": true }, "undefsafe": { @@ -2894,9 +2894,9 @@ "dev": true }, "upath": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/upath/-/upath-1.1.2.tgz", - "integrity": "sha512-kXpym8nmDmlCBr7nKdIx8P2jNBa+pBpIUFRnKJ4dr8htyYGJFokkr2ZvERRtUN+9SY+JqXouNgUPtv6JQva/2Q==", + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/upath/-/upath-1.2.0.tgz", + "integrity": "sha512-aZwGpamFO61g3OlfT7OQCHqhGnW43ieH9WZeP7QxN/G/jS4jfqUkZxoryvJgVPEcrl5NL/ggHsSmLMHuH64Lhg==", "dev": true }, "update-notifier": { diff --git a/package.json b/package.json index dca01a0..2f19e2b 100644 --- a/package.json +++ b/package.json @@ -25,11 +25,11 @@ "readme.md" ], "devDependencies": { - "concurrently": "^4.1.1", - "nodemon": "^1.19.1", - "typescript": "^3.5.3" + "concurrently": "^5.0.0", + "nodemon": "^1.19.4", + "typescript": "^3.7.2" }, "dependencies": { - "@hibas123/utils": "^2.1.0" + "@hibas123/utils": "^2.1.1" } } \ No newline at end of file diff --git a/src/base.ts b/src/base.ts index b7d0d1e..1a04133 100644 --- a/src/base.ts +++ b/src/base.ts @@ -157,26 +157,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: @@ -193,12 +180,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, @@ -216,18 +198,56 @@ export class LoggingBase { } a("]: "); - let formatted: FormattedLine[] = lines.map(line => [...prefix, { - text: line, - formats: [] - }]); + let raw: string[] = []; + + const formatted: FormattedLine[] = []; + let line: FormattedLine; + const newLine = () => { + if (line && line.length > 0) { + formatted.push(line); + raw.push(line.map(e => e.text).join("")); + } + 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 msg: Message = { date: new Date(), file, name: this._name, text: { - raw: lines, + raw, formatted }, type @@ -237,6 +257,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 = (Error).prepareStackTrace; diff --git a/src/index.ts b/src/index.ts index e129a5f..385ae5b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, diff --git a/src/test.ts b/src/test.ts index e8a3a53..64c2b62 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,5 +1,4 @@ -import { Logging, LoggingBase } from "."; -import { LoggingTypes } from "./types"; +import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from "."; Logging.log("test") Logging.log("i", "am", { a: "an" }, 1000); @@ -9,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) diff --git a/src/types.ts b/src/types.ts index 3c1f91d..a585a28 100644 --- a/src/types.ts +++ b/src/types.ts @@ -43,7 +43,6 @@ export enum FormatTypes { BLINK } - export enum Colors { NONE, RED,