Compare commits
35 Commits
0573b45429
...
master
Author | SHA1 | Date | |
---|---|---|---|
3de5f368ef | |||
e0b51625d8 | |||
153aca0ccb | |||
7ca0c4fd72 | |||
8e183ac1a5 | |||
feed4626e6 | |||
9d4e521619 | |||
7d75f65dd3 | |||
eeed068ddd | |||
6daf815ea8 | |||
357b98c69a | |||
96d7808f35 | |||
176d37249d | |||
bcff79fc90 | |||
b92caf6468 | |||
f34800d725 | |||
1fd8da459b | |||
4420fb13ea | |||
511bdf127f | |||
7ae1c3e16e | |||
ca896c1c34 | |||
30f5e241ae | |||
a1cd860688 | |||
f51f4e2aad | |||
fa7a168f17 | |||
2eb9356a9d | |||
9182efe7e7 | |||
94d9731cdd | |||
b647b8fae6 | |||
facb7e7b40 | |||
ed35aa17f2 | |||
cedfe2cced | |||
ce9742a20e | |||
675ec50139 | |||
d2da8da690 |
@ -1,3 +1,4 @@
|
|||||||
|
[*]
|
||||||
charset = utf-8
|
charset = utf-8
|
||||||
indent_style = space
|
indent_style = space
|
||||||
indent_size = 3
|
indent_size = 3
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,4 +2,4 @@ node_modules/
|
|||||||
logs/
|
logs/
|
||||||
yarn.lock
|
yarn.lock
|
||||||
out/
|
out/
|
||||||
.history/
|
esm/
|
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"type": "pwa-node",
|
||||||
|
"request": "launch",
|
||||||
|
"name": "Launch Program",
|
||||||
|
"skipFiles": ["<node_internals>/**"],
|
||||||
|
"program": "${workspaceFolder}/out/test.js",
|
||||||
|
"preLaunchTask": "tsc: build - tsconfig.json",
|
||||||
|
"outFiles": ["${workspaceFolder}/out/**/*.js"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@ -1,29 +1,29 @@
|
|||||||
|
# Logging
|
||||||
|
|
||||||
Simple logging module, that supports terminal coloring and different plugins
|
Simple logging module, that supports terminal coloring and different plugins
|
||||||
|
|
||||||
# Getting Started
|
## Getting Started
|
||||||
|
|
||||||
``` javascript
|
```javascript
|
||||||
|
const Logging = require("@hibas123/logging").default;
|
||||||
const Logging = require("@hibas123/logging").Logging;
|
|
||||||
|
|
||||||
Logging.log("Hello there");
|
Logging.log("Hello there");
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
There are different Logging levels, that also apply terminal coloring:
|
There are different Logging levels, that also apply terminal coloring:
|
||||||
|
|
||||||
``` javascript
|
```javascript
|
||||||
Logging.debug("Debug message")
|
Logging.debug("Debug message");
|
||||||
Logging.log("Log message")
|
Logging.log("Log message");
|
||||||
Logging.warning("Warning")
|
Logging.warning("Warning");
|
||||||
Logging.error(new Error("To less creativity"))
|
Logging.error(new Error("To less creativity"));
|
||||||
Logging.error("Just an simple message as error")
|
Logging.error("Just an simple message as error");
|
||||||
Logging.errorMessage("Nearly the same as error")
|
Logging.errorMessage("Nearly the same as error");
|
||||||
```
|
```
|
||||||
|
|
||||||
All Logging types except the simple error take as many arguments as you want. These will be joined with spaces and serialized with the node util.inspect function.
|
All Logging types except the simple error take as many arguments as you want. These will be joined with spaces and serialized with the node util.inspect function.
|
||||||
|
|
||||||
# Setup
|
## Setup
|
||||||
|
|
||||||
This logging module doesn't require any setup per default, but sometimes it makes sense to configure a few things.
|
This logging module doesn't require any setup per default, but sometimes it makes sense to configure a few things.
|
||||||
|
|
||||||
@ -31,57 +31,66 @@ For example can you disable the console output. This may be helpful, if you inse
|
|||||||
|
|
||||||
Also you can set a name. All messages that are send with this instance are prefixed by this name.
|
Also you can set a name. All messages that are send with this instance are prefixed by this name.
|
||||||
|
|
||||||
``` javascript
|
```javascript
|
||||||
const CustomLogging = new LoggingBase(name | {
|
const CustomLogging = new LoggingBase(
|
||||||
name: "custom", // default undefined
|
name |
|
||||||
console: false // default true
|
{
|
||||||
});
|
name: "custom", // default undefined
|
||||||
|
console: false, // default true
|
||||||
|
}
|
||||||
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Plugins
|
||||||
# Plugins
|
|
||||||
|
|
||||||
There is a Plugin API available, that makes is possible to add custom Logging Adapter.
|
There is a Plugin API available, that makes is possible to add custom Logging Adapter.
|
||||||
|
|
||||||
``` javascript
|
```javascript
|
||||||
const Demo = new LoggingBase("Demo");
|
const Demo = new LoggingBase("Demo");
|
||||||
Demo.addAdapter(new DemoAdapter({ color: "rainbow" }));
|
Demo.addAdapter(new DemoAdapter({ color: "rainbow" }));
|
||||||
```
|
```
|
||||||
|
|
||||||
The adapters need to provide a very simple Interface:
|
The adapters need to provide a very simple Interface:
|
||||||
|
|
||||||
``` typescript
|
```typescript
|
||||||
interface Adapter {
|
interface Adapter {
|
||||||
init(observable: ObservableInterface<Message>, name?: string): void | Promise<void>;
|
init(): void | Promise<void>;
|
||||||
|
|
||||||
flush(sync: true): void;
|
onMessage(message: Message): void;
|
||||||
flush(sync: false): void | Promise<void>;
|
|
||||||
|
flush(sync: true): void;
|
||||||
|
flush(sync: false): void | Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Message {
|
interface Message {
|
||||||
type: LoggingTypes;
|
type: LoggingTypes;
|
||||||
name?:string;
|
names?: string[];
|
||||||
text: {
|
text: IFormatted;
|
||||||
raw: string[],
|
date: Date;
|
||||||
formatted: string[]
|
file: string;
|
||||||
};
|
}
|
||||||
date: Date;
|
|
||||||
file: string;
|
export interface IFormatted {
|
||||||
|
color?: IColors;
|
||||||
|
bgcolor?: IColors;
|
||||||
|
bold?: boolean;
|
||||||
|
italic?: boolean;
|
||||||
|
blink?: boolean;
|
||||||
|
underscore?: boolean;
|
||||||
|
|
||||||
|
content: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum LoggingTypes {
|
enum LoggingTypes {
|
||||||
Log,
|
Log,
|
||||||
Warning,
|
Warning,
|
||||||
Error,
|
Error,
|
||||||
Debug
|
Debug,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The `ObservableInterface` comes from `@hibas123/utils`. It provides a very simple api for subscribing and unsubscribing from the message events.
|
## License
|
||||||
|
|
||||||
More Details on Observable [git](https://git.stamm.me/OpenServer/Utils) or [npm](https://www.npmjs.com/package/@hibas123/utils)
|
|
||||||
|
|
||||||
# License
|
|
||||||
MIT
|
MIT
|
||||||
|
|
||||||
Copyright (c) 2018 Fabian Stamm
|
Copyright (c) 2018 Fabian Stamm
|
||||||
@ -90,4 +99,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
54
benchmark.js
Normal file
54
benchmark.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
const { LoggingBase } = require("./out/index.js");
|
||||||
|
let results = {};
|
||||||
|
|
||||||
|
function benchmark(name, count, runner) {
|
||||||
|
console.profile(name);
|
||||||
|
const start = process.hrtime.bigint()
|
||||||
|
|
||||||
|
runner(count);
|
||||||
|
|
||||||
|
const diffNS = process.hrtime.bigint() - start;
|
||||||
|
const diffMS = Number(diffNS / 1000n / 1000n);
|
||||||
|
console.profileEnd(name)
|
||||||
|
|
||||||
|
results[name]= {
|
||||||
|
count,
|
||||||
|
time: diffMS,
|
||||||
|
timePerI: (diffMS / count).toFixed(4)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
benchmark("simple", 10000000, (cnt) => {
|
||||||
|
const l = new LoggingBase({
|
||||||
|
console: false
|
||||||
|
});
|
||||||
|
for (let i = 0; i < cnt; i++) {
|
||||||
|
l.log("simple log")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
benchmark("complex", 1000000, (cnt) => {
|
||||||
|
const l = new LoggingBase({
|
||||||
|
console: false
|
||||||
|
});
|
||||||
|
for (let i = 0; i < cnt; i++) {
|
||||||
|
l.log("complex log", {
|
||||||
|
a: 1,
|
||||||
|
b: {
|
||||||
|
c:"test"
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
benchmark("very long", 10000000, (cnt) => {
|
||||||
|
const l = new LoggingBase({
|
||||||
|
console: false
|
||||||
|
});
|
||||||
|
const longText = "complex log".repeat(100);
|
||||||
|
for (let i = 0; i < cnt; i++) {
|
||||||
|
l.log(longText)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
console.table(results)
|
8
deno_build.js
Normal file
8
deno_build.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
const pkg = JSON.parse(await Deno.readTextFile("package.json"));
|
||||||
|
const meta = JSON.parse(await Deno.readTextFile("meta.json"));
|
||||||
|
|
||||||
|
meta.version = pkg.version;
|
||||||
|
|
||||||
|
await Deno.copyFile("README.md", "esm/README.md");
|
||||||
|
|
||||||
|
await Deno.writeTextFile("meta.json", JSON.stringify(meta, undefined, 3));
|
16
meta.json
Normal file
16
meta.json
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
"name": "logging",
|
||||||
|
"version": "3.1.2",
|
||||||
|
"description": "",
|
||||||
|
"author": "Fabian Stamm <dev@fabianstamm.de>",
|
||||||
|
"contributors": [],
|
||||||
|
"hooks": {
|
||||||
|
"prepublish": "deno_build.js"
|
||||||
|
},
|
||||||
|
"root": "esm",
|
||||||
|
"files": [
|
||||||
|
"esm/**/*.ts",
|
||||||
|
"esm/**/*.js",
|
||||||
|
"esm/README.md"
|
||||||
|
]
|
||||||
|
}
|
7495
package-lock.json
generated
7495
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
31
package.json
31
package.json
@ -1,16 +1,18 @@
|
|||||||
{
|
{
|
||||||
"name": "@hibas123/logging",
|
"name": "@hibas123/logging",
|
||||||
"version": "2.1.1",
|
"version": "4.0.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "out/index.js",
|
"type": "module",
|
||||||
"types": "out/index.d.ts",
|
"main": "esm/index.js",
|
||||||
|
"types": "esm/index.d.ts",
|
||||||
|
"module": "esm/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"prepublish": "tsc",
|
"prepublishOnly": "npm run build",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"watch-ts": "tsc --watch",
|
"test": "tsc && node esm/test.js",
|
||||||
"watch-js": "nodemon out/test.js",
|
"postpublish": "denreg publish",
|
||||||
"watch": "concurrently npm:watch-*",
|
"bench": "tsc && node benchmark.js",
|
||||||
"test": "node out/test.js"
|
"prof": "tsc && ndb --prof benchmark.js"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@ -20,16 +22,15 @@
|
|||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"files": [
|
"files": [
|
||||||
"src/",
|
"src/",
|
||||||
"out/",
|
"esm/",
|
||||||
"tsconfig.json",
|
"tsconfig.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
],
|
],
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"concurrently": "^5.0.0",
|
"concurrently": "^6.0.2",
|
||||||
"nodemon": "^1.19.4",
|
"ndb": "^1.1.5",
|
||||||
"typescript": "^3.7.2"
|
"nodemon": "^2.0.7",
|
||||||
},
|
"ts-node": "^9.1.1",
|
||||||
"dependencies": {
|
"typescript": "^4.2.4"
|
||||||
"@hibas123/utils": "^2.1.1"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
679
src/base.ts
679
src/base.ts
@ -1,334 +1,473 @@
|
|||||||
import { Observable } from "@hibas123/utils";
|
import { ConsoleAdapter } from "./consolewriter.js";
|
||||||
import { ConsoleAdapter } from "./consolewriter";
|
import inspect from "./inspect.js";
|
||||||
import inspect from "./inspect";
|
import {
|
||||||
import { Adapter, LoggingTypes, Message, FormatConfig, DefaultFormatConfig, Format, FormatTypes, Colors, FormattedText, FormattedLine } from "./types";
|
Adapter,
|
||||||
|
DefaultFormatConfig,
|
||||||
|
Format,
|
||||||
|
FormatConfig,
|
||||||
|
Formatted,
|
||||||
|
ILoggingInterface,
|
||||||
|
LoggingTypes,
|
||||||
|
Message,
|
||||||
|
} from "./types.js";
|
||||||
|
|
||||||
export function removeColors(text: string) {
|
const browser = typeof window !== "undefined";
|
||||||
text = text.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, "");
|
|
||||||
|
|
||||||
// let index = text.indexOf("\x1b");
|
export interface ILoggingOptions {
|
||||||
// while (index >= 0) {
|
/**
|
||||||
// text = text.substring(0, index) + text.substring(index + 5, text.length);
|
* Name will be prefixed on Console output and added to logfiles, if not specified here
|
||||||
// index = text.indexOf("\x1b");
|
*/
|
||||||
// }
|
name: string;
|
||||||
return text;
|
/**
|
||||||
|
* Prints output to console
|
||||||
|
*/
|
||||||
|
console: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables printing of calling file
|
||||||
|
*/
|
||||||
|
resolve_filename: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LoggingBaseOptions {
|
export interface INativeFunctions {
|
||||||
/**
|
startTimer(): any;
|
||||||
* Name will be prefixed on Console output and added to logfiles, if not specified here
|
endTimer(start: any): number;
|
||||||
*/
|
|
||||||
name: string,
|
|
||||||
/**
|
|
||||||
* Prints output to console
|
|
||||||
*/
|
|
||||||
console: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class LoggingBase {
|
export const DefaultNativeFunctions = {
|
||||||
|
startTimer: () => {
|
||||||
|
if (browser && window.performance && window.performance.now) {
|
||||||
|
return window.performance.now();
|
||||||
|
} else {
|
||||||
|
return Date.now();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
endTimer: (start: any) => {
|
||||||
|
if (browser && window.performance && window.performance.now) {
|
||||||
|
return window.performance.now() - start;
|
||||||
|
} else {
|
||||||
|
return Date.now() - start;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
} as INativeFunctions;
|
||||||
|
|
||||||
private _formatMap: FormatConfig = new DefaultFormatConfig();
|
const consoleAdapter = new ConsoleAdapter();
|
||||||
|
declare var process: { cwd: () => string };
|
||||||
|
const PROJECT_ROOT = typeof process !== "undefined" ? process.cwd() : undefined;
|
||||||
|
|
||||||
public set formatMap(value: FormatConfig) {
|
const InitialisedAdapters = Symbol("@hibas123/logging:initialisedAdapters");
|
||||||
this._formatMap = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export abstract class LoggingInterface implements ILoggingInterface {
|
||||||
|
#names: string[];
|
||||||
|
#timerMap = new Map<string, { name: string; start: any }>();
|
||||||
|
|
||||||
private adapter = new Set<Adapter>();
|
get names() {
|
||||||
private adapter_init: Promise<void>[] = [];
|
return [...this.#names];
|
||||||
|
}
|
||||||
|
|
||||||
private messageObservable = new Observable<Message>();
|
protected abstract message(
|
||||||
protected _name: string;
|
type: LoggingTypes,
|
||||||
|
names: string[],
|
||||||
|
message: any[],
|
||||||
|
caller?: { file: string; line: number; column?: number }
|
||||||
|
): void;
|
||||||
|
|
||||||
|
constructor(names: string[]) {
|
||||||
|
this.#names = names;
|
||||||
|
|
||||||
private _logLevel = LoggingTypes.Debug;
|
for (const key in this) {
|
||||||
|
if (typeof this[key] === "function") {
|
||||||
|
this[key] = (this[key] as never as Function).bind(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
get logLevel() {
|
debug(...message: any[]) {
|
||||||
return this._logLevel;
|
this.message(LoggingTypes.Debug, this.#names, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
set logLevel(value: LoggingTypes) {
|
log(...message: any[]) {
|
||||||
this._logLevel = value;
|
this.message(LoggingTypes.Log, this.#names, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
get name() {
|
warning(...message: any[]) {
|
||||||
return this._name;
|
this.message(LoggingTypes.Warn, this.#names, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(options?: Partial<LoggingBaseOptions> | string) {
|
warn(...message: any[]) {
|
||||||
let opt: Partial<LoggingBaseOptions>;
|
this.message(LoggingTypes.Warn, this.#names, message);
|
||||||
if (!options) opt = {}
|
}
|
||||||
else if (typeof options === "string") {
|
|
||||||
opt = { name: options };
|
|
||||||
} else {
|
|
||||||
opt = options;
|
|
||||||
}
|
|
||||||
|
|
||||||
let config: LoggingBaseOptions = {
|
error(error: Error | string, ...message: any[]) {
|
||||||
name: undefined,
|
if (!error)
|
||||||
console: true,
|
error = "Empty ERROR was passed, so no informations available";
|
||||||
...opt
|
if (typeof error === "string") {
|
||||||
};
|
let e = new Error("This is a fake error, to get a stack trace");
|
||||||
|
this.message(LoggingTypes.Error, this.#names, [
|
||||||
|
error,
|
||||||
|
...message,
|
||||||
|
"\n",
|
||||||
|
e.stack,
|
||||||
|
"\n",
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
this.message(
|
||||||
|
LoggingTypes.Error,
|
||||||
|
this.#names,
|
||||||
|
[error.message, "\n", error.stack, "\n", ...message],
|
||||||
|
getCallerFromExisting(error)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (config.name)
|
time(id?: string) {
|
||||||
this._name = config.name;
|
if (!id) id = Math.floor(Math.random() * 899999 + 100000).toString();
|
||||||
|
|
||||||
for (let key in this) {
|
this.#timerMap.set(id, {
|
||||||
if (typeof this[key] === "function") this[key] = (<any>this[key]).bind(this);
|
name: id,
|
||||||
}
|
start: LoggingBase.nativeFunctions.startTimer(),
|
||||||
|
});
|
||||||
|
|
||||||
if (config.console) {
|
return {
|
||||||
this.addAdapter(new ConsoleAdapter());
|
id,
|
||||||
}
|
end: () => this.timeEnd(id),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
timeEnd(id: string) {
|
||||||
|
let timer = this.#timerMap.get(id);
|
||||||
|
if (timer) {
|
||||||
|
let diff = LoggingBase.nativeFunctions.endTimer(timer.start);
|
||||||
|
|
||||||
//Binding function to this
|
this.message(LoggingTypes.Debug, this.#names, [
|
||||||
this.debug = this.debug.bind(this);
|
Format.green(`[${timer.name}]`),
|
||||||
this.log = this.log.bind(this);
|
`->`,
|
||||||
this.warn = this.warn.bind(this);
|
Format.blue(diff.toFixed(4)),
|
||||||
this.warning = this.warning.bind(this);
|
"ms",
|
||||||
this.error = this.error.bind(this);
|
]);
|
||||||
this.errorMessage = this.errorMessage.bind(this);
|
|
||||||
this.flush = this.flush.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
addAdapter(adapter: Adapter) {
|
return diff;
|
||||||
if (!this.adapter.has(adapter)) {
|
}
|
||||||
this.adapter.add(adapter);
|
return -1;
|
||||||
let prms = Promise.resolve(adapter.init(this.messageObservable.getPublicApi(), this._name));
|
}
|
||||||
this.adapter_init.push(prms);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
flush(sync: true): void;
|
abstract getChild(name: string): ILoggingInterface;
|
||||||
flush(sync: false): Promise<void>;
|
}
|
||||||
flush(sync: boolean): void | Promise<void> {
|
|
||||||
if (sync) {
|
|
||||||
this.adapter.forEach(elm => elm.flush(true));
|
|
||||||
} else {
|
|
||||||
let adapters: (void | Promise<void>)[] = [];
|
|
||||||
this.adapter.forEach(elm => adapters.push(elm.flush(false)));
|
|
||||||
return Promise.all(adapters).then(() => { });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public close() {
|
export class LoggingBase extends LoggingInterface {
|
||||||
this.adapter.forEach(adapter => adapter.close ? adapter.close() : undefined);
|
private static [InitialisedAdapters] = new Map<Adapter, number>();
|
||||||
}
|
public static nativeFunctions: INativeFunctions = DefaultNativeFunctions;
|
||||||
|
|
||||||
public waitForSetup() {
|
static DecoupledLogging = class extends LoggingInterface {
|
||||||
return Promise.all(this.adapter_init);
|
#lg: LoggingBase;
|
||||||
}
|
constructor(names: string[], lg: LoggingBase) {
|
||||||
|
super(names);
|
||||||
|
this.#lg = lg;
|
||||||
|
}
|
||||||
|
|
||||||
debug(...message: any[]) {
|
message(...params: [any, any, any, any]) {
|
||||||
if (this._logLevel <= LoggingTypes.Debug)
|
this.#lg.message(...params);
|
||||||
this.message(LoggingTypes.Debug, message);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
log(...message: any[]) {
|
getChild(name: string) {
|
||||||
if (this._logLevel <= LoggingTypes.Log)
|
return new LoggingBase.DecoupledLogging(
|
||||||
this.message(LoggingTypes.Log, message);
|
[...this.names, name],
|
||||||
}
|
this.#lg
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
warning(...message: any[]) {
|
#closed = false;
|
||||||
if (this._logLevel <= LoggingTypes.Warning)
|
#logLevel = LoggingTypes.Debug;
|
||||||
this.message(LoggingTypes.Warning, message);
|
#resolve_filename: boolean;
|
||||||
}
|
#format_map: FormatConfig = new DefaultFormatConfig();
|
||||||
|
#adapters = new Set<Adapter>();
|
||||||
|
|
||||||
warn(...message: any[]) {
|
set logLevel(level: LoggingTypes) {
|
||||||
if (this._logLevel <= LoggingTypes.Warning)
|
this.#logLevel = level;
|
||||||
this.message(LoggingTypes.Warning, message);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
error(error: Error | string) {
|
get logLevel() {
|
||||||
if (this._logLevel > LoggingTypes.Error)
|
return this.#logLevel;
|
||||||
return;
|
}
|
||||||
if (!error) error = "Empty ERROR was passed, so no informations available";
|
|
||||||
if (typeof error === "string") {
|
|
||||||
let e = new Error("This is a fake error, to get a stack trace");
|
|
||||||
this.message(LoggingTypes.Error, [error, "\n", e.stack]);
|
|
||||||
} else {
|
|
||||||
this.message(LoggingTypes.Error, [error.message, "\n", error.stack], getCallerFromExisting(error));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
errorMessage(...message: any[]) {
|
constructor(options?: Partial<ILoggingOptions>) {
|
||||||
if (this._logLevel <= LoggingTypes.Error)
|
super(options?.name ? [options.name] : []);
|
||||||
this.message(LoggingTypes.Error, message);
|
options = {
|
||||||
}
|
console: true,
|
||||||
|
resolve_filename: true,
|
||||||
|
...options,
|
||||||
|
};
|
||||||
|
|
||||||
private message(type: LoggingTypes, message: any[], caller?: { file: string, line: number }) {
|
if (options.console) {
|
||||||
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
|
this.addAdapter(consoleAdapter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let file_raw = caller || getCallerFile();
|
async addAdapter(adapter: Adapter) {
|
||||||
let file = `${file_raw.file}:${String(file_raw.line).padEnd(3, " ")}`;
|
const init = adapter.init();
|
||||||
|
|
||||||
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
|
const add = () => {
|
||||||
let type_format: Format[] = [];
|
this.#adapters.add(adapter);
|
||||||
switch (type) {
|
if (LoggingBase[InitialisedAdapters].has(adapter)) {
|
||||||
case LoggingTypes.Log:
|
LoggingBase[InitialisedAdapters].set(
|
||||||
type_format = this._formatMap.log;
|
adapter,
|
||||||
break;
|
LoggingBase[InitialisedAdapters].get(adapter) + 1
|
||||||
case LoggingTypes.Error:
|
);
|
||||||
type_format = this._formatMap.error;
|
} else {
|
||||||
break;
|
LoggingBase[InitialisedAdapters].set(adapter, 1);
|
||||||
case LoggingTypes.Debug:
|
}
|
||||||
type_format = this._formatMap.debug;
|
};
|
||||||
break;
|
|
||||||
case LoggingTypes.Warning:
|
|
||||||
type_format = this._formatMap.warning;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
const prefix: FormattedText[] = [];
|
if (!init) {
|
||||||
const a = (text: string, formats: Format[] = []) => {
|
add();
|
||||||
prefix.push({
|
} else {
|
||||||
text,
|
await Promise.resolve(init).then(add);
|
||||||
formats
|
}
|
||||||
})
|
}
|
||||||
}
|
|
||||||
|
|
||||||
a("[");
|
getChild(name: string): ILoggingInterface {
|
||||||
a(date, this._formatMap.date);
|
return new LoggingBase.DecoupledLogging([...this.names, name], this);
|
||||||
a("][");
|
}
|
||||||
a(type_str, type_format);
|
|
||||||
if (file_raw.file) {
|
|
||||||
a("][");
|
|
||||||
a(file, this._formatMap.file);
|
|
||||||
}
|
|
||||||
a("]: ");
|
|
||||||
|
|
||||||
|
protected message(
|
||||||
|
type: LoggingTypes,
|
||||||
|
names: string[],
|
||||||
|
message: any[],
|
||||||
|
caller?: { file: string; line: number; column?: number }
|
||||||
|
) {
|
||||||
|
if (this.#logLevel > type) return;
|
||||||
|
if (this.#closed) {
|
||||||
|
//TODO: Maybe error?
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let raw: string[] = [];
|
const date = new Date();
|
||||||
|
const isoStr = date.toISOString();
|
||||||
|
const date_str = isoStr.substring(0, 10) + " " + isoStr.substring(11, 19);
|
||||||
|
|
||||||
const formatted: FormattedLine[] = [];
|
let file: string | undefined = undefined;
|
||||||
let line: FormattedLine;
|
if (this.#resolve_filename) {
|
||||||
const newLine = () => {
|
let file_raw = caller;
|
||||||
if (line && line.length > 0) {
|
if (!file_raw) {
|
||||||
formatted.push(line);
|
try {
|
||||||
raw.push(line.map(e => e.text).join(""));
|
file_raw = getCallerFile();
|
||||||
|
} catch (err) {
|
||||||
|
file_raw = {
|
||||||
|
file: "<unknown>",
|
||||||
|
line: 0,
|
||||||
|
column: 0,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
line = [...prefix];
|
}
|
||||||
}
|
|
||||||
newLine();
|
|
||||||
|
|
||||||
message.forEach((e, i) => {
|
if (
|
||||||
let formats: Format[] = [];
|
PROJECT_ROOT &&
|
||||||
if (typeof e !== "string") {
|
file_raw.file &&
|
||||||
if (typeof e === "object") {
|
file_raw.file.startsWith(PROJECT_ROOT)
|
||||||
if (e[colorSymbol]) {
|
) {
|
||||||
formats.push({
|
let newF = file_raw.file.substring(PROJECT_ROOT.length);
|
||||||
type: FormatTypes.COLOR,
|
|
||||||
color: e[colorSymbol]
|
if (newF.startsWith("/") || newF.startsWith("\\"))
|
||||||
})
|
newF = newF.substring(1);
|
||||||
e = e.value;
|
|
||||||
}
|
file_raw.file = newF;
|
||||||
}
|
}
|
||||||
if (typeof e !== "string")
|
file = `${file_raw.file || "<unknown>"}:${file_raw.line}:${
|
||||||
e = inspect(e, { colors: true, showHidden: true, depth: 3 }) as string;
|
file_raw.column || 0
|
||||||
|
}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let type_str = LoggingTypes[type].toUpperCase().padEnd(5, " ");
|
||||||
|
let type_format: Formatted;
|
||||||
|
switch (type) {
|
||||||
|
case LoggingTypes.Log:
|
||||||
|
type_format = this.#format_map.log;
|
||||||
|
break;
|
||||||
|
case LoggingTypes.Error:
|
||||||
|
type_format = this.#format_map.error;
|
||||||
|
break;
|
||||||
|
case LoggingTypes.Debug:
|
||||||
|
type_format = this.#format_map.debug;
|
||||||
|
break;
|
||||||
|
case LoggingTypes.Warn:
|
||||||
|
type_format = this.#format_map.warning;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nameFormatted: Formatted<string>[] = [];
|
||||||
|
if (names.length > 0) {
|
||||||
|
nameFormatted.push(new Formatted("["));
|
||||||
|
for (let i = 0; i < names.length; i++) {
|
||||||
|
nameFormatted.push(new Formatted(names[i], this.#format_map.names));
|
||||||
|
if (i < names.length - 1) {
|
||||||
|
nameFormatted.push(this.#format_map.names_delimiter);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
nameFormatted.push(new Formatted("]"));
|
||||||
|
}
|
||||||
|
|
||||||
removeColors(e).split("\n").map((text, index, { length }) => {
|
let linePrefix = [
|
||||||
line.push({ text, formats });
|
new Formatted("["),
|
||||||
if (index < length - 1) {
|
new Formatted(date_str, this.#format_map.date),
|
||||||
newLine();
|
new Formatted("]["),
|
||||||
}
|
new Formatted(type_str, type_format),
|
||||||
})
|
...(file
|
||||||
|
? [new Formatted("]["), new Formatted(file, this.#format_map.file)]
|
||||||
|
: []),
|
||||||
|
new Formatted("]"),
|
||||||
|
...nameFormatted,
|
||||||
|
new Formatted(": "),
|
||||||
|
];
|
||||||
|
|
||||||
if (!e.endsWith("\n") && i < message.length - 1) {
|
let formattedMessage: Formatted<string>[] = [...linePrefix];
|
||||||
line.push({ text: " ", formats: [] });
|
message.forEach((msg, idx) => {
|
||||||
|
let format: Formatted;
|
||||||
|
if (msg instanceof Formatted) {
|
||||||
|
format = msg;
|
||||||
|
msg = msg.content;
|
||||||
|
} else {
|
||||||
|
format = new Formatted();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof msg !== "string") {
|
||||||
|
msg = inspect(msg, {
|
||||||
|
colors: false, //TODO: Maybe change when changing the removeColors to return formatted text?
|
||||||
|
showHidden: true,
|
||||||
|
depth: 3,
|
||||||
|
}) as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeColors(msg) // Remove colors is uncommented for now, since there are no real benefits of having it and it reduces performance
|
||||||
|
msg.split("\n").forEach((text, index, { length }) => {
|
||||||
|
if (index != length - 1) {
|
||||||
|
formattedMessage.push(
|
||||||
|
new Formatted(text + "\n", format),
|
||||||
|
...linePrefix
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
formattedMessage.push(new Formatted(text, format));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
formattedMessage.push(new Formatted(" "));
|
||||||
|
});
|
||||||
|
|
||||||
newLine();
|
let msg: Message = {
|
||||||
|
date,
|
||||||
|
file,
|
||||||
|
names,
|
||||||
|
text: formattedMessage,
|
||||||
|
type,
|
||||||
|
};
|
||||||
|
|
||||||
let msg: Message = {
|
this.#adapters.forEach((adapter) => {
|
||||||
date: new Date(),
|
if (adapter.level <= type) {
|
||||||
file,
|
adapter.onMessage(msg);
|
||||||
name: this._name,
|
}
|
||||||
text: {
|
});
|
||||||
raw,
|
}
|
||||||
formatted
|
|
||||||
},
|
|
||||||
type
|
|
||||||
}
|
|
||||||
|
|
||||||
this.messageObservable.send(msg);
|
async close() {
|
||||||
}
|
if (this.#closed) return;
|
||||||
}
|
this.#closed = true;
|
||||||
|
|
||||||
const colorSymbol = Symbol("color");
|
for (const adapter of this.#adapters) {
|
||||||
|
const cnt = LoggingBase[InitialisedAdapters].get(adapter);
|
||||||
export interface ColorFormat {
|
if (!cnt) {
|
||||||
[colorSymbol]: Colors;
|
//TODO: should not happen!
|
||||||
value: any;
|
} else {
|
||||||
}
|
if (cnt <= 1) {
|
||||||
|
if (adapter.close) await adapter.close();
|
||||||
export function withColor(color: Colors, value: any): ColorFormat {
|
LoggingBase[InitialisedAdapters].delete(adapter);
|
||||||
return {
|
} else {
|
||||||
[colorSymbol]: color,
|
LoggingBase[InitialisedAdapters].set(adapter, cnt - 1);
|
||||||
value
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStack() {
|
function getStack() {
|
||||||
// Save original Error.prepareStackTrace
|
// Save original Error.prepareStackTrace
|
||||||
let origPrepareStackTrace = (<any>Error).prepareStackTrace;
|
let origPrepareStackTrace = (<any>Error).prepareStackTrace;
|
||||||
|
try {
|
||||||
|
// Override with function that just returns `stack`
|
||||||
|
(<any>Error).prepareStackTrace = function (_, stack) {
|
||||||
|
return stack;
|
||||||
|
};
|
||||||
|
|
||||||
// Override with function that just returns `stack`
|
// Create a new `Error`, which automatically gets `stack`
|
||||||
(<any>Error).prepareStackTrace = function (_, stack) {
|
let err = new Error();
|
||||||
return stack
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new `Error`, which automatically gets `stack`
|
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
|
||||||
let err = new Error();
|
let stack: any[] = <any>err.stack;
|
||||||
|
|
||||||
// Evaluate `err.stack`, which calls our new `Error.prepareStackTrace`
|
return stack;
|
||||||
let stack: any[] = <any>err.stack;
|
} finally {
|
||||||
|
// Restore original `Error.prepareStackTrace`
|
||||||
// Restore original `Error.prepareStackTrace`
|
(<any>Error).prepareStackTrace = origPrepareStackTrace;
|
||||||
(<any>Error).prepareStackTrace = origPrepareStackTrace;
|
}
|
||||||
|
|
||||||
// Remove superfluous function call on stack
|
|
||||||
stack.shift(); // getStack --> Error
|
|
||||||
|
|
||||||
return stack
|
|
||||||
}
|
|
||||||
|
|
||||||
function baseName(path) {
|
|
||||||
return path.split(/[\\/]/).pop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCallerFile() {
|
function getCallerFile() {
|
||||||
try {
|
try {
|
||||||
let stack = getStack()
|
let stack = getStack();
|
||||||
|
|
||||||
let current_file = stack.shift().getFileName();
|
let current_file = stack.shift().getFileName();
|
||||||
|
|
||||||
while (stack.length) {
|
while (stack.length) {
|
||||||
let caller_file = stack.shift();
|
let caller_file = stack.shift();
|
||||||
if (current_file !== caller_file.getFileName())
|
if (current_file !== caller_file.getFileName()) {
|
||||||
return {
|
return {
|
||||||
file: baseName(caller_file.getFileName()),
|
file: caller_file.getFileName(),
|
||||||
line: caller_file.getLineNumber()
|
line: caller_file.getLineNumber(),
|
||||||
};
|
column: caller_file.getColumnNumber(),
|
||||||
}
|
};
|
||||||
} catch (err) { }
|
}
|
||||||
return { file: undefined, line: 0 };
|
}
|
||||||
|
} catch (err) {}
|
||||||
|
return { file: undefined, line: 0 };
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCallerFromExisting(err: Error): { file: string, line: number } {
|
function getCallerFromExisting(err: Error): {
|
||||||
if (!err || !err.stack) return { file: "NOFILE", line: 0 };
|
file: string;
|
||||||
let lines = err.stack.split("\n");
|
line: number;
|
||||||
lines.shift();// removing first line
|
column?: number;
|
||||||
while (lines.length > 0) {
|
} {
|
||||||
let line = lines.shift();
|
if (!err || !err.stack) return { file: "NOFILE", line: 0 };
|
||||||
let matches = line.match(/[a-zA-Z_-]+[.][a-zA-Z_-]+[:][0-9]+/g)
|
let lines = err.stack.split("\n");
|
||||||
if (matches && matches.length > 0) {
|
lines.shift(); // removing first line
|
||||||
let [f, line] = matches[0].split(":")
|
while (lines.length > 0) {
|
||||||
return {
|
let line = lines.shift();
|
||||||
file: f, line: Number(line)
|
let matches = line.match(
|
||||||
};
|
/[<]?([a-zA-Z]:)?([\/\\]?[a-zA-Z_-])+[.>][a-zA-Z_-]*([:][0-9]+)+/g
|
||||||
}
|
);
|
||||||
}
|
if (matches && matches.length > 0) {
|
||||||
}
|
let match = matches[0].trim();
|
||||||
|
let locationString = match.match(/([:][0-9]+)+$/gm)[0];
|
||||||
|
let line: number;
|
||||||
|
let column: number;
|
||||||
|
if (locationString) {
|
||||||
|
match = match.slice(0, match.length - locationString.length);
|
||||||
|
locationString = locationString.substring(1);
|
||||||
|
[line, column] = locationString.split(":").map(Number);
|
||||||
|
}
|
||||||
|
let file = match;
|
||||||
|
return {
|
||||||
|
file,
|
||||||
|
line,
|
||||||
|
column,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function removeColors(text: string) {
|
||||||
|
text = text.replace(
|
||||||
|
// Putting regex here directly instead of externally actually improves performance. The cause of that is not clear for now.
|
||||||
|
/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
@ -1,16 +1,30 @@
|
|||||||
import { ObservableInterface } from "@hibas123/utils";
|
import { Colors } from "./index.js";
|
||||||
import { Colors } from "./index";
|
import {
|
||||||
import { Adapter, Message, FormattedLine, TerminalFormats, FormatTypes } from "./types";
|
Adapter,
|
||||||
|
Message,
|
||||||
|
TerminalFormats,
|
||||||
|
Formatted,
|
||||||
|
IFormatted,
|
||||||
|
LoggingTypes,
|
||||||
|
} from "./types.js";
|
||||||
|
|
||||||
const browser = typeof window !== "undefined";
|
declare const Deno: any;
|
||||||
|
declare const process: any;
|
||||||
|
|
||||||
|
const browser = typeof window !== "undefined" && typeof Deno === "undefined";
|
||||||
|
const deno = typeof Deno !== "undefined";
|
||||||
|
const NodeJS = typeof process !== undefined;
|
||||||
|
|
||||||
export class ConsoleAdapter implements Adapter {
|
export class ConsoleAdapter implements Adapter {
|
||||||
init(observable: ObservableInterface<Message>) {
|
level: LoggingTypes = LoggingTypes.Debug;
|
||||||
observable.subscribe(this.onMessage.bind(this));
|
constructor(private colors: boolean = true) {}
|
||||||
|
|
||||||
|
init() {}
|
||||||
|
flush() {}
|
||||||
|
|
||||||
|
setLevel(level: LoggingTypes) {
|
||||||
|
this.level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
flush() { }
|
|
||||||
|
|
||||||
// TODO: Check if required!
|
// TODO: Check if required!
|
||||||
// private escape(text: string): string {
|
// private escape(text: string): string {
|
||||||
// return text
|
// return text
|
||||||
@ -18,102 +32,161 @@ export class ConsoleAdapter implements Adapter {
|
|||||||
// .replace(/%c/g, "%%c")
|
// .replace(/%c/g, "%%c")
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private formatLine(line: FormattedLine): [string, string[] | undefined] {
|
private format(
|
||||||
|
formatted: IFormatted<string>[]
|
||||||
|
): [string, string[] | undefined] {
|
||||||
let text = "";
|
let text = "";
|
||||||
let style_formats: string[] = [];
|
let style_formats: string[] = [];
|
||||||
|
|
||||||
if (!browser) {
|
if (!browser) {
|
||||||
for (let part of line) {
|
// NodeJS or Deno
|
||||||
|
for (const format of formatted) {
|
||||||
let formats = "";
|
let formats = "";
|
||||||
for (let format of part.formats) {
|
if (format.bold) {
|
||||||
switch (format.type) {
|
formats += TerminalFormats.Bold;
|
||||||
case FormatTypes.BOLD:
|
}
|
||||||
formats += TerminalFormats.Bold;
|
|
||||||
|
if (format.blink) {
|
||||||
|
formats += TerminalFormats.Blink;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (format.underscore) {
|
||||||
|
formats += TerminalFormats.Underscore;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (format.color) {
|
||||||
|
switch (format.color) {
|
||||||
|
case Colors.RED:
|
||||||
|
formats += TerminalFormats.FgRed;
|
||||||
break;
|
break;
|
||||||
case FormatTypes.UNDERSCORE:
|
case Colors.GREEN:
|
||||||
formats += TerminalFormats.Underscore;
|
formats += TerminalFormats.FgGreen;
|
||||||
break;
|
break;
|
||||||
case FormatTypes.BLINK:
|
case Colors.YELLOW:
|
||||||
formats += TerminalFormats.Blink;
|
formats += TerminalFormats.FgYellow;
|
||||||
break;
|
break;
|
||||||
case FormatTypes.COLOR:
|
case Colors.BLUE:
|
||||||
switch (format.color) {
|
formats += TerminalFormats.FgBlue;
|
||||||
case Colors.RED:
|
break;
|
||||||
formats += TerminalFormats.FgRed;
|
case Colors.MAGENTA:
|
||||||
break;
|
formats += TerminalFormats.FgMagenta;
|
||||||
case Colors.GREEN:
|
break;
|
||||||
formats += TerminalFormats.FgGreen;
|
case Colors.CYAN:
|
||||||
break;
|
formats += TerminalFormats.FgCyan;
|
||||||
case Colors.YELLOW:
|
break;
|
||||||
formats += TerminalFormats.FgYellow;
|
case Colors.WHITE:
|
||||||
break;
|
formats += TerminalFormats.FgWhite;
|
||||||
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
text += formats + part.text + TerminalFormats.Reset;
|
|
||||||
|
if (format.bgcolor) {
|
||||||
|
switch (format.bgcolor) {
|
||||||
|
case Colors.RED:
|
||||||
|
formats += TerminalFormats.BgRed;
|
||||||
|
break;
|
||||||
|
case Colors.GREEN:
|
||||||
|
formats += TerminalFormats.BgGreen;
|
||||||
|
break;
|
||||||
|
case Colors.YELLOW:
|
||||||
|
formats += TerminalFormats.BgYellow;
|
||||||
|
break;
|
||||||
|
case Colors.BLUE:
|
||||||
|
formats += TerminalFormats.BgBlue;
|
||||||
|
break;
|
||||||
|
case Colors.MAGENTA:
|
||||||
|
formats += TerminalFormats.BgMagenta;
|
||||||
|
break;
|
||||||
|
case Colors.CYAN:
|
||||||
|
formats += TerminalFormats.BgCyan;
|
||||||
|
break;
|
||||||
|
case Colors.WHITE:
|
||||||
|
formats += TerminalFormats.BgWhite;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
text += formats + format.content + TerminalFormats.Reset;
|
||||||
|
// (formats.length > 0 ? TerminalFormats.Reset : ""); //TODO: Benchmark if this is better
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (let part of line) {
|
for (const format of formatted) {
|
||||||
let styles: string[] = [];
|
let styles: string[] = [];
|
||||||
let resetStyles: string[] = [];
|
let resetStyles: string[] = [];
|
||||||
for (let format of part.formats) {
|
if (format.bold) {
|
||||||
switch (format.type) {
|
styles.push("font-weight: bold;");
|
||||||
case FormatTypes.BOLD:
|
resetStyles.push("font-weight: unset");
|
||||||
styles.push("font-weight: bold;");
|
}
|
||||||
resetStyles.push("font-weight: unset");
|
|
||||||
|
if (format.underscore) {
|
||||||
|
styles.push("text-decoration: underline");
|
||||||
|
resetStyles.push("text-decoration: unset");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (format.blink) {
|
||||||
|
styles.push("text-decoration: blink");
|
||||||
|
resetStyles.push("text-decoration: unset");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (format.color) {
|
||||||
|
let color = "";
|
||||||
|
switch (format.color) {
|
||||||
|
case Colors.RED:
|
||||||
|
color = "red";
|
||||||
break;
|
break;
|
||||||
case FormatTypes.UNDERSCORE:
|
case Colors.GREEN:
|
||||||
styles.push("text-decoration: underline");
|
color = "green";
|
||||||
resetStyles.push("text-decoration: unset");
|
|
||||||
break;
|
break;
|
||||||
case FormatTypes.BLINK:
|
case Colors.YELLOW:
|
||||||
styles.push("text-decoration: blink");
|
color = "gold";
|
||||||
resetStyles.push("text-decoration: unset");
|
|
||||||
break;
|
break;
|
||||||
case FormatTypes.COLOR:
|
case Colors.BLUE:
|
||||||
let color = "";
|
color = "blue";
|
||||||
switch (format.color) {
|
break;
|
||||||
case Colors.RED:
|
case Colors.MAGENTA:
|
||||||
color = "red";
|
color = "magenta";
|
||||||
break;
|
break;
|
||||||
case Colors.GREEN:
|
case Colors.CYAN:
|
||||||
color = "green";
|
color = "cyan";
|
||||||
break;
|
break;
|
||||||
case Colors.YELLOW:
|
case Colors.WHITE:
|
||||||
color = "gold";
|
color = "white";
|
||||||
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;
|
break;
|
||||||
}
|
}
|
||||||
|
styles.push("color: " + color);
|
||||||
|
resetStyles.push("color: unset");
|
||||||
}
|
}
|
||||||
text += "%c" + part.text.replace(/%c/g, "%%c") + "%c";
|
|
||||||
|
if (format.bgcolor) {
|
||||||
|
let color = "";
|
||||||
|
switch (format.bgcolor) {
|
||||||
|
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("background-color: " + color);
|
||||||
|
resetStyles.push("background-color: unset");
|
||||||
|
}
|
||||||
|
|
||||||
|
text += "%c" + format.content.replace(/%c/g, "%%c") + "%c";
|
||||||
style_formats.push(styles.join(";"), resetStyles.join(";"));
|
style_formats.push(styles.join(";"), resetStyles.join(";"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,26 +195,27 @@ export class ConsoleAdapter implements Adapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMessage(message: Message) {
|
onMessage(message: Message) {
|
||||||
let lines = message.text.formatted;
|
|
||||||
|
|
||||||
let prefix = "";
|
|
||||||
if (message.name) prefix = `[${message.name}]=>`;
|
|
||||||
|
|
||||||
if (browser) {
|
if (browser) {
|
||||||
|
if (this.colors) {
|
||||||
let formats: string[] = [];
|
const [text, formats] = this.format(message.text);
|
||||||
let text = lines.map(line => {
|
console.log(text, ...formats);
|
||||||
let [t, fmts] = this.formatLine(line);
|
} else {
|
||||||
formats.push(...fmts);
|
console.log(Formatted.strip(message.text));
|
||||||
return t;
|
}
|
||||||
}).join("\n");
|
|
||||||
// console.log(formats);
|
|
||||||
console.log(text, ...formats);
|
|
||||||
} else {
|
} else {
|
||||||
lines.forEach(line => {
|
const text = this.colors
|
||||||
let [text] = this.formatLine(line);
|
? this.format(message.text)[0]
|
||||||
console.log(prefix + text);
|
: Formatted.strip(message.text);
|
||||||
})
|
|
||||||
|
if (deno) {
|
||||||
|
//TODO: Deno specific thing
|
||||||
|
console.log(text);
|
||||||
|
} else if (typeof process !== "undefined") {
|
||||||
|
//NodeJS
|
||||||
|
process.stdout.write(text + "\n");
|
||||||
|
} else {
|
||||||
|
console.log(text);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
37
src/index.ts
37
src/index.ts
@ -1,22 +1,23 @@
|
|||||||
import { LoggingBase } from "./base";
|
import { LoggingBase } from "./base.js";
|
||||||
export { ConsoleAdapter } from "./consolewriter";
|
export { LoggingBase } from "./base.js";
|
||||||
export { LoggingBase, LoggingBaseOptions, removeColors, withColor } from "./base";
|
export { ConsoleAdapter } from "./consolewriter.js";
|
||||||
|
export { ILoggingOptions, INativeFunctions } from "./base.js";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
Adapter,
|
Adapter,
|
||||||
LoggingTypes,
|
LoggingTypes,
|
||||||
Message,
|
Message,
|
||||||
FormatConfig,
|
FormatConfig,
|
||||||
FormattedLine,
|
DefaultFormatConfig as DefaultColorMap,
|
||||||
DefaultFormatConfig as DefaultColorMap,
|
IColors as Colors,
|
||||||
FormattedText,
|
Format,
|
||||||
Colors,
|
TerminalFormats,
|
||||||
Format,
|
Formatted,
|
||||||
FormatTypes,
|
IFormatted,
|
||||||
TerminalFormats
|
ILoggingInterface,
|
||||||
} from "./types";
|
ILoggingTimer,
|
||||||
|
} from "./types.js";
|
||||||
|
|
||||||
export { ObservableInterface } from "@hibas123/utils";
|
const Logging = new LoggingBase();
|
||||||
|
|
||||||
export let Logging: LoggingBase = undefined;
|
|
||||||
Logging = new LoggingBase();
|
|
||||||
export default Logging;
|
export default Logging;
|
||||||
|
636
src/inspect.ts
636
src/inspect.ts
@ -1,12 +1,11 @@
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Module exports.
|
* Module exports.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
interface InspectOptions {
|
interface InspectOptions {
|
||||||
depth: number;
|
depth: number;
|
||||||
colors: boolean;
|
colors: boolean;
|
||||||
showHidden: boolean;
|
showHidden: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,397 +18,440 @@ interface InspectOptions {
|
|||||||
*/
|
*/
|
||||||
/* legacy: obj, showHidden, depth, colors*/
|
/* legacy: obj, showHidden, depth, colors*/
|
||||||
export default function inspect(obj: any, opts: Partial<InspectOptions>) {
|
export default function inspect(obj: any, opts: Partial<InspectOptions>) {
|
||||||
// default options
|
// default options
|
||||||
let ctx = {
|
let ctx = {
|
||||||
seen: [],
|
seen: [],
|
||||||
stylize: stylizeNoColor,
|
stylize: stylizeNoColor,
|
||||||
depth: undefined,
|
depth: undefined,
|
||||||
colors: undefined,
|
colors: undefined,
|
||||||
showHidden: undefined,
|
showHidden: undefined,
|
||||||
customInspect: undefined
|
customInspect: undefined,
|
||||||
};
|
};
|
||||||
// legacy...
|
// legacy...
|
||||||
if (arguments.length >= 3) ctx.depth = arguments[2];
|
if (arguments.length >= 3) ctx.depth = arguments[2];
|
||||||
if (arguments.length >= 4) ctx.colors = arguments[3];
|
if (arguments.length >= 4) ctx.colors = arguments[3];
|
||||||
if (isBoolean(opts)) {
|
if (isBoolean(opts)) {
|
||||||
// legacy...
|
// legacy...
|
||||||
ctx.showHidden = opts;
|
ctx.showHidden = opts;
|
||||||
} else if (opts) {
|
} else if (opts) {
|
||||||
// got an "options" object
|
// got an "options" object
|
||||||
_extend(ctx, opts);
|
_extend(ctx, opts);
|
||||||
}
|
}
|
||||||
// set default options
|
// set default options
|
||||||
if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
|
if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
|
||||||
if (isUndefined(ctx.depth)) ctx.depth = 2;
|
if (isUndefined(ctx.depth)) ctx.depth = 2;
|
||||||
if (isUndefined(ctx.colors)) ctx.colors = false;
|
if (isUndefined(ctx.colors)) ctx.colors = false;
|
||||||
if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
|
if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
|
||||||
if (ctx.colors) ctx.stylize = stylizeWithColor;
|
if (ctx.colors) ctx.stylize = stylizeWithColor;
|
||||||
return formatValue(ctx, obj, ctx.depth);
|
return formatValue(ctx, obj, ctx.depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
||||||
inspect.colors = {
|
inspect.colors = {
|
||||||
'bold': [1, 22],
|
bold: [1, 22],
|
||||||
'italic': [3, 23],
|
italic: [3, 23],
|
||||||
'underline': [4, 24],
|
underline: [4, 24],
|
||||||
'inverse': [7, 27],
|
inverse: [7, 27],
|
||||||
'white': [37, 39],
|
white: [37, 39],
|
||||||
'grey': [90, 39],
|
grey: [90, 39],
|
||||||
'black': [30, 39],
|
black: [30, 39],
|
||||||
'blue': [34, 39],
|
blue: [34, 39],
|
||||||
'cyan': [36, 39],
|
cyan: [36, 39],
|
||||||
'green': [32, 39],
|
green: [32, 39],
|
||||||
'magenta': [35, 39],
|
magenta: [35, 39],
|
||||||
'red': [31, 39],
|
red: [31, 39],
|
||||||
'yellow': [33, 39]
|
yellow: [33, 39],
|
||||||
};
|
};
|
||||||
|
|
||||||
// Don't use 'blue' not visible on cmd.exe
|
// Don't use 'blue' not visible on cmd.exe
|
||||||
inspect.styles = {
|
inspect.styles = {
|
||||||
'special': 'cyan',
|
special: "cyan",
|
||||||
'number': 'yellow',
|
number: "yellow",
|
||||||
'boolean': 'yellow',
|
boolean: "yellow",
|
||||||
'undefined': 'grey',
|
undefined: "grey",
|
||||||
'null': 'bold',
|
null: "bold",
|
||||||
'string': 'green',
|
string: "green",
|
||||||
'date': 'magenta',
|
date: "magenta",
|
||||||
// "name": intentionally not styling
|
// "name": intentionally not styling
|
||||||
'regexp': 'red'
|
regexp: "red",
|
||||||
};
|
};
|
||||||
|
|
||||||
function stylizeNoColor(str, styleType) {
|
function stylizeNoColor(str, styleType) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isBoolean(arg) {
|
function isBoolean(arg) {
|
||||||
return typeof arg === 'boolean';
|
return typeof arg === "boolean";
|
||||||
}
|
}
|
||||||
|
|
||||||
function isUndefined(arg) {
|
function isUndefined(arg) {
|
||||||
return arg === void 0;
|
return arg === void 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function stylizeWithColor(str, styleType) {
|
function stylizeWithColor(str, styleType) {
|
||||||
var style = inspect.styles[styleType];
|
var style = inspect.styles[styleType];
|
||||||
|
|
||||||
if (style) {
|
if (style) {
|
||||||
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
|
return (
|
||||||
'\u001b[' + inspect.colors[style][1] + 'm';
|
"\u001b[" +
|
||||||
} else {
|
inspect.colors[style][0] +
|
||||||
return str;
|
"m" +
|
||||||
}
|
str +
|
||||||
|
"\u001b[" +
|
||||||
|
inspect.colors[style][1] +
|
||||||
|
"m"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function isFunction(arg) {
|
function isFunction(arg) {
|
||||||
return typeof arg === 'function';
|
return typeof arg === "function";
|
||||||
}
|
}
|
||||||
|
|
||||||
function isString(arg) {
|
function isString(arg) {
|
||||||
return typeof arg === 'string';
|
return typeof arg === "string";
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNumber(arg) {
|
function isNumber(arg) {
|
||||||
return typeof arg === 'number';
|
return typeof arg === "number";
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNull(arg) {
|
function isNull(arg) {
|
||||||
return arg === null;
|
return arg === null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasOwn(obj, prop) {
|
function hasOwn(obj, prop) {
|
||||||
return Object.prototype.hasOwnProperty.call(obj, prop);
|
return Object.prototype.hasOwnProperty.call(obj, prop);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isRegExp(re) {
|
function isRegExp(re) {
|
||||||
return isObject(re) && objectToString(re) === '[object RegExp]';
|
return isObject(re) && objectToString(re) === "[object RegExp]";
|
||||||
}
|
}
|
||||||
|
|
||||||
function isObject(arg) {
|
function isObject(arg) {
|
||||||
return typeof arg === 'object' && arg !== null;
|
return typeof arg === "object" && arg !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function isError(e) {
|
function isError(e) {
|
||||||
return isObject(e) &&
|
return (
|
||||||
(objectToString(e) === '[object Error]' || e instanceof Error);
|
isObject(e) &&
|
||||||
|
(objectToString(e) === "[object Error]" || e instanceof Error)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDate(d) {
|
function isDate(d) {
|
||||||
return isObject(d) && objectToString(d) === '[object Date]';
|
return isObject(d) && objectToString(d) === "[object Date]";
|
||||||
}
|
}
|
||||||
|
|
||||||
function objectToString(o) {
|
function objectToString(o) {
|
||||||
return Object.prototype.toString.call(o);
|
return Object.prototype.toString.call(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayToHash(array) {
|
function arrayToHash(array) {
|
||||||
var hash = {};
|
var hash = {};
|
||||||
|
|
||||||
array.forEach(function (val, idx) {
|
array.forEach(function (val, idx) {
|
||||||
hash[val] = true;
|
hash[val] = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
|
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
|
||||||
var output = [];
|
var output = [];
|
||||||
for (var i = 0, l = value.length; i < l; ++i) {
|
for (var i = 0, l = value.length; i < l; ++i) {
|
||||||
if (hasOwn(value, String(i))) {
|
if (hasOwn(value, String(i))) {
|
||||||
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
output.push(
|
||||||
String(i), true));
|
formatProperty(
|
||||||
} else {
|
ctx,
|
||||||
output.push('');
|
value,
|
||||||
}
|
recurseTimes,
|
||||||
}
|
visibleKeys,
|
||||||
keys.forEach(function (key) {
|
String(i),
|
||||||
if (!key.match(/^\d+$/)) {
|
true
|
||||||
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
|
)
|
||||||
key, true));
|
);
|
||||||
}
|
} else {
|
||||||
});
|
output.push("");
|
||||||
return output;
|
}
|
||||||
|
}
|
||||||
|
keys.forEach(function (key) {
|
||||||
|
if (!key.match(/^\d+$/)) {
|
||||||
|
output.push(
|
||||||
|
formatProperty(ctx, value, recurseTimes, visibleKeys, key, true)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatError(value) {
|
function formatError(value) {
|
||||||
return '[' + Error.prototype.toString.call(value) + ']';
|
return "[" + Error.prototype.toString.call(value) + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatValue(ctx, value, recurseTimes) {
|
function formatValue(ctx, value, recurseTimes) {
|
||||||
// Provide a hook for user-specified inspect functions.
|
// Provide a hook for user-specified inspect functions.
|
||||||
// Check that value is an object with an inspect function on it
|
// Check that value is an object with an inspect function on it
|
||||||
if (ctx.customInspect &&
|
if (
|
||||||
value &&
|
ctx.customInspect &&
|
||||||
isFunction(value.inspect) &&
|
value &&
|
||||||
// Filter out the util module, it's inspect function is special
|
isFunction(value.inspect) &&
|
||||||
value.inspect !== inspect &&
|
// Filter out the util module, it's inspect function is special
|
||||||
// Also filter out any prototype objects using the circular check.
|
value.inspect !== inspect &&
|
||||||
!(value.constructor && value.constructor.prototype === value)) {
|
// Also filter out any prototype objects using the circular check.
|
||||||
var ret = value.inspect(recurseTimes, ctx);
|
!(value.constructor && value.constructor.prototype === value)
|
||||||
if (!isString(ret)) {
|
) {
|
||||||
ret = formatValue(ctx, ret, recurseTimes);
|
var ret = value.inspect(recurseTimes, ctx);
|
||||||
}
|
if (!isString(ret)) {
|
||||||
return ret;
|
ret = formatValue(ctx, ret, recurseTimes);
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
// Primitive types cannot have properties
|
// Primitive types cannot have properties
|
||||||
var primitive = formatPrimitive(ctx, value);
|
var primitive = formatPrimitive(ctx, value);
|
||||||
if (primitive) {
|
if (primitive) {
|
||||||
return primitive;
|
return primitive;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look up the keys of the object.
|
// Look up the keys of the object.
|
||||||
var keys = Object.keys(value);
|
var keys = Object.keys(value);
|
||||||
var visibleKeys = arrayToHash(keys);
|
var visibleKeys = arrayToHash(keys);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (ctx.showHidden && Object.getOwnPropertyNames) {
|
if (ctx.showHidden && Object.getOwnPropertyNames) {
|
||||||
keys = Object.getOwnPropertyNames(value);
|
keys = Object.getOwnPropertyNames(value);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
// IE doesn't make error fields non-enumerable
|
// IE doesn't make error fields non-enumerable
|
||||||
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
|
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
|
||||||
if (isError(value)
|
if (
|
||||||
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
|
isError(value) &&
|
||||||
return formatError(value);
|
(keys.indexOf("message") >= 0 || keys.indexOf("description") >= 0)
|
||||||
}
|
) {
|
||||||
|
return formatError(value);
|
||||||
|
}
|
||||||
|
|
||||||
// Some type of object without properties can be shortcutted.
|
// Some type of object without properties can be shortcutted.
|
||||||
if (keys.length === 0) {
|
if (keys.length === 0) {
|
||||||
if (isFunction(value)) {
|
if (isFunction(value)) {
|
||||||
var name = value.name ? ': ' + value.name : '';
|
var name = value.name ? ": " + value.name : "";
|
||||||
return ctx.stylize('[Function' + name + ']', 'special');
|
return ctx.stylize("[Function" + name + "]", "special");
|
||||||
}
|
}
|
||||||
if (isRegExp(value)) {
|
if (isRegExp(value)) {
|
||||||
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
|
||||||
}
|
}
|
||||||
if (isDate(value)) {
|
if (isDate(value)) {
|
||||||
return ctx.stylize(Date.prototype.toString.call(value), 'date');
|
return ctx.stylize(Date.prototype.toString.call(value), "date");
|
||||||
}
|
}
|
||||||
if (isError(value)) {
|
if (isError(value)) {
|
||||||
return formatError(value);
|
return formatError(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var base = '', array = false, braces = ['{', '}'];
|
var base = "",
|
||||||
|
array = false,
|
||||||
|
braces = ["{", "}"];
|
||||||
|
|
||||||
// Make Array say that they are Array
|
// Make Array say that they are Array
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
array = true;
|
array = true;
|
||||||
braces = ['[', ']'];
|
braces = ["[", "]"];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make functions say that they are functions
|
// Make functions say that they are functions
|
||||||
if (isFunction(value)) {
|
if (isFunction(value)) {
|
||||||
var n = value.name ? ': ' + value.name : '';
|
var n = value.name ? ": " + value.name : "";
|
||||||
base = ' [Function' + n + ']';
|
base = " [Function" + n + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make RegExps say that they are RegExps
|
// Make RegExps say that they are RegExps
|
||||||
if (isRegExp(value)) {
|
if (isRegExp(value)) {
|
||||||
base = ' ' + RegExp.prototype.toString.call(value);
|
base = " " + RegExp.prototype.toString.call(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make dates with properties first say the date
|
// Make dates with properties first say the date
|
||||||
if (isDate(value)) {
|
if (isDate(value)) {
|
||||||
base = ' ' + Date.prototype.toUTCString.call(value);
|
base = " " + Date.prototype.toUTCString.call(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make error with message first say the error
|
// Make error with message first say the error
|
||||||
if (isError(value)) {
|
if (isError(value)) {
|
||||||
base = ' ' + formatError(value);
|
base = " " + formatError(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (keys.length === 0 && (!array || value.length == 0)) {
|
if (keys.length === 0 && (!array || value.length == 0)) {
|
||||||
return braces[0] + base + braces[1];
|
return braces[0] + base + braces[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (recurseTimes < 0) {
|
if (recurseTimes < 0) {
|
||||||
if (isRegExp(value)) {
|
if (isRegExp(value)) {
|
||||||
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
|
return ctx.stylize(RegExp.prototype.toString.call(value), "regexp");
|
||||||
} else {
|
} else {
|
||||||
return ctx.stylize('[Object]', 'special');
|
return ctx.stylize("[Object]", "special");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.seen.push(value);
|
ctx.seen.push(value);
|
||||||
|
|
||||||
var output;
|
var output;
|
||||||
if (array) {
|
if (array) {
|
||||||
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
|
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
|
||||||
} else {
|
} else {
|
||||||
output = keys.map(function (key) {
|
output = keys.map(function (key) {
|
||||||
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
|
return formatProperty(
|
||||||
});
|
ctx,
|
||||||
}
|
value,
|
||||||
|
recurseTimes,
|
||||||
|
visibleKeys,
|
||||||
|
key,
|
||||||
|
array
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
ctx.seen.pop();
|
ctx.seen.pop();
|
||||||
|
|
||||||
return reduceToSingleString(output, base, braces);
|
return reduceToSingleString(output, base, braces);
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
|
||||||
var name, str, desc;
|
var name, str, desc;
|
||||||
desc = { value: void 0 };
|
desc = { value: void 0 };
|
||||||
try {
|
try {
|
||||||
// ie6 › navigator.toString
|
// ie6 › navigator.toString
|
||||||
// throws Error: Object doesn't support this property or method
|
// throws Error: Object doesn't support this property or method
|
||||||
desc.value = value[key];
|
desc.value = value[key];
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// ie10 › Object.getOwnPropertyDescriptor(window.location, 'hash')
|
// ie10 › Object.getOwnPropertyDescriptor(window.location, 'hash')
|
||||||
// throws TypeError: Object doesn't support this action
|
// throws TypeError: Object doesn't support this action
|
||||||
if (Object.getOwnPropertyDescriptor) {
|
if (Object.getOwnPropertyDescriptor) {
|
||||||
desc = Object.getOwnPropertyDescriptor(value, key) || desc;
|
desc = Object.getOwnPropertyDescriptor(value, key) || desc;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
if (desc.get) {
|
if (desc.get) {
|
||||||
if (desc.set) {
|
if (desc.set) {
|
||||||
str = ctx.stylize('[Getter/Setter]', 'special');
|
str = ctx.stylize("[Getter/Setter]", "special");
|
||||||
} else {
|
} else {
|
||||||
str = ctx.stylize('[Getter]', 'special');
|
str = ctx.stylize("[Getter]", "special");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (desc.set) {
|
if (desc.set) {
|
||||||
str = ctx.stylize('[Setter]', 'special');
|
str = ctx.stylize("[Setter]", "special");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!hasOwn(visibleKeys, key)) {
|
if (!hasOwn(visibleKeys, key)) {
|
||||||
name = '[' + key + ']';
|
name = "[" + key + "]";
|
||||||
}
|
}
|
||||||
if (!str) {
|
if (!str) {
|
||||||
if (ctx.seen.indexOf(desc.value) < 0) {
|
if (ctx.seen.indexOf(desc.value) < 0) {
|
||||||
if (isNull(recurseTimes)) {
|
if (isNull(recurseTimes)) {
|
||||||
str = formatValue(ctx, desc.value, null);
|
str = formatValue(ctx, desc.value, null);
|
||||||
|
} else {
|
||||||
|
str = formatValue(ctx, desc.value, recurseTimes - 1);
|
||||||
|
}
|
||||||
|
if (str.indexOf("\n") > -1) {
|
||||||
|
if (array) {
|
||||||
|
str = str
|
||||||
|
.split("\n")
|
||||||
|
.map(function (line) {
|
||||||
|
return " " + line;
|
||||||
|
})
|
||||||
|
.join("\n")
|
||||||
|
.substr(2);
|
||||||
} else {
|
} else {
|
||||||
str = formatValue(ctx, desc.value, recurseTimes - 1);
|
str =
|
||||||
|
"\n" +
|
||||||
|
str
|
||||||
|
.split("\n")
|
||||||
|
.map(function (line) {
|
||||||
|
return " " + line;
|
||||||
|
})
|
||||||
|
.join("\n");
|
||||||
}
|
}
|
||||||
if (str.indexOf('\n') > -1) {
|
}
|
||||||
if (array) {
|
} else {
|
||||||
str = str.split('\n').map(function (line) {
|
str = ctx.stylize("[Circular]", "special");
|
||||||
return ' ' + line;
|
}
|
||||||
}).join('\n').substr(2);
|
}
|
||||||
} else {
|
if (isUndefined(name)) {
|
||||||
str = '\n' + str.split('\n').map(function (line) {
|
if (array && key.match(/^\d+$/)) {
|
||||||
return ' ' + line;
|
return str;
|
||||||
}).join('\n');
|
}
|
||||||
}
|
name = JSON.stringify("" + key);
|
||||||
}
|
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
|
||||||
} else {
|
name = name.substr(1, name.length - 2);
|
||||||
str = ctx.stylize('[Circular]', 'special');
|
name = ctx.stylize(name, "name");
|
||||||
}
|
} else {
|
||||||
}
|
name = name
|
||||||
if (isUndefined(name)) {
|
.replace(/'/g, "\\'")
|
||||||
if (array && key.match(/^\d+$/)) {
|
.replace(/\\"/g, '"')
|
||||||
return str;
|
.replace(/(^"|"$)/g, "'");
|
||||||
}
|
name = ctx.stylize(name, "string");
|
||||||
name = JSON.stringify('' + key);
|
}
|
||||||
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
|
}
|
||||||
name = name.substr(1, name.length - 2);
|
|
||||||
name = ctx.stylize(name, 'name');
|
|
||||||
} else {
|
|
||||||
name = name.replace(/'/g, "\\'")
|
|
||||||
.replace(/\\"/g, '"')
|
|
||||||
.replace(/(^"|"$)/g, "'");
|
|
||||||
name = ctx.stylize(name, 'string');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return name + ': ' + str;
|
return name + ": " + str;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatPrimitive(ctx, value) {
|
function formatPrimitive(ctx, value) {
|
||||||
if (isUndefined(value))
|
if (isUndefined(value)) return ctx.stylize("undefined", "undefined");
|
||||||
return ctx.stylize('undefined', 'undefined');
|
if (isString(value)) {
|
||||||
if (isString(value)) {
|
var simple =
|
||||||
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
|
"'" +
|
||||||
|
JSON.stringify(value)
|
||||||
|
.replace(/^"|"$/g, "")
|
||||||
.replace(/'/g, "\\'")
|
.replace(/'/g, "\\'")
|
||||||
.replace(/\\"/g, '"') + '\'';
|
.replace(/\\"/g, '"') +
|
||||||
return ctx.stylize(simple, 'string');
|
"'";
|
||||||
}
|
return ctx.stylize(simple, "string");
|
||||||
if (isNumber(value))
|
}
|
||||||
return ctx.stylize('' + value, 'number');
|
if (isNumber(value)) return ctx.stylize("" + value, "number");
|
||||||
if (isBoolean(value))
|
if (isBoolean(value)) return ctx.stylize("" + value, "boolean");
|
||||||
return ctx.stylize('' + value, 'boolean');
|
// For some reason typeof null is "object", so special case here.
|
||||||
// For some reason typeof null is "object", so special case here.
|
if (isNull(value)) return ctx.stylize("null", "null");
|
||||||
if (isNull(value))
|
|
||||||
return ctx.stylize('null', 'null');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function reduceToSingleString(output, base, braces) {
|
function reduceToSingleString(output, base, braces) {
|
||||||
var numLinesEst = 0;
|
var numLinesEst = 0;
|
||||||
var length = output.reduce(function (prev, cur) {
|
var length = output.reduce(function (prev, cur) {
|
||||||
numLinesEst++;
|
numLinesEst++;
|
||||||
if (cur.indexOf('\n') >= 0) numLinesEst++;
|
if (cur.indexOf("\n") >= 0) numLinesEst++;
|
||||||
return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
|
return prev + cur.replace(/\u001b\[\d\d?m/g, "").length + 1;
|
||||||
}, 0);
|
}, 0);
|
||||||
|
|
||||||
if (length > 60) {
|
if (length > 60) {
|
||||||
return braces[0] +
|
return (
|
||||||
(base === '' ? '' : base + '\n ') +
|
braces[0] +
|
||||||
' ' +
|
(base === "" ? "" : base + "\n ") +
|
||||||
output.join(',\n ') +
|
" " +
|
||||||
' ' +
|
output.join(",\n ") +
|
||||||
braces[1];
|
" " +
|
||||||
}
|
braces[1]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
|
return braces[0] + base + " " + output.join(", ") + " " + braces[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
function _extend<T extends Y, Y>(origin: T, add: Y) {
|
function _extend<T extends Y, Y>(origin: T, add: Y) {
|
||||||
// Don't do anything if add isn't an object
|
// Don't do anything if add isn't an object
|
||||||
if (!add || !isObject(add)) return origin;
|
if (!add || !isObject(add)) return origin;
|
||||||
|
|
||||||
var keys = Object.keys(add);
|
var keys = Object.keys(add);
|
||||||
var i = keys.length;
|
var i = keys.length;
|
||||||
while (i--) {
|
while (i--) {
|
||||||
origin[keys[i]] = add[keys[i]];
|
origin[keys[i]] = add[keys[i]];
|
||||||
}
|
}
|
||||||
return origin;
|
return origin;
|
||||||
}
|
}
|
||||||
|
75
src/test.ts
75
src/test.ts
@ -1,39 +1,43 @@
|
|||||||
import { Logging, LoggingBase, LoggingTypes, Colors, withColor } from ".";
|
import Logging from "./index.js";
|
||||||
|
|
||||||
Logging.log("test")
|
import { LoggingBase, LoggingTypes, Format } from "./index.js";
|
||||||
|
|
||||||
|
Logging.log("test");
|
||||||
Logging.log("i", "am", { a: "an" }, 1000);
|
Logging.log("i", "am", { a: "an" }, 1000);
|
||||||
Logging.error(new Error("fehler 001"));
|
Logging.error(new Error("fehler 001"));
|
||||||
Logging.debug("Some Debug infos");
|
Logging.debug("Some Debug infos");
|
||||||
Logging.errorMessage("i", "am", "an", "error");
|
Logging.error("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(
|
||||||
|
"\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(Format.magenta("This text should be magenta!"), "This not!");
|
||||||
Logging.log(withColor(Colors.MAGENTA, { somekey: "Some value" }))
|
Logging.log(Format.magenta({ somekey: "Some value" }));
|
||||||
|
|
||||||
let err = new Error()
|
let err = new Error();
|
||||||
if (typeof err.stack !== "string") console.log("Stacktrace invalid", err.stack)
|
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!");
|
cus.log("This has some %c symbols inside of it!");
|
||||||
|
|
||||||
let cus2 = new LoggingBase("test2");
|
let cus2 = new LoggingBase({ name: "test2" });
|
||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2");
|
||||||
|
|
||||||
let cus22 = new LoggingBase("test2");
|
let cus22 = new LoggingBase({ name: "test2" });
|
||||||
cus22.log("Hello from custom Logger 22")
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2");
|
||||||
cus22.log("Hello from custom Logger 22")
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2");
|
||||||
cus22.log("Hello from custom Logger 22")
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2");
|
||||||
cus22.log("Hello from custom Logger 22")
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2");
|
||||||
cus22.log("Hello from custom Logger 22")
|
cus22.log("Hello from custom Logger 22");
|
||||||
cus2.log("Hello from custom Logger 2")
|
cus2.log("Hello from custom Logger 2");
|
||||||
|
|
||||||
Logging.debug("Only Errors should appear:")
|
Logging.debug("Only Errors should appear:");
|
||||||
Logging.logLevel = LoggingTypes.Error;
|
Logging.logLevel = LoggingTypes.Error;
|
||||||
|
|
||||||
Logging.debug("This should not be there 1");
|
Logging.debug("This should not be there 1");
|
||||||
@ -42,4 +46,27 @@ Logging.warn("This should not be there 3");
|
|||||||
Logging.warning("This should not be there 4");
|
Logging.warning("This should not be there 4");
|
||||||
|
|
||||||
Logging.error("This should be there 1");
|
Logging.error("This should be there 1");
|
||||||
Logging.errorMessage("This should be there 2");
|
Logging.error("This should be there 2");
|
||||||
|
|
||||||
|
Logging.logLevel = LoggingTypes.Debug;
|
||||||
|
|
||||||
|
const c1 = Logging.getChild("child-level-1");
|
||||||
|
|
||||||
|
c1.log("Hello from Child 1");
|
||||||
|
|
||||||
|
const c2 = c1.getChild("child-level-2");
|
||||||
|
|
||||||
|
c2.log("Hello from Child 2");
|
||||||
|
|
||||||
|
c2.log("MSG from C2");
|
||||||
|
c1.log("MSG from C1");
|
||||||
|
Logging.log("MSG from root");
|
||||||
|
|
||||||
|
const timer = Logging.time("timer1");
|
||||||
|
setTimeout(() => timer.end(), 1000);
|
||||||
|
|
||||||
|
const withoutFile = new LoggingBase({
|
||||||
|
resolve_filename: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
withoutFile.log("This should not have a file attached");
|
||||||
|
337
src/types.ts
337
src/types.ts
@ -1,119 +1,256 @@
|
|||||||
import { ObservableInterface } from "@hibas123/utils";
|
|
||||||
|
|
||||||
export enum LoggingTypes {
|
export enum LoggingTypes {
|
||||||
Debug,
|
Debug,
|
||||||
Log,
|
Log,
|
||||||
Warning,
|
Warn,
|
||||||
Error
|
Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const TerminalFormats = {
|
export const TerminalFormats = {
|
||||||
Reset: "\x1b[0m",
|
Reset: "\x1b[0m",
|
||||||
Bold: "\x1b[1m",
|
Bold: "\x1b[1m",
|
||||||
Underscore: "\x1b[4m",
|
Underscore: "\x1b[4m",
|
||||||
Blink: "\x1b[5m",
|
Blink: "\x1b[5m",
|
||||||
Reverse: "\x1b[7m",
|
Reverse: "\x1b[7m",
|
||||||
Hidden: "\x1b[8m",
|
Hidden: "\x1b[8m",
|
||||||
|
|
||||||
FgBlack: "\x1b[30m",
|
FgBlack: "\x1b[30m",
|
||||||
FgRed: "\x1b[31m",
|
FgRed: "\x1b[31m",
|
||||||
FgGreen: "\x1b[32m",
|
FgGreen: "\x1b[32m",
|
||||||
FgYellow: "\x1b[33m",
|
FgYellow: "\x1b[33m",
|
||||||
FgBlue: "\x1b[34m",
|
FgBlue: "\x1b[34m",
|
||||||
FgMagenta: "\x1b[35m",
|
FgMagenta: "\x1b[35m",
|
||||||
FgCyan: "\x1b[36m",
|
FgCyan: "\x1b[36m",
|
||||||
FgWhite: "\x1b[37m",
|
FgWhite: "\x1b[37m",
|
||||||
|
|
||||||
BgBlack: "\x1b[40m",
|
BgBlack: "\x1b[40m",
|
||||||
BgRed: "\x1b[41m",
|
BgRed: "\x1b[41m",
|
||||||
BgGreen: "\x1b[42m",
|
BgGreen: "\x1b[42m",
|
||||||
BgYellow: "\x1b[43m",
|
BgYellow: "\x1b[43m",
|
||||||
BgBlue: "\x1b[44m",
|
BgBlue: "\x1b[44m",
|
||||||
BgMagenta: "\x1b[45m",
|
BgMagenta: "\x1b[45m",
|
||||||
BgCyan: "\x1b[46m",
|
BgCyan: "\x1b[46m",
|
||||||
BgWhite: "\x1b[47m"
|
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 enum IColors {
|
||||||
|
NONE,
|
||||||
|
RED,
|
||||||
|
GREEN,
|
||||||
|
YELLOW,
|
||||||
|
BLUE,
|
||||||
|
MAGENTA,
|
||||||
|
CYAN,
|
||||||
|
WHITE,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IFormatted<T = any> {
|
||||||
|
color?: IColors;
|
||||||
|
bgcolor?: IColors;
|
||||||
|
bold?: boolean;
|
||||||
|
italic?: boolean;
|
||||||
|
blink?: boolean;
|
||||||
|
underscore?: boolean;
|
||||||
|
|
||||||
|
content: T;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Formatted<T = any> implements IFormatted<T> {
|
||||||
|
static strip(formatted: IFormatted<string> | IFormatted<string>[]) {
|
||||||
|
if (Array.isArray(formatted)) {
|
||||||
|
return formatted.reduce((p, c) => p + c.content, "");
|
||||||
|
} else {
|
||||||
|
return formatted.content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(content?: T, apply?: Formatted) {
|
||||||
|
if (content instanceof Formatted) {
|
||||||
|
this.color = content.color;
|
||||||
|
this.bgcolor = content.bgcolor;
|
||||||
|
this.bold = content.bold;
|
||||||
|
this.italic = content.italic;
|
||||||
|
this.blink = content.blink;
|
||||||
|
this.underscore = content.underscore;
|
||||||
|
this.content = content.content;
|
||||||
|
} else {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (apply) {
|
||||||
|
this.color = apply.color;
|
||||||
|
this.bgcolor = apply.bgcolor;
|
||||||
|
this.bold = apply.bold;
|
||||||
|
this.italic = apply.italic;
|
||||||
|
this.blink = apply.blink;
|
||||||
|
this.underscore = apply.underscore;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
color?: IColors;
|
||||||
|
bgcolor?: IColors;
|
||||||
|
bold?: boolean;
|
||||||
|
italic?: boolean;
|
||||||
|
blink?: boolean;
|
||||||
|
underscore?: boolean;
|
||||||
|
|
||||||
|
content: T;
|
||||||
|
|
||||||
|
_color(c: IColors) {
|
||||||
|
this.color = c;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_bgcolor(c: IColors) {
|
||||||
|
this.bgcolor = c;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_bold(active = true) {
|
||||||
|
this.bold = active;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_italic(active = true) {
|
||||||
|
this.italic = active;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
_blink(active = true) {
|
||||||
|
this.blink = active;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const AddColorFormat = (color: IColors) => (content: any) => {
|
||||||
|
if (content instanceof Formatted) {
|
||||||
|
content.color = color;
|
||||||
|
return content;
|
||||||
|
} else {
|
||||||
|
const d = new Formatted(content);
|
||||||
|
d.color = color;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const AddBGColorFormat = (color: IColors) => (content: any) => {
|
||||||
|
if (content instanceof Formatted) {
|
||||||
|
content.bgcolor = color;
|
||||||
|
return content;
|
||||||
|
} else {
|
||||||
|
const d = new Formatted(content);
|
||||||
|
d.bgcolor = color;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Format = {
|
||||||
|
none: AddColorFormat(IColors.NONE),
|
||||||
|
red: AddColorFormat(IColors.RED),
|
||||||
|
green: AddColorFormat(IColors.GREEN),
|
||||||
|
yellow: AddColorFormat(IColors.YELLOW),
|
||||||
|
blue: AddColorFormat(IColors.BLUE),
|
||||||
|
magenta: AddColorFormat(IColors.MAGENTA),
|
||||||
|
cyan: AddColorFormat(IColors.CYAN),
|
||||||
|
white: AddColorFormat(IColors.WHITE),
|
||||||
|
bgnone: AddBGColorFormat(IColors.NONE),
|
||||||
|
bgred: AddBGColorFormat(IColors.RED),
|
||||||
|
bggreen: AddBGColorFormat(IColors.GREEN),
|
||||||
|
bgyellow: AddBGColorFormat(IColors.YELLOW),
|
||||||
|
bgblue: AddBGColorFormat(IColors.BLUE),
|
||||||
|
bgmagenta: AddBGColorFormat(IColors.MAGENTA),
|
||||||
|
bgcyan: AddBGColorFormat(IColors.CYAN),
|
||||||
|
bgwhite: AddBGColorFormat(IColors.WHITE),
|
||||||
|
bold: (content: any) => {
|
||||||
|
if (content instanceof Formatted) {
|
||||||
|
content.bold = true;
|
||||||
|
return content;
|
||||||
|
} else {
|
||||||
|
const d = new Formatted(content);
|
||||||
|
d.bold = true;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
italic: (content: any) => {
|
||||||
|
if (content instanceof Formatted) {
|
||||||
|
content.italic = true;
|
||||||
|
return content;
|
||||||
|
} else {
|
||||||
|
const d = new Formatted(content);
|
||||||
|
d.italic = true;
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export interface FormatConfig {
|
||||||
|
error: Formatted;
|
||||||
|
warning: Formatted;
|
||||||
|
log: Formatted;
|
||||||
|
debug: Formatted;
|
||||||
|
|
||||||
|
date: Formatted;
|
||||||
|
file: Formatted;
|
||||||
|
|
||||||
|
names: Formatted;
|
||||||
|
names_delimiter: Formatted;
|
||||||
|
}
|
||||||
|
|
||||||
export class DefaultFormatConfig implements FormatConfig {
|
export class DefaultFormatConfig implements FormatConfig {
|
||||||
error = [colorFormat(Colors.RED), boldFormat];
|
error = new Formatted()._color(IColors.RED)._bold();
|
||||||
warning = [colorFormat(Colors.YELLOW), boldFormat];
|
warning = new Formatted()._color(IColors.YELLOW)._bold();
|
||||||
log = [colorFormat(Colors.NONE), boldFormat];
|
log = new Formatted()._color(IColors.NONE)._bold();
|
||||||
debug = [colorFormat(Colors.CYAN), boldFormat];
|
debug = new Formatted()._color(IColors.CYAN)._bold();
|
||||||
|
|
||||||
date = [colorFormat(Colors.NONE)];
|
date = new Formatted()._color(IColors.NONE);
|
||||||
file = [colorFormat(Colors.NONE)];
|
file = new Formatted()._color(IColors.NONE);
|
||||||
|
names = new Formatted()._bold()._color(IColors.CYAN);
|
||||||
|
names_delimiter = new Formatted(" -> ")._bold();
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
names?: string[];
|
||||||
text: {
|
text: IFormatted<string>[];
|
||||||
raw: string[],
|
date: Date;
|
||||||
formatted: FormattedLine[]
|
file: string;
|
||||||
};
|
|
||||||
date: Date;
|
|
||||||
file: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Adapter {
|
export interface Adapter {
|
||||||
init(observable: ObservableInterface<Message>, name?: string): void | Promise<void>;
|
readonly level: LoggingTypes;
|
||||||
|
|
||||||
flush(sync: true): void;
|
/**
|
||||||
flush(sync: false): void | Promise<void>;
|
* This function initialises the Adapter. It might be called multiple times, when added to multiple instances
|
||||||
|
* @param observable An observable to subscribe to messages
|
||||||
|
*/
|
||||||
|
init(): void | Promise<void>;
|
||||||
|
|
||||||
|
flush(sync: true): void;
|
||||||
|
flush(sync: false): void | Promise<void>;
|
||||||
|
|
||||||
close?(): void;
|
setLevel(level: LoggingTypes): void;
|
||||||
}
|
|
||||||
|
onMessage(message: Message): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a close function is available, it will be called when no logging instance references it anymore.
|
||||||
|
*
|
||||||
|
* WARNING: The adapter might be reinitialised, when it is added to a new Logging instance
|
||||||
|
*/
|
||||||
|
close?(): void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ILoggingTimer {
|
||||||
|
end: () => number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ILoggingInterface {
|
||||||
|
debug(...message: any[]): void;
|
||||||
|
log(...message: any[]): void;
|
||||||
|
warn(...message: any[]): void;
|
||||||
|
error(...message: any[]): void;
|
||||||
|
|
||||||
|
time(name?: string): ILoggingTimer;
|
||||||
|
timeEnd(id: string): number;
|
||||||
|
|
||||||
|
getChild(name: string): ILoggingInterface;
|
||||||
|
}
|
||||||
|
@ -1,19 +1,13 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "commonjs",
|
"module": "ESNext",
|
||||||
"target": "es2017",
|
"target": "ESNext",
|
||||||
|
"moduleResolution": "node",
|
||||||
|
"outDir": "esm",
|
||||||
"noImplicitAny": false,
|
"noImplicitAny": false,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "out",
|
"declaration": true
|
||||||
"declaration": true,
|
|
||||||
"typeRoots": [
|
|
||||||
"node_modules/@types"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": ["node_modules"],
|
||||||
"node_modules"
|
"include": ["src"]
|
||||||
],
|
}
|
||||||
"include": [
|
|
||||||
"src"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user