### Complex Debug Namespace Filtering (Shell)
Source: https://github.com/grammyjs/debug/blob/main/README.md
Provides an example of complex filtering for debug namespaces, where a broad namespace is enabled, a specific one is disabled, and then a more specific sub-namespace of the disabled one is re-enabled. The most specific rule takes precedence.
```shell
# notice that we didn't get app:auth, but we did get app:auth:warning
> DEBUG=app:*,-app:auth,app:auth:warning node index.js
```
--------------------------------
### Enable Multiple Debug Namespaces (Shell)
Source: https://github.com/grammyjs/debug/blob/main/README.md
Illustrates how to enable debug logs for multiple specific namespaces ('app:main' and 'app:auth') by separating them with a comma in the `DEBUG` environment variable.
```shell
> DEBUG=app:main,app:auth node index.js
```
--------------------------------
### Import and Create Debug Logger (TypeScript)
Source: https://github.com/grammyjs/debug/blob/main/README.md
Demonstrates how to import the `createDebug` function and initialize a debug logger instance with a specific namespace. This logger can then be used to log messages.
```typescript
import { createDebug } from "@grammyjs/debug";
```
```typescript
const debug = createDebug("app:main");
debug("Creating new user", { email: req.body.email });
```
```typescript
const debug = createDebug("app:auth");
debug("User authentication failed", { email: req.body.email });
```
--------------------------------
### Create Debug Loggers with Namespaces (TypeScript)
Source: https://context7.com/grammyjs/debug/llms.txt
Demonstrates how to create distinct debug logger instances using namespaces. Each logger is created with `createDebug` and can log strings, objects, and multiple values. Namespaces help organize logs by application module or library component.
```typescript
import { createDebug } from "@grammyjs/debug";
// Create debug loggers for different application modules
const debugMain = createDebug("app:main");
const debugAuth = createDebug("app:auth");
const debugDB = createDebug("app:database");
// Basic logging with strings
debugMain("Application starting...");
// Output: app:main Application starting...
// Logging with objects and additional data
debugAuth("User login attempt", {
email: "user@example.com",
timestamp: Date.now()
});
// Output: app:auth User login attempt { email: 'user@example.com', timestamp: 1699900000000 }
// Logging multiple values
debugDB("Query executed", "SELECT * FROM users", { rows: 42, duration: "15ms" });
// Output: app:database Query executed SELECT * FROM users { rows: 42, duration: '15ms' }
// Library author example - use library name as namespace prefix
const debugGrammy = createDebug("grammy:core");
const debugGrammyAPI = createDebug("grammy:api");
debugGrammy("Bot initialized");
debugGrammyAPI("Sending request to Telegram API", { method: "getUpdates" });
```
--------------------------------
### Enable Debug Logs via Environment Variable (Shell)
Source: https://github.com/grammyjs/debug/blob/main/README.md
Shows how to enable debug logs for a specific namespace ('app:main') by setting the `DEBUG` environment variable in a shell environment before running a Node.js application.
```shell
> DEBUG=app:main node index.js
```
--------------------------------
### Enable All Debug Logs (Shell)
Source: https://github.com/grammyjs/debug/blob/main/README.md
Demonstrates enabling all debug logs across all namespaces by setting the `DEBUG` environment variable to '*'. This will output logs from all debuggers that are active.
```shell
> DEBUG=* node index.js
```
--------------------------------
### Control Debug Output with DEBUG Environment Variable (Bash)
Source: https://context7.com/grammyjs/debug/llms.txt
Illustrates how to use the `DEBUG` environment variable to control which debug namespaces are active. It supports single namespaces, wildcards, comma-separated lists, and negation for fine-grained control over log output. The most specific rule takes precedence.
```bash
# Enable a single namespace
DEBUG=app:main node index.js
# Enable all namespaces under "app"
DEBUG=app:* node index.js
# Enable multiple specific namespaces
DEBUG=app:main,app:auth,server:http node index.js
# Enable all logs
DEBUG=* node index.js
# Enable all "app" namespaces except "app:auth"
DEBUG=app:*,-app:auth node index.js
# Complex pattern: enable app:*, disable app:auth, but re-enable app:auth:warning
DEBUG=app:*,-app:auth,app:auth:warning node index.js
# Result: app:main (enabled), app:auth (disabled), app:auth:warning (enabled)
# Browser usage - set env object before loading scripts
#
#
```
--------------------------------
### Disable Specific Debug Namespace (Shell)
Source: https://github.com/grammyjs/debug/blob/main/README.md
Shows how to enable all logs within a broad namespace ('app:*') but explicitly disable logs from a specific sub-namespace ('app:auth') by prefixing it with a '-'.
```shell
# all "app" enabled except "app:auth"
> DEBUG=app:*,-app:auth node index.js
```
--------------------------------
### Color Utilities for Namespace Labels
Source: https://context7.com/grammyjs/debug/llms.txt
The color utilities provide consistent, visually distinct colors for namespace labels. The `selectColor` function generates a deterministic color based on the namespace string hash, returning an RGB hex number. The `colorNs` function wraps a namespace with ANSI escape codes for terminal output.
```typescript
import { selectColor, colorNs } from "@grammyjs/debug";
// Get a consistent color for a namespace (returns RGB hex number)
const color1 = selectColor("app:main");
console.log(color1.toString(16)); // e.g., "cc0066"
// Same namespace always returns the same color
const color2 = selectColor("app:main");
console.log(color1 === color2); // true
// Different namespaces get different colors
const authColor = selectColor("app:auth");
const dbColor = selectColor("app:database");
console.log(authColor !== dbColor); // likely true
// Create ANSI-colored namespace string for terminal output
const coloredNs = colorNs("app:main");
console.log(coloredNs); // "\x1b[38;2;204;0;102mapp:main\x1b[1;0m"
// Use custom color instead of auto-selected
const customColored = colorNs("app:custom", 0x00ff00);
console.log(customColored); // Green colored "app:custom"
```
--------------------------------
### Programmatically Enable/Disable Debug Loggers (TypeScript)
Source: https://context7.com/grammyjs/debug/llms.txt
Shows how to control the enabled state of individual debug logger instances directly within the code using the `.enabled` property. This allows for dynamic runtime control over logging, overriding environment variable settings, and is useful for features like verbose mode.
```typescript
import { createDebug } from "@grammyjs/debug";
const debug = createDebug("app:feature");
// Check if logging is currently enabled
console.log("Initially enabled:", debug.enabled);
// Force enable regardless of DEBUG environment variable
debug.enabled = true;
debug("This will always log when enabled = true");
// Force disable regardless of DEBUG environment variable
debug.enabled = false;
debug("This will never log when enabled = false");
// Dynamic toggling based on application state
function setVerboseMode(verbose: boolean) {
debug.enabled = verbose;
}
// Usage in an application
const verboseDebug = createDebug("app:verbose");
verboseDebug.enabled = false; // Disabled by default
// Enable verbose logging on user request
function handleUserRequest(req: { verbose?: boolean }) {
if (req.verbose) {
verboseDebug.enabled = true;
}
verboseDebug("Processing request with verbose output", req);
}
```
--------------------------------
### Programmatic Control of Logger Enable State (TypeScript)
Source: https://github.com/grammyjs/debug/blob/main/README.md
Explains how to programmatically enable or disable an individual debug logger instance in TypeScript, overriding the `DEBUG` environment variable settings. This is useful for fine-grained control within the application.
```typescript
const log = createDebug("app:feature");
// Enable this logger regardless of DEBUG environment
log.enabled = true;
// Disable this logger regardless of DEBUG environment
log.enabled = false;
```
--------------------------------
### Namespace Parsing and Matching with Namespaces Class
Source: https://context7.com/grammyjs/debug/llms.txt
The Namespaces class parses and matches debug namespace specifications. It builds an internal tree structure from a DEBUG pattern and provides a `check` method to determine if a specific namespace should be enabled. This class is useful for advanced use cases requiring dynamic pattern updates or custom parsing.
```typescript
import { Namespaces } from "@grammyjs/debug";
// Create a namespace matcher from DEBUG pattern
const namespaces = new Namespaces("app:*,-app:auth,app:auth:warning");
// Check if specific namespaces are enabled
console.log(namespaces.check("app")); // true - matches app:*
console.log(namespaces.check("app:main")); // true - matches app:*
console.log(namespaces.check("app:auth")); // false - explicitly disabled
console.log(namespaces.check("app:auth:warning")); // true - explicitly re-enabled
console.log(namespaces.check("other")); // false - no match
// Update the pattern dynamically
namespaces.update("server:*,db:queries");
console.log(namespaces.check("server:http")); // true
console.log(namespaces.check("db:queries")); // true
console.log(namespaces.check("app:main")); // depends on previous state
// Parse comma or space-separated patterns
const multiPattern = new Namespaces("http:request http:response,cache:*");
console.log(multiPattern.check("http:request")); // true
console.log(multiPattern.check("cache:redis")); // true
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.