Skip to content

Commit 503cc94

Browse files
committed
feat: custom log formatting
1 parent a109cfa commit 503cc94

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,9 @@ This will start a web server where you can:
210210
- View available tools and resources
211211
- Test tool calls interactively
212212
- See server logs and responses
213+
214+
## Log Formatting (Optional)
215+
216+
The MCP server supports custom log formatting through the `LOG_FORMAT` environment variable. This allows you to control the format of log messages output by the server.
217+
**Example Format Strings:** `"%(asctime)s %(levelname)s [%(name)s] %(message)s"`.
218+
If `LOG_FORMAT` is not set, the server uses FastMCP's default logging configuration.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import logging
2+
import os
3+
import sys
4+
5+
6+
def configure_logging() -> None:
7+
"""Configure logging based on LOG_FORMAT environment variable.
8+
9+
If LOG_FORMAT is set, configures the root logger with a StreamHandler
10+
using the specified format string. This must be called before FastMCP
11+
initialization to ensure our configuration takes precedence.
12+
13+
If LOG_FORMAT is not set or is empty, does nothing and lets FastMCP
14+
configure logging with its default settings.
15+
16+
Environment variables:
17+
LOG_FORMAT: Python logging format string (optional)
18+
Examples:
19+
- "%(asctime)s agent %(levelname)s [%(name)s] %(message)s"
20+
- "%(levelname)s: %(message)s"
21+
- "[%(name)s] %(message)s"
22+
23+
Raises:
24+
ValueError: If LOG_FORMAT contains an invalid format string
25+
"""
26+
log_format = os.getenv("LOG_FORMAT", "").strip()
27+
28+
# If LOG_FORMAT is not set or empty, do nothing
29+
if not log_format:
30+
return
31+
32+
# Create handler for stderr (same as FastMCP default)
33+
handler = logging.StreamHandler(sys.stderr)
34+
35+
# Create formatter with the specified format string
36+
# This will raise an error if the format string is invalid (fail fast)
37+
formatter = logging.Formatter(log_format)
38+
handler.setFormatter(formatter)
39+
40+
# Configure root logger
41+
# This must be done before FastMCP calls logging.basicConfig()
42+
logging.root.addHandler(handler)
43+
logging.root.setLevel(logging.INFO)

src/codeocean_mcp_server/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from codeocean import CodeOcean
44
from mcp.server.fastmcp import FastMCP
55

6+
from codeocean_mcp_server.logging_config import configure_logging
67
from codeocean_mcp_server.tools import (
78
capsules,
89
computations,
@@ -13,6 +14,7 @@
1314

1415
def main():
1516
"""Run the MCP server."""
17+
configure_logging()
1618
domain = os.getenv("CODEOCEAN_DOMAIN")
1719
token = os.getenv("CODEOCEAN_TOKEN")
1820
if not domain or not token:

0 commit comments

Comments
 (0)