@lowfat/log 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -1
- package/dist/index.d.mts +42 -2
- package/dist/index.mjs +66 -13
- package/package.json +1 -10
- package/tsdown.config.ts +9 -0
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
1
|
# Lowfat Log
|
|
2
2
|
|
|
3
|
-
A simple and easy-to-use logging tool, small enough to be included with client-side code, but works excellent with Node, and Deno, as well.
|
|
3
|
+
A simple and easy-to-use logging tool, small enough to be included with client-side code, but works excellent with Node, and Deno, as well.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
> [!IMPORTANT]
|
|
8
|
+
> `@lowfat/log` does currently not work in browsers (i.e., via [UNPKG](https://unpkg.com)). Feel free to change that and let me know via a pull request!
|
|
9
|
+
|
|
10
|
+
Install the package with [NPM](https://npmjs.com), [NodeJS](https://nodejs.org) is required:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm i @lowfat/log
|
|
14
|
+
# or only for development purposes
|
|
15
|
+
npm i -D @lowfat/log
|
|
16
|
+
```
|
|
17
|
+
Directly import the package into your project:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import Log from '@lowfat/log';
|
|
21
|
+
|
|
22
|
+
Log.info('Works without initialization!');
|
|
23
|
+
// prints "[LFP] Works without initialization!"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Customization
|
|
27
|
+
|
|
28
|
+
#### Change label
|
|
29
|
+
|
|
30
|
+
By default, every message is prefixed with `[LFP]`, to change that, you have to initialize a new instance of the logger:
|
|
31
|
+
|
|
32
|
+
```javascript
|
|
33
|
+
import Log from '@lowfat/log';
|
|
34
|
+
|
|
35
|
+
const sublabel = 'my sublabel';
|
|
36
|
+
const logger1 = new Log({ sublabel });
|
|
37
|
+
logger1.info('Now with custom label!');
|
|
38
|
+
// prints "[LFP/my sublabel] Now with custom label!"
|
|
39
|
+
|
|
40
|
+
const logger2 = new Log({ label: 'my label', sublabel });
|
|
41
|
+
logger2.warn('Custom label AND sublabel');
|
|
42
|
+
// prints "[my label/my sublabel] Custom label AND sublabel"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Use this logger as your new default:
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
// ./logger.js
|
|
49
|
+
import Log from '@lowfat/log';
|
|
50
|
+
|
|
51
|
+
export const logger = new Log({ label: 'my label' });
|
|
52
|
+
|
|
53
|
+
// ./some-place-else.js
|
|
54
|
+
import { logger } from './path/to/logger.js';
|
|
55
|
+
|
|
56
|
+
logger.info('Works everywhere!');
|
|
57
|
+
// prints "[my label] Works everywhere!"
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Development
|
|
61
|
+
|
|
62
|
+
Clone this repository and install dependencies:
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
git clone https://codeberg.org/lowfatprophet/log.git
|
|
66
|
+
cd log
|
|
67
|
+
npm i
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Start a development watcher for faster iterations:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
npm run dev
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Building is done with [tsdown](https://tsdown.dev):
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
npm run build
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Testing is done with [vitest](https://vitest.dev):
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
npm run test
|
|
86
|
+
```
|
package/dist/index.d.mts
CHANGED
|
@@ -1,24 +1,64 @@
|
|
|
1
1
|
//#region src/index.d.ts
|
|
2
|
+
declare global {
|
|
3
|
+
interface ImportMeta {
|
|
4
|
+
env: ImportMetaEnv;
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
interface ImportMetaEnv {
|
|
8
|
+
LOG: string;
|
|
9
|
+
}
|
|
2
10
|
interface LoggerProgessOptions {
|
|
3
11
|
color: number;
|
|
4
12
|
symbol: string;
|
|
5
13
|
}
|
|
14
|
+
interface LoggerOptions {
|
|
15
|
+
label?: string;
|
|
16
|
+
sublabel?: string;
|
|
17
|
+
total?: number;
|
|
18
|
+
}
|
|
19
|
+
interface LoggerColors {
|
|
20
|
+
BLACK: number;
|
|
21
|
+
CYAN: number;
|
|
22
|
+
GRAY: number;
|
|
23
|
+
GREEN: number;
|
|
24
|
+
MAGENTA: number;
|
|
25
|
+
RED: number;
|
|
26
|
+
YELLOW: number;
|
|
27
|
+
}
|
|
6
28
|
declare class Log {
|
|
7
29
|
#private;
|
|
30
|
+
static colors: LoggerColors;
|
|
8
31
|
static error(...message: string[]): void;
|
|
9
32
|
static warn(...message: string[]): void;
|
|
10
33
|
static note(...message: string[]): void;
|
|
11
34
|
static info(...message: string[]): void;
|
|
12
|
-
|
|
35
|
+
static success(...message: string[]): void;
|
|
36
|
+
static debug(...message: string[]): void;
|
|
37
|
+
constructor({
|
|
38
|
+
label,
|
|
39
|
+
sublabel,
|
|
40
|
+
total
|
|
41
|
+
}: LoggerOptions);
|
|
13
42
|
error(...message: string[]): void;
|
|
14
43
|
warn(...message: string[]): void;
|
|
15
44
|
note(...message: string[]): void;
|
|
16
45
|
info(...message: string[]): void;
|
|
46
|
+
success(...message: string[]): void;
|
|
47
|
+
debug(...message: string[]): void;
|
|
17
48
|
update(total: number): void;
|
|
18
49
|
progress(i: number, {
|
|
19
50
|
color,
|
|
20
51
|
symbol
|
|
21
52
|
}?: Partial<LoggerProgessOptions>): void;
|
|
22
53
|
}
|
|
54
|
+
declare function error(...message: string[]): void;
|
|
55
|
+
declare function warn(...message: string[]): void;
|
|
56
|
+
declare function note(...message: string[]): void;
|
|
57
|
+
declare function info(...message: string[]): void;
|
|
58
|
+
declare function success(...message: string[]): void;
|
|
59
|
+
declare function debug(...message: string[]): void;
|
|
60
|
+
declare function bold(msg: string | number): string;
|
|
61
|
+
declare function italic(msg: string | number): string;
|
|
62
|
+
declare function dim(msg: string | number): string;
|
|
23
63
|
//#endregion
|
|
24
|
-
export { LoggerProgessOptions, Log as default };
|
|
64
|
+
export { LoggerColors, LoggerOptions, LoggerProgessOptions, bold, debug, Log as default, dim, error, info, italic, note, success, warn };
|
package/dist/index.mjs
CHANGED
|
@@ -1,32 +1,49 @@
|
|
|
1
1
|
import { zeroPad } from "@lowfat/utils/strings";
|
|
2
2
|
//#region src/index.ts
|
|
3
3
|
var Log = class Log {
|
|
4
|
+
static {
|
|
5
|
+
this.colors = {
|
|
6
|
+
BLACK: 0,
|
|
7
|
+
CYAN: 36,
|
|
8
|
+
GRAY: 90,
|
|
9
|
+
GREEN: 32,
|
|
10
|
+
MAGENTA: 35,
|
|
11
|
+
RED: 31,
|
|
12
|
+
YELLOW: 33
|
|
13
|
+
};
|
|
14
|
+
}
|
|
4
15
|
/** label The custom LFP label */
|
|
5
|
-
static #label = "
|
|
16
|
+
static #label = "LFP";
|
|
6
17
|
/** The column width of the `stdout` terminal window, minus padding */
|
|
7
18
|
static #maxColumns = process.stdout.columns - Log.#label.length - 6;
|
|
8
19
|
/** Per default `false` gets set to `true` when first instance gets created and sets up one listener to change the static variable `#maxColumns` */
|
|
9
20
|
static #listening = false;
|
|
10
21
|
/** Styles the line prefix. */
|
|
11
22
|
static #prefix(label = Log.#label) {
|
|
12
|
-
return `\x1b[
|
|
23
|
+
return `\x1b[${Log.colors.GRAY}m[${label}]\x1b[0m`;
|
|
13
24
|
}
|
|
14
25
|
/** Creates a colored output string for printing; https://stackoverflow.com/a/41407246 */
|
|
15
26
|
static #clr(clr, msg) {
|
|
16
27
|
return `\x1b[${clr}m${msg}\x1b[0m`;
|
|
17
28
|
}
|
|
18
29
|
static error(...message) {
|
|
19
|
-
console.error(Log.#prefix(), Log.#clr(
|
|
30
|
+
console.error(Log.#prefix(), Log.#clr(Log.colors.RED, message.join(" ")));
|
|
20
31
|
}
|
|
21
32
|
static warn(...message) {
|
|
22
|
-
console.warn(Log.#prefix(), Log.#clr(
|
|
33
|
+
console.warn(Log.#prefix(), Log.#clr(Log.colors.YELLOW, message.join(" ")));
|
|
23
34
|
}
|
|
24
35
|
static note(...message) {
|
|
25
|
-
console.info(Log.#prefix(), Log.#clr(
|
|
36
|
+
console.info(Log.#prefix(), Log.#clr(Log.colors.CYAN, message.join(" ")));
|
|
26
37
|
}
|
|
27
38
|
static info(...message) {
|
|
28
39
|
console.log(Log.#prefix(), message.join(" "));
|
|
29
40
|
}
|
|
41
|
+
static success(...message) {
|
|
42
|
+
console.log(Log.#prefix(), Log.#clr(Log.colors.GREEN, message.join(" ")));
|
|
43
|
+
}
|
|
44
|
+
static debug(...message) {
|
|
45
|
+
console.debug(Log.#prefix(), Log.#clr(Log.colors.MAGENTA, message.join(" ")));
|
|
46
|
+
}
|
|
30
47
|
/** Updates the static value for maximum column count. */
|
|
31
48
|
static #setMaxColumns(label, padding = 6) {
|
|
32
49
|
Log.#maxColumns = process.stdout.columns - label.length - padding;
|
|
@@ -42,26 +59,32 @@ var Log = class Log {
|
|
|
42
59
|
#total = 0;
|
|
43
60
|
/** A custom prefix to identify origin of the logging. */
|
|
44
61
|
#instancePrefix;
|
|
45
|
-
/** Log instance that allows for updating */
|
|
46
|
-
constructor(label, total) {
|
|
47
|
-
const instanceLabel =
|
|
62
|
+
/** Log instance that allows for updating and custom labels */
|
|
63
|
+
constructor({ label, sublabel, total }) {
|
|
64
|
+
const instanceLabel = sublabel?.length ? `${label?.length ? label : Log.#label}/${sublabel}` : label?.length ? label : Log.#label;
|
|
48
65
|
this.#instancePrefix = Log.#prefix(instanceLabel);
|
|
49
|
-
this.update(total);
|
|
66
|
+
if (total) this.update(total);
|
|
50
67
|
Log.#setMaxColumns(instanceLabel);
|
|
51
68
|
Log.#setupListener(instanceLabel);
|
|
52
69
|
}
|
|
53
70
|
error(...message) {
|
|
54
|
-
console.error(this.#instancePrefix, Log.#clr(
|
|
71
|
+
console.error(this.#instancePrefix, Log.#clr(Log.colors.RED, message.join(" ")));
|
|
55
72
|
}
|
|
56
73
|
warn(...message) {
|
|
57
|
-
console.warn(this.#instancePrefix, Log.#clr(
|
|
74
|
+
console.warn(this.#instancePrefix, Log.#clr(Log.colors.YELLOW, message.join(" ")));
|
|
58
75
|
}
|
|
59
76
|
note(...message) {
|
|
60
|
-
console.info(this.#instancePrefix, Log.#clr(
|
|
77
|
+
console.info(this.#instancePrefix, Log.#clr(Log.colors.CYAN, message.join(" ")));
|
|
61
78
|
}
|
|
62
79
|
info(...message) {
|
|
63
80
|
console.log(this.#instancePrefix, message.join(" "));
|
|
64
81
|
}
|
|
82
|
+
success(...message) {
|
|
83
|
+
console.log(this.#instancePrefix, Log.#clr(Log.colors.GREEN, message.join(" ")));
|
|
84
|
+
}
|
|
85
|
+
debug(...message) {
|
|
86
|
+
console.debug(this.#instancePrefix, Log.#clr(Log.colors.MAGENTA, message.join(" ")));
|
|
87
|
+
}
|
|
65
88
|
/** Updates the total number, resets the progress visualizer. */
|
|
66
89
|
update(total) {
|
|
67
90
|
this.#total = total ?? 0;
|
|
@@ -76,5 +99,35 @@ var Log = class Log {
|
|
|
76
99
|
if (i >= this.#total - 1) process.stdout.write("\n");
|
|
77
100
|
}
|
|
78
101
|
};
|
|
102
|
+
function error(...message) {
|
|
103
|
+
Log.error(...message);
|
|
104
|
+
}
|
|
105
|
+
function warn(...message) {
|
|
106
|
+
Log.warn(...message);
|
|
107
|
+
}
|
|
108
|
+
function note(...message) {
|
|
109
|
+
Log.note(...message);
|
|
110
|
+
}
|
|
111
|
+
function info(...message) {
|
|
112
|
+
Log.info(...message);
|
|
113
|
+
}
|
|
114
|
+
function success(...message) {
|
|
115
|
+
Log.success(...message);
|
|
116
|
+
}
|
|
117
|
+
function debug(...message) {
|
|
118
|
+
Log.debug(...message);
|
|
119
|
+
}
|
|
120
|
+
/** Returns a string affixed with ASCII formatting for bold text. */
|
|
121
|
+
function bold(msg) {
|
|
122
|
+
return `\x1b[1m${msg}\x1b[22m`;
|
|
123
|
+
}
|
|
124
|
+
/** Returns a string affixed with ASCII formatting for italic text. */
|
|
125
|
+
function italic(msg) {
|
|
126
|
+
return `\x1b[3m${msg}\x1b[23m`;
|
|
127
|
+
}
|
|
128
|
+
/** Returns a string affixed with ASCII formatting for dim text. */
|
|
129
|
+
function dim(msg) {
|
|
130
|
+
return `\x1b[2m${msg}\x1b[22m`;
|
|
131
|
+
}
|
|
79
132
|
//#endregion
|
|
80
|
-
export { Log as default };
|
|
133
|
+
export { bold, debug, Log as default, dim, error, info, italic, note, success, warn };
|
package/package.json
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@biomejs/biome": "^2.4.14",
|
|
12
12
|
"@types/node": "^25.6.2",
|
|
13
|
-
"@typescript/native-preview": "^7.0.0-dev.20260421.2",
|
|
14
13
|
"tsdown": "^0.22.0",
|
|
15
14
|
"tslib": "^2.8.1",
|
|
16
15
|
"typescript": "^6.0.3",
|
|
@@ -27,15 +26,7 @@
|
|
|
27
26
|
"dev": "npx tsdown --watch",
|
|
28
27
|
"test": "vitest --disableConsoleIntercept true"
|
|
29
28
|
},
|
|
30
|
-
"tsdown": {
|
|
31
|
-
"format": {
|
|
32
|
-
"esm": [
|
|
33
|
-
"nodenext"
|
|
34
|
-
]
|
|
35
|
-
},
|
|
36
|
-
"outDir": "dist"
|
|
37
|
-
},
|
|
38
29
|
"type": "module",
|
|
39
30
|
"types": "./dist/index.d.mts",
|
|
40
|
-
"version": "0.0
|
|
31
|
+
"version": "0.2.0"
|
|
41
32
|
}
|