-
Notifications
You must be signed in to change notification settings - Fork 44
feat: Add structured logging to MCP server #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |||||
| """ | ||||||
|
|
||||||
| import json | ||||||
| import logging | ||||||
| import random | ||||||
| from pathlib import Path | ||||||
| from typing import Optional, Literal | ||||||
|
|
@@ -29,6 +30,22 @@ | |||||
|
|
||||||
| from fastmcp import FastMCP, Context | ||||||
|
|
||||||
| # Configure logging | ||||||
| LOG_DIR = Path(__file__).parent / "logs" | ||||||
| LOG_DIR.mkdir(exist_ok=True) | ||||||
|
|
||||||
| logging.basicConfig( | ||||||
| level=logging.INFO, | ||||||
| format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||||||
| handlers=[ | ||||||
| logging.FileHandler(LOG_DIR / "mcp_server.log"), | ||||||
| logging.StreamHandler() | ||||||
| ] | ||||||
| ) | ||||||
|
Comment on lines
+37
to
+44
|
||||||
|
|
||||||
| logger = logging.getLogger("copilot_tips_server") | ||||||
| logger.info("Initializing Copilot Tips MCP Server") | ||||||
|
|
||||||
| # Initialize FastMCP server | ||||||
| mcp = FastMCP( | ||||||
| name="Copilot Tips Server", | ||||||
|
|
@@ -41,10 +58,14 @@ | |||||
|
|
||||||
| def load_tips() -> list[dict]: | ||||||
| """Load tips from the JSON data file.""" | ||||||
| logger.debug(f"Loading tips from {DATA_FILE}") | ||||||
|
||||||
| logger.debug(f"Loading tips from {DATA_FILE}") | |
| logger.debug("Loading tips from %s", DATA_FILE) |
Copilot
AI
Jan 14, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using f-strings in log statements creates the formatted string even when the log level would filter it out. Use lazy formatting with logger.info('Tool invoked: get_tip_by_topic(search_term=%r, category=%s, difficulty=%s)', search_term, category, difficulty) to avoid unnecessary string construction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log directory path uses
Path(__file__).parent / 'logs', which places logs in thesrc/directory. Consider using a project root-relative path or making this configurable, as logs are typically stored at the project root or in a dedicated directory outside the source tree.