The logging system in CodeGate (codegate_logging.py
) provides a flexible and
structured logging solution with support for both JSON and text output.
Logs are automatically routed based on their level:
- stdout: INFO and DEBUG messages
- stderr: ERROR, CRITICAL, and WARNING messages
When using JSON format (default), log entries include:
{
"timestamp": "YYYY-MM-DDThh:mm:ss.mmmZ",
"log_level": "LOG_LEVEL",
"module": "MODULE_NAME",
"event": "Log message",
"extra": {
// Additional fields as you desire
}
}
When using text format, log entries follow this pattern:
YYYY-MM-DDThh:mm:ss.mmmZ - LEVEL - NAME - MESSAGE
- Consistent timestamps: ISO-8601 format with millisecond precision in UTC
- Automatic JSON serialization: extra fields are automatically serialized to JSON
- Non-serializable handling: graceful handling of non-serializable values
- Exception support: full exception and stack trace integration
- Dual output: separate handlers for error and non-error logs
- Configurable levels: support for ERROR, WARNING, INFO, and DEBUG levels
import structlog
logger = structlog.get_logger(__name__)
# Different log levels
logger.info("This is an info message")
logger.debug("This is a debug message")
logger.error("This is an error message")
logger.warning("This is a warning message")
logger.info("Server starting", extra={
"host": "0",
"port": 8989,
"environment": "production"
})
try:
# Some code that might raise an exception
raise ValueError("Something went wrong")
except Exception as e:
logger.error("Error occurred", exc_info=True)
The logging system can be configured through:
-
CLI arguments:
codegate serve --log-level DEBUG --log-format TEXT
-
Environment variables:
export APP_LOG_LEVEL=DEBUG export CODEGATE_LOG_FORMAT=TEXT
-
Configuration file:
log_level: DEBUG log_format: TEXT
-
Always use the appropriate log level:
ERROR
for errors that need immediate attentionWARNING
for potentially harmful situationsINFO
for general operational informationDEBUG
for detailed information useful during development
-
Include relevant context in extra fields:
logger.info("User action", extra={ "user_id": "123", "action": "login", "ip_address": "192.168.1.1" })
-
Use structured logging with JSON format in production for better log aggregation and analysis.
-
Enable
DEBUG
level logging during development for maximum visibility.