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.
Files changed (2) hide show
  1. package/README.md +69 -106
  2. package/package.json +11 -11
package/README.md CHANGED
@@ -1,157 +1,120 @@
1
1
  # pretty-logx
2
2
 
3
- A lightweight, beautiful, TypeScript-first logging library for Node.js.
4
-
5
- [![npm version](https://img.shields.io/npm/v/pretty-logx.svg)](https://www.npmjs.com/package/pretty-logx)
6
- [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
7
- [![Node.js >= 18](https://img.shields.io/badge/node-%3E%3D18-brightgreen)](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
- ## Quick Start
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({ level: 'debug', namespace: 'app' });
20
+ const log = createLogger();
38
21
 
39
- log.info('Server started', { port: 3000 });
40
- log.debug({ query: 'SELECT * FROM users', duration: '12ms' }, 'DB query');
41
- log.error({ err: new Error('Connection failed') }, 'Database 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
- ## API
45
-
46
- ### `createLogger(config?)`
27
+ ### Context & Child Loggers
47
28
 
48
- Creates a new logger instance.
29
+ Pass request metadata or create scoped child loggers:
49
30
 
50
31
  ```ts
51
- const log = createLogger({
52
- level: 'info', // Minimum log level (default: 'info')
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
- ### Log Methods
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
- All log methods accept two calling conventions:
41
+ ### Production JSON Mode
65
42
 
66
43
  ```ts
67
- // Message only
68
- log.info('Hello world');
69
-
70
- // Message + extra data
71
- log.info('Request complete', { status: 200 });
44
+ const log = createLogger({
45
+ level: 'info',
46
+ json: process.env.NODE_ENV === 'production',
47
+ namespace: 'api',
48
+ });
72
49
 
73
- // Bindings object first (recommended for structured logging)
74
- log.info({ requestId: 'abc', userId: 42 }, 'User logged in');
50
+ log.info({ path: '/api/v1/users', status: 200 }, 'Request completed');
51
+ ```
75
52
 
76
- // Error logging
77
- log.error({ err: new Error('oops') }, 'Something failed');
78
- log.error('Request failed', new Error('Timeout'));
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
- Available methods: `trace`, `debug`, `info`, `warn`, `error`, `fatal`
58
+ ### Error Handling
82
59
 
83
- ### Level Predicates
60
+ Pass standard errors and get formatted stack traces in dev, or serialized error objects in JSON mode:
84
61
 
85
62
  ```ts
86
- if (log.isDebugEnabled) {
87
- log.debug('Expensive debug info', computeDebugInfo());
63
+ try {
64
+ throw new Error('Something broke');
65
+ } catch (err) {
66
+ log.error('Operation failed', err);
88
67
  }
89
68
  ```
90
69
 
91
- Predicates: `isTraceEnabled`, `isDebugEnabled`, `isInfoEnabled`, `isWarnEnabled`, `isErrorEnabled`, `isFatalEnabled`
70
+ ### Circular Objects
92
71
 
93
- ### Child Loggers
72
+ Circular references are caught and serialized as `[Circular]` instead of crashing your app:
94
73
 
95
74
  ```ts
96
- const reqLog = log.child({ requestId: 'req-123' });
97
- reqLog.info('Processing'); // Includes requestId automatically
75
+ const obj: any = { name: 'app' };
76
+ obj.self = obj;
98
77
 
99
- // Override config in child
100
- const quietChild = log.child({}, { level: 'warn' });
78
+ log.info({ config: obj }, 'Loaded configuration');
101
79
  ```
102
80
 
103
- ### JSON Mode (Production)
81
+ ## Options
104
82
 
105
- ```ts
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
- ```ts
114
- log.level = 'debug'; // Change level
115
- log.configure({ json: true, level: 'warn' }); // Multiple options at once
116
- ```
117
-
118
- ### Color Control
119
-
120
- ```ts
121
- import { setColorsEnabled } from 'pretty-logx';
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
- | Level | Value | Use Case |
132
- |---------|-------|---------------------------------|
133
- | `trace` | 10 | Extremely detailed debugging |
134
- | `debug` | 20 | Debug information |
135
- | `info` | 30 | Normal operational messages |
136
- | `warn` | 40 | Warnings, recoverable issues |
137
- | `error` | 50 | Errors, failures |
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
- Full TypeScript support with strict types:
105
+ Check active levels before running expensive operations:
144
106
 
145
107
  ```ts
146
- import type { LoggerConfig, LogLevelName, LogRecord } from 'pretty-logx';
108
+ if (log.isDebugEnabled) {
109
+ log.debug('Debug payload', getExpensiveMetrics());
110
+ }
147
111
  ```
148
112
 
149
- ## CommonJS Support
113
+ ## Environment Flags
150
114
 
151
- ```js
152
- const { createLogger } = require('pretty-logx');
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
- [MIT](LICENSE) © 2024
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.0",
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/your-username/pretty-logx.git"
19
+ "url": "git+https://github.com/rayrudr1817/pretty-logx.git"
20
20
  },
21
- "homepage": "https://github.com/your-username/pretty-logx#readme",
21
+ "homepage": "https://github.com/rayrudr1817/pretty-logx#readme",
22
22
  "bugs": {
23
- "url": "https://github.com/your-username/pretty-logx/issues"
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.cjs",
29
- "module": "./dist/index.js",
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.ts",
35
- "default": "./dist/index.js"
34
+ "types": "./dist/index.d.mts",
35
+ "default": "./dist/index.mjs"
36
36
  },
37
37
  "require": {
38
- "types": "./dist/index.d.cts",
39
- "default": "./dist/index.cjs"
38
+ "types": "./dist/index.d.ts",
39
+ "default": "./dist/index.js"
40
40
  }
41
41
  }
42
42
  },