pretty-logx 0.1.0 → 0.1.1
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 +69 -106
- package/package.json +11 -11
package/README.md
CHANGED
|
@@ -1,157 +1,120 @@
|
|
|
1
1
|
# pretty-logx
|
|
2
2
|
|
|
3
|
-
A
|
|
4
|
-
|
|
5
|
-
[](https://www.npmjs.com/package/pretty-logx)
|
|
6
|
-
[](LICENSE)
|
|
7
|
-
[](https://nodejs.org)
|
|
8
|
-
|
|
9
|
-
## Features
|
|
10
|
-
|
|
11
|
-
- 🎨 **Colored terminal output** with timestamps
|
|
12
|
-
- 📊 **6 log levels**: `trace`, `debug`, `info`, `warn`, `error`, `fatal`
|
|
13
|
-
- 🏭 **JSON mode** for structured production logging
|
|
14
|
-
- 🔖 **Namespaces** for tagging log sources
|
|
15
|
-
- 👶 **Child loggers** with inherited bindings
|
|
16
|
-
- 🐛 **Pretty error stack traces**
|
|
17
|
-
- 📦 **Structured logging** with object bindings
|
|
18
|
-
- 🎛️ **Configurable levels and colors**
|
|
19
|
-
- 🚫 **NO_COLOR** and **FORCE_COLOR** support
|
|
20
|
-
- 🔄 **Safe circular reference handling**
|
|
21
|
-
- 📝 **TypeScript-first** with full type inference
|
|
22
|
-
- 📦 **ESM + CommonJS** dual package
|
|
23
|
-
- ⚡ **Zero runtime dependencies**
|
|
24
|
-
- 🟢 **Node.js 18+**
|
|
25
|
-
|
|
26
|
-
## Installation
|
|
3
|
+
A clean, lightweight logger for Node.js with colored dev output, structured JSON for production, and zero dependencies.
|
|
27
4
|
|
|
28
5
|
```bash
|
|
29
6
|
npm install pretty-logx
|
|
30
7
|
```
|
|
31
8
|
|
|
32
|
-
##
|
|
9
|
+
## Why
|
|
10
|
+
|
|
11
|
+
Most loggers are either too heavy (Pino, Winston) or too barebones (Console). `pretty-logx` gives you formatted terminal output in development, fast structured JSON in production, child loggers with context, and safe circular JSON handling — all in under 5KB with zero runtime dependencies.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Basic
|
|
33
16
|
|
|
34
17
|
```ts
|
|
35
18
|
import { createLogger } from 'pretty-logx';
|
|
36
19
|
|
|
37
|
-
const log = createLogger(
|
|
20
|
+
const log = createLogger();
|
|
38
21
|
|
|
39
|
-
log.info('Server started
|
|
40
|
-
log.
|
|
41
|
-
log.error(
|
|
22
|
+
log.info('Server started on port 3000');
|
|
23
|
+
log.warn('Disk space low', { remaining: '1.2GB' });
|
|
24
|
+
log.error('Database connection failed', new Error('ECONNREFUSED'));
|
|
42
25
|
```
|
|
43
26
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
### `createLogger(config?)`
|
|
27
|
+
### Context & Child Loggers
|
|
47
28
|
|
|
48
|
-
|
|
29
|
+
Pass request metadata or create scoped child loggers:
|
|
49
30
|
|
|
50
31
|
```ts
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
json: false, // JSON mode for production (default: false)
|
|
54
|
-
namespace: 'myapp', // Optional namespace prefix
|
|
55
|
-
timestamp: true, // Include timestamps (default: true)
|
|
56
|
-
timestampFormat: 'short', // 'iso' | 'short' | 'none' (default: 'short')
|
|
57
|
-
colors: true, // Override color auto-detection
|
|
58
|
-
write: (msg, level) => process.stdout.write(msg + '\n'), // Custom writer
|
|
59
|
-
});
|
|
60
|
-
```
|
|
32
|
+
// Bindings first (structured)
|
|
33
|
+
log.info({ userId: 'u_123', action: 'checkout' }, 'Order placed');
|
|
61
34
|
|
|
62
|
-
|
|
35
|
+
// Scoped child logger
|
|
36
|
+
const authLog = log.child({ scope: 'auth' });
|
|
37
|
+
authLog.debug('Verifying JWT');
|
|
38
|
+
authLog.info({ userId: 'u_123' }, 'Session created');
|
|
39
|
+
```
|
|
63
40
|
|
|
64
|
-
|
|
41
|
+
### Production JSON Mode
|
|
65
42
|
|
|
66
43
|
```ts
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
44
|
+
const log = createLogger({
|
|
45
|
+
level: 'info',
|
|
46
|
+
json: process.env.NODE_ENV === 'production',
|
|
47
|
+
namespace: 'api',
|
|
48
|
+
});
|
|
72
49
|
|
|
73
|
-
|
|
74
|
-
|
|
50
|
+
log.info({ path: '/api/v1/users', status: 200 }, 'Request completed');
|
|
51
|
+
```
|
|
75
52
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
53
|
+
Output:
|
|
54
|
+
```json
|
|
55
|
+
{"level":"info","levelValue":30,"time":"2026-11-23T17:15:00.000Z","msg":"Request completed","namespace":"api","path":"/api/v1/users","status":200}
|
|
79
56
|
```
|
|
80
57
|
|
|
81
|
-
|
|
58
|
+
### Error Handling
|
|
82
59
|
|
|
83
|
-
|
|
60
|
+
Pass standard errors and get formatted stack traces in dev, or serialized error objects in JSON mode:
|
|
84
61
|
|
|
85
62
|
```ts
|
|
86
|
-
|
|
87
|
-
|
|
63
|
+
try {
|
|
64
|
+
throw new Error('Something broke');
|
|
65
|
+
} catch (err) {
|
|
66
|
+
log.error('Operation failed', err);
|
|
88
67
|
}
|
|
89
68
|
```
|
|
90
69
|
|
|
91
|
-
|
|
70
|
+
### Circular Objects
|
|
92
71
|
|
|
93
|
-
|
|
72
|
+
Circular references are caught and serialized as `[Circular]` instead of crashing your app:
|
|
94
73
|
|
|
95
74
|
```ts
|
|
96
|
-
const
|
|
97
|
-
|
|
75
|
+
const obj: any = { name: 'app' };
|
|
76
|
+
obj.self = obj;
|
|
98
77
|
|
|
99
|
-
|
|
100
|
-
const quietChild = log.child({}, { level: 'warn' });
|
|
78
|
+
log.info({ config: obj }, 'Loaded configuration');
|
|
101
79
|
```
|
|
102
80
|
|
|
103
|
-
|
|
81
|
+
## Options
|
|
104
82
|
|
|
105
|
-
|
|
106
|
-
const log = createLogger({ json: true, level: 'info' });
|
|
107
|
-
log.info({ userId: 42 }, 'Login');
|
|
108
|
-
// Output: {"level":"info","levelValue":30,"time":"2024-01-15T12:00:00.000Z","msg":"Login","userId":42}
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
### Runtime Configuration
|
|
83
|
+
`createLogger(options)` accepts:
|
|
112
84
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
setColorsEnabled(false); // Disable colors globally
|
|
123
|
-
|
|
124
|
-
// Or via environment:
|
|
125
|
-
// NO_COLOR=1 → disables colors
|
|
126
|
-
// FORCE_COLOR=1 → enables colors even in non-TTY
|
|
127
|
-
```
|
|
85
|
+
| Option | Type | Default | Description |
|
|
86
|
+
| --- | --- | --- | --- |
|
|
87
|
+
| `level` | `string` | `'info'` | `'trace'`, `'debug'`, `'info'`, `'warn'`, `'error'`, `'fatal'`, `'silent'` |
|
|
88
|
+
| `json` | `boolean` | `false` | Enable structured JSON output |
|
|
89
|
+
| `namespace` | `string` | `undefined` | Optional prefix tag (e.g. `api`, `worker`) |
|
|
90
|
+
| `timestamp` | `boolean` | `true` | Show timestamp in dev logs |
|
|
91
|
+
| `timestampFormat` | `string` | `'short'` | `'short'` (`HH:MM:SS.mmm`), `'iso'`, or `'none'` |
|
|
92
|
+
| `colors` | `boolean` | `auto` | Auto-detected. Supports `NO_COLOR` and `FORCE_COLOR` |
|
|
93
|
+
| `write` | `function` | `stdout/stderr` | Custom write handler `(output, level) => void` |
|
|
128
94
|
|
|
129
95
|
## Log Levels
|
|
130
96
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
| `fatal` | 60 | Critical failures |
|
|
139
|
-
| `silent`| ∞ | Suppress all output |
|
|
140
|
-
|
|
141
|
-
## TypeScript
|
|
97
|
+
- `trace` (10)
|
|
98
|
+
- `debug` (20)
|
|
99
|
+
- `info` (30)
|
|
100
|
+
- `warn` (40)
|
|
101
|
+
- `error` (50) — writes to `stderr`
|
|
102
|
+
- `fatal` (60) — writes to `stderr`
|
|
103
|
+
- `silent` (Infinity)
|
|
142
104
|
|
|
143
|
-
|
|
105
|
+
Check active levels before running expensive operations:
|
|
144
106
|
|
|
145
107
|
```ts
|
|
146
|
-
|
|
108
|
+
if (log.isDebugEnabled) {
|
|
109
|
+
log.debug('Debug payload', getExpensiveMetrics());
|
|
110
|
+
}
|
|
147
111
|
```
|
|
148
112
|
|
|
149
|
-
##
|
|
113
|
+
## Environment Flags
|
|
150
114
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
```
|
|
115
|
+
- `NO_COLOR=1` — disables ANSI colors ([no-color.org](https://no-color.org))
|
|
116
|
+
- `FORCE_COLOR=1` — forces ANSI colors even in non-TTY environments
|
|
154
117
|
|
|
155
118
|
## License
|
|
156
119
|
|
|
157
|
-
[
|
|
120
|
+
MIT © [Rudransh](https://github.com/rayrudr1817)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pretty-logx",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "A lightweight, beautiful, TypeScript-first logging library for Node.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"logger",
|
|
@@ -12,31 +12,31 @@
|
|
|
12
12
|
"json",
|
|
13
13
|
"node"
|
|
14
14
|
],
|
|
15
|
-
"author": "",
|
|
15
|
+
"author": "Rudransh",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "https://github.com/
|
|
19
|
+
"url": "git+https://github.com/rayrudr1817/pretty-logx.git"
|
|
20
20
|
},
|
|
21
|
-
"homepage": "https://github.com/
|
|
21
|
+
"homepage": "https://github.com/rayrudr1817/pretty-logx#readme",
|
|
22
22
|
"bugs": {
|
|
23
|
-
"url": "https://github.com/
|
|
23
|
+
"url": "https://github.com/rayrudr1817/pretty-logx/issues"
|
|
24
24
|
},
|
|
25
25
|
"engines": {
|
|
26
26
|
"node": ">=18.0.0"
|
|
27
27
|
},
|
|
28
|
-
"main": "./dist/index.
|
|
29
|
-
"module": "./dist/index.
|
|
28
|
+
"main": "./dist/index.js",
|
|
29
|
+
"module": "./dist/index.mjs",
|
|
30
30
|
"types": "./dist/index.d.ts",
|
|
31
31
|
"exports": {
|
|
32
32
|
".": {
|
|
33
33
|
"import": {
|
|
34
|
-
"types": "./dist/index.d.
|
|
35
|
-
"default": "./dist/index.
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"default": "./dist/index.mjs"
|
|
36
36
|
},
|
|
37
37
|
"require": {
|
|
38
|
-
"types": "./dist/index.d.
|
|
39
|
-
"default": "./dist/index.
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
42
|
},
|