smart-logging 1.0.0 → 1.0.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 +83 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,2 +1,84 @@
|
|
|
1
1
|
# smart-logging
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Zero-setup console interception for Node.js. Install it, import it once at startup, and every `console.log`, `console.error`, etc. automatically routes through [Pino](https://github.com/pinojs/pino) with pretty output, timestamps, and file/line metadata. No manual logger wiring required.
|
|
4
|
+
|
|
5
|
+
Optional tag helpers (`addTag`, `clearTags`) let you stamp logs with request IDs or any metadata you need.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install smart-logging
|
|
11
|
+
# or
|
|
12
|
+
yarn add smart-logging
|
|
13
|
+
# or
|
|
14
|
+
pnpm add smart-logging
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### ESM
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
import 'smart-logging';
|
|
23
|
+
// Optional helpers
|
|
24
|
+
import { addTag, clearTags } from 'smart-logging';
|
|
25
|
+
|
|
26
|
+
addTag({ requestId: 'req-123' });
|
|
27
|
+
console.log('This will include the requestId, timestamp, and file info');
|
|
28
|
+
clearTags();
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### CommonJS
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
require('smart-logging');
|
|
35
|
+
const { addTag, clearTags } = require('smart-logging');
|
|
36
|
+
|
|
37
|
+
addTag({ userId: 'user-42' });
|
|
38
|
+
console.error(new Error('Oops!'));
|
|
39
|
+
clearTags();
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
That single import is all you need—every built-in console method is monkey-patched once so your app logs are structured and pretty everywhere.
|
|
43
|
+
|
|
44
|
+
## Features
|
|
45
|
+
|
|
46
|
+
- Automatic console interception (`log`, `info`, `warn`, `error`, `debug`)
|
|
47
|
+
- Pretty printed output (via `pino-pretty`) with timestamps and file:line
|
|
48
|
+
- Async context support using `AsyncLocalStorage`
|
|
49
|
+
- `addTag(metadata)` to attach request IDs, user IDs, feature flags, etc.
|
|
50
|
+
- `clearTags()` to reset metadata when a request ends
|
|
51
|
+
- Works with both ESM `import` and CommonJS `require`
|
|
52
|
+
|
|
53
|
+
## Tags / Metadata
|
|
54
|
+
|
|
55
|
+
Use `addTag` to merge metadata into the current async context. Values must be strings, numbers, booleans, or `null`; anything else throws.
|
|
56
|
+
|
|
57
|
+
```javascript
|
|
58
|
+
addTag({ requestId: 'req-9000', plan: 'enterprise' });
|
|
59
|
+
console.info('Handling billing webhook');
|
|
60
|
+
|
|
61
|
+
addTag({ featureFlag: 'beta-mode' }); // merges into existing tags
|
|
62
|
+
console.warn('Customer enabled beta mode');
|
|
63
|
+
|
|
64
|
+
clearTags(); // resets the context for the next request
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Tags are appended to each log line as `key: value`, making it easy to grep or pipe into log aggregators.
|
|
68
|
+
|
|
69
|
+
## Error Handling
|
|
70
|
+
|
|
71
|
+
Unhandled errors retain their stack trace and are sent through Pino with context attached. The package also registers listeners for:
|
|
72
|
+
|
|
73
|
+
- `uncaughtException`
|
|
74
|
+
- `unhandledRejection`
|
|
75
|
+
- `SIGINT` (Ctrl+C)
|
|
76
|
+
- `process exit`
|
|
77
|
+
|
|
78
|
+
## Contributing
|
|
79
|
+
|
|
80
|
+
PRs and issues are welcome! If you’d like to extend the logger or add integrations, open a discussion first so we can align on direction.
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
[ISC](./LICENSE)
|