voltlog-io 1.0.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/CODE_OF_CONDUCT.md +41 -0
- package/CONTRIBUTING.md +112 -0
- package/LICENSE +21 -0
- package/README.md +372 -0
- package/dist/index.d.mts +707 -0
- package/dist/index.d.ts +707 -0
- package/dist/index.js +782 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +738 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +60 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
## Our Standards
|
|
8
|
+
|
|
9
|
+
Examples of behavior that contributes to a positive environment include:
|
|
10
|
+
|
|
11
|
+
- Using welcoming and inclusive language
|
|
12
|
+
- Being respectful of differing viewpoints and experiences
|
|
13
|
+
- Gracefully accepting constructive criticism
|
|
14
|
+
- Focusing on what is best for the community
|
|
15
|
+
- Showing empathy towards other community members
|
|
16
|
+
|
|
17
|
+
Examples of unacceptable behavior by participants include:
|
|
18
|
+
|
|
19
|
+
- The use of sexualized language or imagery and unwelcome sexual attention or advances
|
|
20
|
+
- Trolling, insulting/derogatory comments, and personal or political attacks
|
|
21
|
+
- Public or private harassment
|
|
22
|
+
- Publishing others' private information, such as a physical or electronic address, without explicit permission
|
|
23
|
+
- Other conduct which could reasonably be considered inappropriate in a professional setting
|
|
24
|
+
|
|
25
|
+
## Our Responsibilities
|
|
26
|
+
|
|
27
|
+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
|
28
|
+
|
|
29
|
+
## Scope
|
|
30
|
+
|
|
31
|
+
This Code of Conduct applies within all project spaces, and it also applies when an individual is representing the project or its community in public spaces. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
|
32
|
+
|
|
33
|
+
## Enforcement
|
|
34
|
+
|
|
35
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at <rohit@rohittiwari.me>. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
|
36
|
+
|
|
37
|
+
## Attribution
|
|
38
|
+
|
|
39
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
|
40
|
+
|
|
41
|
+
[homepage]: https://www.contributor-covenant.org
|
package/CONTRIBUTING.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# Contributing to VoltLog
|
|
2
|
+
|
|
3
|
+
First off, thanks for taking the time to contribute! ❤️
|
|
4
|
+
|
|
5
|
+
All types of contributions are encouraged and valued. See the [Table of Contents](#table-of-contents) for different ways to help and details about how this project handles them. Please make sure to read the relevant section before making your contribution. It will make it a lot easier for us maintainers and smooth out the experience for all involved. The community looks forward to your contributions.
|
|
6
|
+
|
|
7
|
+
> And if you like the project, but just don't have time to contribute, that's fine. There are other easy ways to support the project and show your appreciation, which we would also be very happy about:
|
|
8
|
+
>
|
|
9
|
+
> - Star the project
|
|
10
|
+
> - Tweet about it
|
|
11
|
+
> - Refer this project in your project's readme
|
|
12
|
+
> - Mention the project at local meetups and tell your friends/colleagues
|
|
13
|
+
|
|
14
|
+
## Table of Contents
|
|
15
|
+
|
|
16
|
+
- [Code of Conduct](#code-of-conduct)
|
|
17
|
+
- [I Have a Question](#i-have-a-question)
|
|
18
|
+
- [I Want To Contribute](#i-want-to-contribute)
|
|
19
|
+
- [Reporting Bugs](#reporting-bugs)
|
|
20
|
+
- [Suggesting Enhancements](#suggesting-enhancements)
|
|
21
|
+
- [Your First Code Contribution](#your-first-code-contribution)
|
|
22
|
+
|
|
23
|
+
## Code of Conduct
|
|
24
|
+
|
|
25
|
+
This project and everyone participating in it is governed by the
|
|
26
|
+
[VoltLog Code of Conduct](CODE_OF_CONDUCT.md).
|
|
27
|
+
By participating, you are expected to uphold this code. Please report unacceptable behavior
|
|
28
|
+
to <rohit@rohittiwari.me>.
|
|
29
|
+
|
|
30
|
+
## I Have a Question
|
|
31
|
+
|
|
32
|
+
> If you want to ask a question, we assume that you have read the available [Documentation](https://github.com/rohittiwari-dev/voltlog-io).
|
|
33
|
+
|
|
34
|
+
Before you ask a question, it is best to search for existing [Issues](https://github.com/rohittiwari-dev/voltlog-io/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
|
|
35
|
+
|
|
36
|
+
If you then still feel the need to ask a question and need clarification, we recommend the following:
|
|
37
|
+
|
|
38
|
+
- Open an [Issue](https://github.com/rohittiwari-dev/voltlog-io/issues/new).
|
|
39
|
+
- Provide as much context as you can about what you're running into.
|
|
40
|
+
- Provide project and platform versions (nodejs, npm, etc), depending on what seems relevant.
|
|
41
|
+
|
|
42
|
+
We will then take care of the issue as soon as possible.
|
|
43
|
+
|
|
44
|
+
## I Want To Contribute
|
|
45
|
+
|
|
46
|
+
> ### Legal Notice
|
|
47
|
+
>
|
|
48
|
+
> When contributing to this project, you must agree that you have authored the content, that you have the rights to the content and that the content you contribute may be provided under the project license.
|
|
49
|
+
|
|
50
|
+
### Reporting Bugs
|
|
51
|
+
|
|
52
|
+
#### Before Submitting a Bug Report
|
|
53
|
+
|
|
54
|
+
A good bug report shouldn't leave others needing to chase you up for more information. Therefore, we ask you to investigate carefully, collect information and describe the issue in detail in your report. Please complete the following steps in advance to help us fix any potential bug as fast as possible.
|
|
55
|
+
|
|
56
|
+
- Make sure that you are using the latest version.
|
|
57
|
+
- Determine if your bug is really a bug and not an error on your side e.g. using incompatible environment components/versions (Make sure that you have read the [documentation](https://github.com/rohittiwari-dev/voltlog-io). If you are looking for support, you might want to check [this section](#i-have-a-question)).
|
|
58
|
+
- To see if other users have experienced (and potentially already solved) the same issue, check if there is not already a bug report existing for your bug or error in the [bug tracker](https://github.com/rohittiwari-dev/voltlog-io/issues?q=label%3Abug).
|
|
59
|
+
- Also make sure to search the internet (including Stack Overflow) to see if users outside of the GitHub community have discussed the issue.
|
|
60
|
+
- Collect information about the bug:
|
|
61
|
+
- Stack trace (Traceback)
|
|
62
|
+
- OS, Platform and Version (Windows, Linux, macOS, x86, ARM)
|
|
63
|
+
- Version of the interpreter, compiler, SDK, runtime environment, package manager, depending on what seems relevant.
|
|
64
|
+
- Possibly your input and the output
|
|
65
|
+
- Can you reliably reproduce the issue? And can you also reproduce it with older versions?
|
|
66
|
+
|
|
67
|
+
#### How Do I Submit a Good Bug Report?
|
|
68
|
+
|
|
69
|
+
> You must never report security related issues, vulnerabilities or bugs including sensitive information to the issue tracker, or elsewhere in public. Instead sensitive bugs must be sent by email to <rohit@rohittiwari.me>.
|
|
70
|
+
|
|
71
|
+
We use GitHub issues to track bugs and errors. If you run into an issue with the project:
|
|
72
|
+
|
|
73
|
+
- Open an [Issue](https://github.com/rohittiwari-dev/voltlog-io/issues/new). (Since we can't be sure the third-party aid isn't calling home or doing something evil check the origin first)
|
|
74
|
+
- Explain the behavior you would expect and the actual behavior.
|
|
75
|
+
- Please provide as much context as possible and describe the _reproduction steps_ that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case.
|
|
76
|
+
- Provide the information you collected in the previous section.
|
|
77
|
+
|
|
78
|
+
Once it's filed:
|
|
79
|
+
|
|
80
|
+
- The project team will label the issue accordingly.
|
|
81
|
+
- A team member will try to reproduce the issue with your provided steps. If there are no reproduction steps or no obvious way to reproduce the issue, the team will ask you for those steps and mark the issue as `needs-repro`. Bugs with the `needs-repro` tag will not be addressed until they are reproduced.
|
|
82
|
+
- If the team is able to reproduce the issue, it will be marked `needs-fix`, as well as possibly other tags (such as `critical`), and the issue will be left to be [implemented by someone](#your-first-code-contribution).
|
|
83
|
+
|
|
84
|
+
### Suggesting Enhancements
|
|
85
|
+
|
|
86
|
+
This section guides you through submitting an enhancement suggestion for VoltLog, **including completely new features and minor improvements to existing functionality**. Following these guidelines will help maintainers and the community to understand your suggestion and find related suggestions.
|
|
87
|
+
|
|
88
|
+
#### Before Submitting an Enhancement
|
|
89
|
+
|
|
90
|
+
- Make sure that you are using the latest version.
|
|
91
|
+
- Read the [documentation](https://github.com/rohittiwari-dev/voltlog-io) carefully and find out if the functionality is already covered, maybe by an individual configuration.
|
|
92
|
+
- Perform a [search](https://github.com/rohittiwari-dev/voltlog-io/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
|
|
93
|
+
- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library.
|
|
94
|
+
|
|
95
|
+
#### How Do I Submit a Good Enhancement Suggestion?
|
|
96
|
+
|
|
97
|
+
Enhancement suggestions are tracked as [GitHub issues](https://github.com/rohittiwari-dev/voltlog-io/issues).
|
|
98
|
+
|
|
99
|
+
- Use a **clear and descriptive title** for the issue to identify the suggestion.
|
|
100
|
+
- Provide a **step-by-step description of the suggested enhancement** in as many details as possible.
|
|
101
|
+
- **Describe the current behavior** and **explain which behavior you expected to see instead** and why. At this point you can also tell which alternatives do not work for you.
|
|
102
|
+
- You may want to include **screenshots and animated GIFs** which help you demonstrate the steps or point out the part which the suggestion is related to.
|
|
103
|
+
- **Explain why this enhancement would be useful** to most users. You may also want to point out the other projects that solved it better and which could serve as inspiration.
|
|
104
|
+
|
|
105
|
+
### Your First Code Contribution
|
|
106
|
+
|
|
107
|
+
1. Fork the repo and create your branch from `main`.
|
|
108
|
+
2. Install dependencies: `npm install`.
|
|
109
|
+
3. Make your changes.
|
|
110
|
+
4. Run tests: `npm test`.
|
|
111
|
+
5. Ensure your code lints: `npm run lint`.
|
|
112
|
+
6. Submit a Pull Request!
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Rohit Tiwari
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
# VoltLog
|
|
2
|
+
|
|
3
|
+
#### Structured logger for real-time infrastructure
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/voltlog-io)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
[](https://github.com/rohittiwari-dev/voltlog-io/actions)
|
|
8
|
+
|
|
9
|
+
**VoltLog** is a modern, lightweight, and type-safe structured logger designed specifically for high-throughput, real-time systems like IoT platforms, WebSocket servers (OCPP), and microservices.
|
|
10
|
+
|
|
11
|
+
It’s built to solve the common pain points of generic loggers in event-driven architectures: context tracking, sensitive data redaction, high-volume sampling, and developer experience.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ✨ Features
|
|
16
|
+
|
|
17
|
+
- **🚀 Zero-Dependency Core**: Only uses `cuid2` for IDs — no massive dependency tree.
|
|
18
|
+
- **🔒 Secure by Design**: Built-in redaction middleware for passwords, tokens, and headers.
|
|
19
|
+
- **📊 High-Throughput Ready**: Intelligent sampling middleware to log 1% of debug logs while keeping 100% of errors.
|
|
20
|
+
- **⚡ Developer Experience**: Beautiful, icon-rich pretty printing for local development.
|
|
21
|
+
- **🔌 Framework Agnostic**: Works in Node.js, Bun, Deno, and Browsers.
|
|
22
|
+
- **🧩 Pluggable Architecture**: Middleware and Transformers let you build custom pipelines easily.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 📦 Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install voltlog-io
|
|
30
|
+
# or
|
|
31
|
+
bun add voltlog-io
|
|
32
|
+
pnpm add voltlog-io
|
|
33
|
+
yarn add voltlog-io
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
---
|
|
37
|
+
|
|
38
|
+
## 🚀 Quick Start
|
|
39
|
+
|
|
40
|
+
### Basic Usage
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { createLogger, consoleTransport } from "voltlog-io";
|
|
44
|
+
|
|
45
|
+
const logger = createLogger({
|
|
46
|
+
level: "INFO",
|
|
47
|
+
transports: [consoleTransport()],
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
logger.info("Server started", { port: 3000, env: "production" });
|
|
51
|
+
// {"level":30,"message":"Server started","meta":{"port":3000,"env":"production"},"timestamp":1700000000000}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Local Development (Pretty Print)
|
|
55
|
+
|
|
56
|
+
Use `prettyTransport` to get readable, colored logs with icons during development.
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { createLogger, prettyTransport } from "voltlog-io";
|
|
60
|
+
|
|
61
|
+
const logger = createLogger({
|
|
62
|
+
level: "DEBUG",
|
|
63
|
+
transports: [prettyTransport({ colorize: true })],
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
logger.info("Server listening", { port: 3000 });
|
|
67
|
+
// ℹ 10:30:00.123 INFO Server listening {"port":3000}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## 💡 Key Concepts & Use Cases
|
|
73
|
+
|
|
74
|
+
### 1. Context Binding (Child Loggers)
|
|
75
|
+
|
|
76
|
+
Perfect for tracking requests, WebSocket connections, or charging sessions. Bind context once, and all subsequent logs inherit it.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
// Global logger
|
|
80
|
+
const logger = createLogger({
|
|
81
|
+
level: "INFO",
|
|
82
|
+
transports: [consoleTransport()],
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
// Create a child logger for a specific connection
|
|
86
|
+
const connLogger = logger.child({
|
|
87
|
+
connectionId: "conn-123",
|
|
88
|
+
ip: "192.168.1.10",
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
connLogger.info("Connected");
|
|
92
|
+
// Logs: message="Connected" + { connectionId: "...", ip: "..." }
|
|
93
|
+
|
|
94
|
+
connLogger.warn("High latency");
|
|
95
|
+
// Logs: message="High latency" + { connectionId: "...", ip: "..." }
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 2. Sensitive Data Redaction
|
|
99
|
+
|
|
100
|
+
Automatically scrub sensitive fields before they hit your logs. Essential for GDPR/security compliance.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
import { createLogger, consoleTransport, redactionMiddleware } from "voltlog-io";
|
|
104
|
+
|
|
105
|
+
const logger = createLogger({
|
|
106
|
+
transports: [consoleTransport()],
|
|
107
|
+
middleware: [
|
|
108
|
+
redactionMiddleware({
|
|
109
|
+
paths: ["password", "token", "headers.authorization", "user.secrets"],
|
|
110
|
+
}),
|
|
111
|
+
],
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
logger.info("User login", {
|
|
115
|
+
username: "alice",
|
|
116
|
+
password: "supersecretpassword",
|
|
117
|
+
});
|
|
118
|
+
// {"message":"User login","meta":{"username":"alice","password":"[REDACTED]"}}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### 3. Sampling (Cost Control)
|
|
122
|
+
|
|
123
|
+
In high-throughput systems (like OCPP servers handling thousands of chargers), `DEBUG` logs can be overwhelming and expensive to store. Use sampling to log only a fraction of them.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { createLogger, consoleTransport, samplingMiddleware } from "voltlog-io";
|
|
127
|
+
|
|
128
|
+
const logger = createLogger({
|
|
129
|
+
level: "DEBUG",
|
|
130
|
+
transports: [consoleTransport()],
|
|
131
|
+
middleware: [
|
|
132
|
+
samplingMiddleware({
|
|
133
|
+
rate: 0.1, // Keep 10% of logs
|
|
134
|
+
level: "DEBUG", // Only sample DEBUG logs (keep 100% of INFO/WARN/ERROR)
|
|
135
|
+
}),
|
|
136
|
+
],
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### 4. Custom Transports
|
|
141
|
+
|
|
142
|
+
Send logs to files, databases, or external services like Datadog/CloudWatch.
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const fileTransport = {
|
|
146
|
+
name: "file",
|
|
147
|
+
transform: (entry) => {
|
|
148
|
+
fs.appendFileSync("app.log", JSON.stringify(entry) + "\n");
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const logger = createLogger({
|
|
153
|
+
transports: [consoleTransport(), fileTransport],
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## 🛠️ Transports
|
|
160
|
+
|
|
161
|
+
Transports determine where your logs go. You can combine multiple transports.
|
|
162
|
+
|
|
163
|
+
### `consoleTransport`
|
|
164
|
+
|
|
165
|
+
Writes logs to `stdout` as single-line JSON. Best for production.
|
|
166
|
+
|
|
167
|
+
```ts
|
|
168
|
+
transports: [consoleTransport({ level: "INFO" })];
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### `prettyTransport`
|
|
172
|
+
|
|
173
|
+
Writes colorful, human-readable logs to `stdout`. Best for local dev.
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
transports: [prettyTransport({ colorize: true, level: "DEBUG" })];
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### `batchTransport`
|
|
180
|
+
|
|
181
|
+
Buffers logs and writes them in chunks. High performance for I/O bound transports.
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
transports: [
|
|
185
|
+
batchTransport({
|
|
186
|
+
batchSize: 100,
|
|
187
|
+
flushIntervalMs: 5000,
|
|
188
|
+
transport: webhookTransport({ url: "..." }),
|
|
189
|
+
}),
|
|
190
|
+
];
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### `jsonStreamTransport`
|
|
194
|
+
|
|
195
|
+
Writes logs as newline-delimited JSON (NDJSON) to a writable stream (like a file).
|
|
196
|
+
|
|
197
|
+
```ts
|
|
198
|
+
import { createWriteStream } from "node:fs";
|
|
199
|
+
const stream = createWriteStream("app.log");
|
|
200
|
+
transports: [jsonStreamTransport({ stream })];
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### `webhookTransport`
|
|
204
|
+
|
|
205
|
+
Sends logs to an external HTTP endpoint (Slack, Discord, Logstash).
|
|
206
|
+
|
|
207
|
+
```ts
|
|
208
|
+
transports: [
|
|
209
|
+
webhookTransport({
|
|
210
|
+
url: "https://hooks.slack.com/services/...",
|
|
211
|
+
batchSize: 10, // Batches requests automatically
|
|
212
|
+
}),
|
|
213
|
+
];
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### `redisTransport`
|
|
217
|
+
|
|
218
|
+
Publishes logs to a Redis channel or pushes to a list/stream.
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
import Redis from "ioredis";
|
|
222
|
+
const redis = new Redis();
|
|
223
|
+
transports: [
|
|
224
|
+
redisTransport({
|
|
225
|
+
client: redis,
|
|
226
|
+
channel: "app-logs",
|
|
227
|
+
mode: "pubsub", // or 'stream' / 'list'
|
|
228
|
+
}),
|
|
229
|
+
];
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## 🛡️ Middleware
|
|
235
|
+
|
|
236
|
+
Middleware transforms, filters, or enriches logs _before_ they reach the transports.
|
|
237
|
+
|
|
238
|
+
### `redactionMiddleware`
|
|
239
|
+
|
|
240
|
+
Removes sensitive fields from log objects. Supports dot notation for nested fields.
|
|
241
|
+
|
|
242
|
+
```ts
|
|
243
|
+
middleware: [
|
|
244
|
+
redactionMiddleware({
|
|
245
|
+
paths: ["password", "token", "user.creditCard", "headers.authorization"],
|
|
246
|
+
censor: "[SECRET]",
|
|
247
|
+
}),
|
|
248
|
+
];
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### `samplingMiddleware`
|
|
252
|
+
|
|
253
|
+
Reduces log volume by only keeping a percentage of logs. Useful for high-volume debug logs.
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
middleware: [
|
|
257
|
+
samplingMiddleware({
|
|
258
|
+
rate: 0.05, // Keep 5%
|
|
259
|
+
level: "DEBUG", // Apply only to DEBUG level (keep all INFO/ERROR)
|
|
260
|
+
}),
|
|
261
|
+
];
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### `alertMiddleware`
|
|
265
|
+
|
|
266
|
+
Trigger callbacks when specific conditions are met (e.g. error rate spike).
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
middleware: [
|
|
270
|
+
alertMiddleware({
|
|
271
|
+
match: (entry) => entry.level >= 50, // Errors
|
|
272
|
+
onAlert: (entry) => sendToSlack(entry),
|
|
273
|
+
throttleMs: 60000, // Only alert once per minute
|
|
274
|
+
}),
|
|
275
|
+
];
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### `ocppMiddleware`
|
|
279
|
+
|
|
280
|
+
Specialized middleware for OCPP messages. Adds directionality and styling when combined with pretty print.
|
|
281
|
+
|
|
282
|
+
```ts
|
|
283
|
+
middleware: [
|
|
284
|
+
ocppMiddleware(), // Adds { direction: 'IN' | 'OUT' } to logs
|
|
285
|
+
];
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
---
|
|
289
|
+
|
|
290
|
+
## 🧩 Custom Middleware
|
|
291
|
+
|
|
292
|
+
You can easily write your own middleware to enrich, filter, or transform logs. Middleware is just a function that takes an `entry` and a `next` callback.
|
|
293
|
+
|
|
294
|
+
### Example: Enriching Logs with Machine Info
|
|
295
|
+
|
|
296
|
+
```ts
|
|
297
|
+
import { os } from "node:os";
|
|
298
|
+
import { createLogger, createMiddleware, consoleTransport } from "voltlog-io";
|
|
299
|
+
|
|
300
|
+
const machineInfoMiddleware = createMiddleware((entry, next) => {
|
|
301
|
+
// Add hostname to every log
|
|
302
|
+
entry.meta = {
|
|
303
|
+
...entry.meta,
|
|
304
|
+
hostname: os.hostname(),
|
|
305
|
+
pid: process.pid,
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
// Continue pipeline
|
|
309
|
+
next(entry);
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
// Usage
|
|
313
|
+
const logger = createLogger({
|
|
314
|
+
middleware: [machineInfoMiddleware],
|
|
315
|
+
transports: [consoleTransport()],
|
|
316
|
+
});
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
### Example: Filtering Logs
|
|
320
|
+
|
|
321
|
+
```ts
|
|
322
|
+
import { createMiddleware } from "voltlog-io";
|
|
323
|
+
|
|
324
|
+
const filterMiddleware = createMiddleware((entry, next) => {
|
|
325
|
+
// Drop logs containing specific text
|
|
326
|
+
if (entry.message.includes("Heartbeat")) {
|
|
327
|
+
return; // Don't call next(), effectively dropping the log
|
|
328
|
+
}
|
|
329
|
+
next(entry);
|
|
330
|
+
};
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
---
|
|
334
|
+
|
|
335
|
+
## 📚 API Reference
|
|
336
|
+
|
|
337
|
+
### `createLogger(config)`
|
|
338
|
+
|
|
339
|
+
Creates a new logger instance.
|
|
340
|
+
|
|
341
|
+
| Config Option | Type | Default | Description |
|
|
342
|
+
| ------------- | --------------- | -------- | ---------------------------------------------------------------------- |
|
|
343
|
+
| `level` | `LogLevelName` | `"INFO"` | Minimum log level (`TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`, `FATAL`) |
|
|
344
|
+
| `transports` | `Transformer[]` | `[]` | Array of output transformers (console, pretty, file, etc.) |
|
|
345
|
+
| `middleware` | `Middleware[]` | `[]` | Array of middleware functions (redaction, sampling, enrichment) |
|
|
346
|
+
|
|
347
|
+
### Log Levels
|
|
348
|
+
|
|
349
|
+
| Level | Value | Usage |
|
|
350
|
+
| ------- | ----- | ----------------------------------------------------------------- |
|
|
351
|
+
| `FATAL` | 60 | System is unusable, requires immediate attention. |
|
|
352
|
+
| `ERROR` | 50 | Error conditions that affect operation but don't stop the system. |
|
|
353
|
+
| `WARN` | 40 | Runtime warnings (deprecated APIs, poor performance). |
|
|
354
|
+
| `INFO` | 30 | Normal operational messages (startup, shutdown). |
|
|
355
|
+
| `DEBUG` | 20 | Detailed info useful for debugging (state changes). |
|
|
356
|
+
| `TRACE` | 10 | Very detailed tracing (function entry/exit, full payloads). |
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
## 🤝 Contributing
|
|
361
|
+
|
|
362
|
+
We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for details.
|
|
363
|
+
|
|
364
|
+
1. Fork the repository
|
|
365
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
366
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
367
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
368
|
+
5. Open a Pull Request
|
|
369
|
+
|
|
370
|
+
## License
|
|
371
|
+
|
|
372
|
+
[MIT](LICENSE) © [Rohit Tiwari](https://github.com/rohittiwari-dev)
|