The logging_config.py module#
Summary#
Configure logging for MCP servers. |
|
Get a logger instance with the specified name. |
Description#
Logging configuration for PyAnsys MCP servers.
This module provides centralized logging configuration that ensures log messages are properly routed to stderr (not stdout) to avoid interfering with the MCP protocol on stdio transport.
Module detail#
- logging_config.setup_logging(level: str | None = None, log_file: str | None = None, format_string: str | None = None) logging.Logger#
Configure logging for MCP servers.
This method sets up logging to stderr (to avoid interfering with MCP protocol on stdout) and optionally to a file. You can control the log level using the
LOGLEVELenvironment variable or thelevelparameter.- Parameters:
- level
str, default:None Log level. Options are
"DEBUG","INFO","WARNING","ERROR"and,"CRITICAL". InNone, theLOGLEVELenvironment variable is used or it defaults to"INFO".- log_file
str, default:None Path to the log file. If a path is provided, logs are written to both stderr and the specified file.
- format_string
str, default:None Custom format string for log messages. If
None, the default format is used.
- level
- Returns:
logging.LoggerRoot logger instance.
Notes
Logs are sent to stderr, NOT stdout. stdout is reserved for MCP protocol.
If logs went to stdout, it would break the MCP protocol and cause client communication to fail.
The
LOGLEVELenvironment variable can be used to set the log level.The root logger is configured, so all loggers in your application use this configuration.
Examples
Basic setup (logs to stderr):
>>> from ansys.common.mcp.logging_config import setup_logging >>> logger = setup_logging() >>> logger.info("Server starting...")
With file output:
>>> logger = setup_logging(level="DEBUG", log_file="server.log")
Using environment variable:
>>> # Set LOGLEVEL=DEBUG before running >>> logger = setup_logging()
- logging_config.get_logger(name: str) logging.Logger#
Get a logger instance with the specified name.
This is a convenience wrapper around
logging.getLogger()that ensures logging has been configured. Ifsetup_logging()hasn’t been called, it is called with default settings.- Parameters:
- name
str Logger name (typically __name__ of the calling module).
- name
- Returns:
logging.LoggerLogger instance.
Examples
>>> from ansys.common.mcp.logging_config import get_logger >>> logger = get_logger(__name__) >>> logger.info("Processing request...")