The logging_config.py module#

Summary#

setup_logging

Configure logging for MCP servers.

get_logger

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 LOGLEVEL environment variable or the level parameter.

Parameters:
levelstr, default: None

Log level. Options are "DEBUG", "INFO", "WARNING", "ERROR" and, "CRITICAL". In None, the LOGLEVEL environment variable is used or it defaults to "INFO".

log_filestr, default: None

Path to the log file. If a path is provided, logs are written to both stderr and the specified file.

format_stringstr, default: None

Custom format string for log messages. If None, the default format is used.

Returns:
logging.Logger

Root 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 LOGLEVEL environment 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. If setup_logging() hasn’t been called, it is called with default settings.

Parameters:
namestr

Logger name (typically __name__ of the calling module).

Returns:
logging.Logger

Logger instance.

Examples

>>> from ansys.common.mcp.logging_config import get_logger
>>> logger = get_logger(__name__)
>>> logger.info("Processing request...")