Skip to content

Commit b2f8b73

Browse files
committed
feat: implement log level control via CLI flags
Introduces a log level system (ERROR, WARN, INFO, DEBUG) to control application logging verbosity. - Added `LogLevel` enum and `setLogLevel` function to `src/utils/logger.ts`. - Modified logger methods to respect the current log level. - Added global `--verbose` (DEBUG level) and `--silent` (ERROR level) flags to the CLI (`src/cli.ts`). - Used the `preAction` hook in `commander` to set the log level based on CLI flags before command execution. - Set the default log level for the MCP server (`src/mcp/index.ts`) to `ERROR`.
1 parent 1ff5d78 commit b2f8b73

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

src/cli.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/usr/bin/env node
22
import "dotenv/config";
33
import { Command } from "commander";
4-
import { PipelineManager } from "./pipeline/PipelineManager"; // Import PipelineManager
4+
import packageJson from "../package.json";
5+
import { PipelineManager } from "./pipeline/PipelineManager";
56
import { DocumentManagementService } from "./store/DocumentManagementService";
67
import { FindVersionTool, ListLibrariesTool, ScrapeTool, SearchTool } from "./tools";
8+
import { LogLevel, setLogLevel } from "./utils/logger";
79

810
const formatOutput = (data: unknown) => JSON.stringify(data, null, 2);
911

@@ -39,7 +41,10 @@ async function main() {
3941
program
4042
.name("docs-mcp")
4143
.description("CLI for managing documentation vector store")
42-
.version("1.0.0");
44+
.version(packageJson.version)
45+
// Add global options for logging level
46+
.option("--verbose", "Enable verbose (debug) logging", false)
47+
.option("--silent", "Disable all logging except errors", false);
4348

4449
program
4550
.command("scrape <library> <url>") // Remove <version> as positional
@@ -160,6 +165,19 @@ async function main() {
160165
}
161166
});
162167

168+
// Hook to set log level after parsing global options but before executing command action
169+
program.hook("preAction", (thisCommand) => {
170+
// Global options are attached to the program (thisCommand)
171+
const options = thisCommand.opts();
172+
if (options.silent) {
173+
// If silent is true, it overrides verbose
174+
setLogLevel(LogLevel.ERROR);
175+
} else if (options.verbose) {
176+
setLogLevel(LogLevel.DEBUG);
177+
}
178+
// Otherwise, the default LogLevel.INFO remains set from logger.ts
179+
});
180+
163181
await program.parseAsync();
164182
} catch (error) {
165183
console.error("Error:", error instanceof Error ? error.message : String(error));

src/mcp/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ import {
1717
SearchTool,
1818
VersionNotFoundError,
1919
} from "../tools";
20-
import { logger } from "../utils/logger";
20+
import { LogLevel, logger, setLogLevel } from "../utils/logger"; // Import LogLevel and setLogLevel
2121
import { createError, createResponse } from "./utils";
2222

2323
export async function startServer() {
24-
// Disable debug logs
25-
// FIXME: This is a temporary fix. We should use a proper logging library with levels.
26-
logger.debug = () => {};
27-
logger.info = () => {};
24+
// Set the default log level for the server to ERROR
25+
setLogLevel(LogLevel.ERROR);
2826

2927
const docService = new DocumentManagementService();
3028

src/utils/logger.ts

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,61 @@
1+
/**
2+
* Defines the available log levels.
3+
*/
4+
export enum LogLevel {
5+
ERROR = 0,
6+
WARN = 1,
7+
INFO = 2,
8+
DEBUG = 3,
9+
}
10+
11+
let currentLogLevel: LogLevel = LogLevel.INFO; // Default level
12+
13+
/**
14+
* Sets the current logging level for the application.
15+
* @param level - The desired log level.
16+
*/
17+
export function setLogLevel(level: LogLevel): void {
18+
currentLogLevel = level;
19+
}
20+
21+
/**
22+
* Provides logging functionalities with level control.
23+
*/
124
export const logger = {
2-
debug: (message: string) => console.debug(message),
3-
info: (message: string) => console.log(message),
4-
warn: (message: string) => console.warn(message),
5-
error: (message: string) => console.error(message),
25+
/**
26+
* Logs a debug message if the current log level is DEBUG or higher.
27+
* @param message - The message to log.
28+
*/
29+
debug: (message: string) => {
30+
if (currentLogLevel >= LogLevel.DEBUG) {
31+
console.debug(message);
32+
}
33+
},
34+
/**
35+
* Logs an info message if the current log level is INFO or higher.
36+
* @param message - The message to log.
37+
*/
38+
info: (message: string) => {
39+
if (currentLogLevel >= LogLevel.INFO) {
40+
console.log(message); // Using console.log for INFO
41+
}
42+
},
43+
/**
44+
* Logs a warning message if the current log level is WARN or higher.
45+
* @param message - The message to log.
46+
*/
47+
warn: (message: string) => {
48+
if (currentLogLevel >= LogLevel.WARN) {
49+
console.warn(message);
50+
}
51+
},
52+
/**
53+
* Logs an error message if the current log level is ERROR or higher (always logs).
54+
* @param message - The message to log.
55+
*/
56+
error: (message: string) => {
57+
if (currentLogLevel >= LogLevel.ERROR) {
58+
console.error(message);
59+
}
60+
},
661
};

0 commit comments

Comments
 (0)