@matheusvcouto/debug 1.0.3 → 1.0.4
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 +124 -3
- package/index.js +1 -1
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -1,11 +1,132 @@
|
|
|
1
1
|
# @matheusvcouto/debug
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A simple, extensible, and type-safe debugging utility for TypeScript/JavaScript applications. It provides categorized logging and flexible control over debug output using tags and environment variables, supporting hierarchical namespaces.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Categorized Logging:** `log`, `warn`, `error`, `info`, `success` methods for different log levels.
|
|
8
|
+
- **Colored Output:** Automatically formats logs with colors for better readability in the console.
|
|
9
|
+
- **Tag-Based Control:** Enable or disable specific debug outputs using descriptive tags.
|
|
10
|
+
- **Hierarchical Namespaces:** Granular control over logs using a colon-separated namespace system (e.g., `API:FETCH` can be controlled by enabling `API`).
|
|
11
|
+
- **Environment Variable Integration:** Control which tags are active via a `DEBUG` environment variable.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
You can install `@matheusvcouto/debug` using your preferred package manager:
|
|
6
16
|
|
|
7
17
|
```bash
|
|
18
|
+
# Using bun
|
|
8
19
|
bun add @matheusvcouto/debug
|
|
9
|
-
|
|
20
|
+
|
|
21
|
+
# Using npm
|
|
10
22
|
npm install @matheusvcouto/debug
|
|
23
|
+
|
|
24
|
+
# Using yarn
|
|
25
|
+
yarn add @matheusvcouto/debug
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Basic Example
|
|
31
|
+
|
|
32
|
+
First, import `createDebug` and define your debug environment variable (e.g., `process.env.DEBUG`).
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
// src/my-module.ts
|
|
36
|
+
import { createDebug } from '@matheusvcouto/debug';
|
|
37
|
+
|
|
38
|
+
// Define known tags for type safety and autocompletion
|
|
39
|
+
const knownTags = ["API", "DB", "AUTH", "SERVICE:USER"] as const;
|
|
40
|
+
|
|
41
|
+
// Initialize the debug utility.
|
|
42
|
+
// The first argument is typically an environment variable (e.g., process.env.DEBUG)
|
|
43
|
+
// The second argument is an array of known tags for type inference and autocompletion.
|
|
44
|
+
const { debug, DEBUG } = createDebug(process.env.DEBUG || "", knownTags);
|
|
45
|
+
|
|
46
|
+
// Create a debugger instance for a specific tag
|
|
47
|
+
const apiDebug = debug("API");
|
|
48
|
+
const userDebug = debug("SERVICE:USER");
|
|
49
|
+
const authDebug = debug("AUTH");
|
|
50
|
+
|
|
51
|
+
apiDebug.log("Starting API call...");
|
|
52
|
+
userDebug.info("Fetching user data for ID:", 123);
|
|
53
|
+
|
|
54
|
+
if (DEBUG.API) { // Check if a tag is enabled for conditional logic
|
|
55
|
+
apiDebug.success("API call completed successfully.");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
authDebug.error("Authentication failed for user 'testuser'.");
|
|
59
|
+
|
|
60
|
+
// Extending a debugger for sub-modules
|
|
61
|
+
const dbDebug = debug("DB");
|
|
62
|
+
const queryDebug = dbDebug.extend("QUERY"); // This creates a debugger for "DB:QUERY"
|
|
63
|
+
|
|
64
|
+
queryDebug.log("Executing complex query...");
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Controlling Debug Output
|
|
68
|
+
|
|
69
|
+
The output of your debug messages is controlled by the `DEBUG` environment variable.
|
|
70
|
+
Separate multiple tags with commas. Use `*` to enable all tags.
|
|
71
|
+
|
|
72
|
+
| `DEBUG` Value | Description | Example Output Enabled |
|
|
73
|
+
| :------------------ | :--------------------------------------------------------- | :------------------------------ |
|
|
74
|
+
| `API` | Enables logs only for the `API` tag. | `API` |
|
|
75
|
+
| `API,AUTH` | Enables logs for `API` and `AUTH` tags. | `API`, `AUTH` |
|
|
76
|
+
| `API:*` | Enables `API` and all its hierarchical children (e.g., `API:FETCH`, `API:AUTH`). | `API`, `API:FETCH`, `API:AUTH` |
|
|
77
|
+
| `*` | Enables all debug tags. | All |
|
|
78
|
+
| (empty or undefined) | Disables all debug output. | None |
|
|
79
|
+
|
|
80
|
+
**Example of running your application with debug enabled:**
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# On Linux/macOS
|
|
84
|
+
DEBUG="API,DB:QUERY" bun run src/index.ts
|
|
85
|
+
|
|
86
|
+
# On Windows (Command Prompt)
|
|
87
|
+
set DEBUG=API,DB:QUERY && bun run src/index.ts
|
|
88
|
+
|
|
89
|
+
# On Windows (PowerShell)
|
|
90
|
+
$env:DEBUG="API,DB:QUERY"; bun run src/index.ts
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### API Reference
|
|
94
|
+
|
|
95
|
+
#### `createDebug(debugEnvVar: string, knownTags: readonly string[]): { debug: DebuggerFactory, DEBUG: TagStatus, isEnabled: (tag: string) => boolean }`
|
|
96
|
+
|
|
97
|
+
- `debugEnvVar`: A string usually sourced from an environment variable (e.g., `process.env.DEBUG`) that dictates which debug tags are enabled.
|
|
98
|
+
- `knownTags`: A `readonly` array of strings representing all known debug tags in your application. This is used for type inference and autocompletion.
|
|
99
|
+
|
|
100
|
+
Returns an object containing:
|
|
101
|
+
- `debug`: A function to create `Debugger` instances for specific tags.
|
|
102
|
+
- `DEBUG`: A proxy object where you can check the enabled status of any tag (e.g., `if (DEBUG.API) { ... }`).
|
|
103
|
+
- `isEnabled`: A utility function to programmatically check if a given tag is enabled.
|
|
104
|
+
|
|
105
|
+
#### `Debugger` Interface
|
|
106
|
+
|
|
107
|
+
Each `Debugger` instance provides the following logging methods:
|
|
108
|
+
|
|
109
|
+
- `log(...args: any[]): void`
|
|
110
|
+
- `warn(...args: any[]): void`
|
|
111
|
+
- `error(...args: any[]): void`
|
|
112
|
+
- `info(...args: any[]): void`
|
|
113
|
+
- `success(...args: any[]): void`
|
|
114
|
+
- `extend(subtag: string): Debugger`: Creates a new `Debugger` instance with an extended tag (e.g., `debug("API").extend("FETCH")` results in `API:FETCH`).
|
|
115
|
+
|
|
116
|
+
## Development
|
|
117
|
+
|
|
118
|
+
This project uses `bun` for development and building.
|
|
119
|
+
|
|
120
|
+
### Building the project
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
bun run build
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
This will compile the TypeScript source code into the `dist` directory and generate declaration files.
|
|
127
|
+
|
|
128
|
+
### Running tests
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
bun test
|
|
11
132
|
```
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var{Deno:k}=globalThis,v=typeof k?.noColor==="boolean"?k.noColor:!1,w=!v;function Q(q,J){return{open:`\x1B[${q.join(";")}m`,close:`\x1B[${J}m`,regexp:new RegExp(`\\x1b\\[${J}m`,"g")}}function W(q,J){return w?`${J.open}${q.replace(J.regexp,J.open)}${J.close}`:q}function C(q){return W(q,Q([1],22))}function G(q){return W(q,Q([31],39))}function I(q){return W(q,Q([32],39))}function L(q){return W(q,Q([33],39))}function M(q){return W(q,Q([34],39))}function _(q){return W(q,Q([36],39))}var h=new RegExp(["[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)","(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TXZcf-nq-uy=><~]))"].join("|"),"g");var B="",F=0;function D(){let q=Date.now();if(q-F>=1000)B=new Date(q).toLocaleTimeString("pt-BR",{hour:"2-digit",minute:"2-digit",second:"2-digit"}),F=q;return B}function P(q,J){function Z(H,K){if(K.has("*"))return!0;let X=H.toUpperCase();if(K.has(X))return!0;let O=X.split(":");for(let z=1;z<O.length;z++){let Y=O.slice(0,z).join(":");if(K.has(Y))return!0}return!1}let $=new Set(q.split(",").map((H)=>H.trim().toUpperCase()).filter(Boolean)),R=new Proxy({},{get:(H,K)=>{if(typeof K!=="string")return!1;return Z(K,$)}});function j(H){let K=Z(H,$),X=(z)=>j(`${H}:${z}`);if(!K)return{log:()=>{},warn:()=>{},error:()=>{},info:()=>{},success:()=>{},extend:X};let O=(z,Y)=>{let S=`[${D()}]`,V=`[${H}]`,N=Y(z);return`${S} ${
|
|
1
|
+
var{Deno:k}=globalThis,v=typeof k?.noColor==="boolean"?k.noColor:!1,w=!v;function Q(q,J){return{open:`\x1B[${q.join(";")}m`,close:`\x1B[${J}m`,regexp:new RegExp(`\\x1b\\[${J}m`,"g")}}function W(q,J){return w?`${J.open}${q.replace(J.regexp,J.open)}${J.close}`:q}function C(q){return W(q,Q([1],22))}function G(q){return W(q,Q([31],39))}function I(q){return W(q,Q([32],39))}function L(q){return W(q,Q([33],39))}function M(q){return W(q,Q([34],39))}function _(q){return W(q,Q([36],39))}var h=new RegExp(["[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)","(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TXZcf-nq-uy=><~]))"].join("|"),"g");var B="",F=0;function D(){let q=Date.now();if(q-F>=1000)B=new Date(q).toLocaleTimeString("pt-BR",{hour:"2-digit",minute:"2-digit",second:"2-digit"}),F=q;return B}function P(q,J){function Z(H,K){if(K.has("*"))return!0;let X=H.toUpperCase();if(K.has(X))return!0;let O=X.split(":");for(let z=1;z<O.length;z++){let Y=O.slice(0,z).join(":");if(K.has(Y))return!0}return!1}let $=new Set(q.split(",").map((H)=>H.trim().toUpperCase()).filter(Boolean)),R=new Proxy({},{get:(H,K)=>{if(typeof K!=="string")return!1;return Z(K,$)}});function j(H){let K=Z(H,$),X=(z)=>j(`${H}:${z}`);if(!K)return{log:()=>{},warn:()=>{},error:()=>{},info:()=>{},success:()=>{},extend:X};let O=(z,Y)=>{let S=`[${D()}]`,V=`[${H}]`,N=Y(z);return`${S} ${N} ${V}`};return{log:(...z)=>console.log(O("log",_),...z),warn:(...z)=>console.warn(O("warn",L),...z),error:(...z)=>console.error(O("error",(Y)=>G(C(Y))),...z),info:(...z)=>console.info(O("info",M),...z),success:(...z)=>console.log(O("success",I),...z),extend:X}}return{DEBUG:R,debug:j,isEnabled:(H)=>Z(H,$)}}export{P as createDebug};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@matheusvcouto/debug",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "no description",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"author": "Matheus Couto",
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"repository": "",
|
|
29
|
+
"readme": "./README.md",
|
|
29
30
|
"peerDependencies": {
|
|
30
31
|
"typescript": "^5.7.0"
|
|
31
32
|
}
|