|
| 1 | +# Logging |
| 2 | + |
| 3 | +This document describes the logging system used in the Ethereum Execution Spec Tests project. Currently, logging is only supported for `consume` commands. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The project uses Python's standard logging module with custom extensions to provide enhanced logging capabilities. Our logging system is implemented in the `src/pytest_plugins/logging.py` module and provides the following features: |
| 8 | + |
| 9 | +- Custom log levels between the standard Python log levels |
| 10 | +- Timestamps with millisecond precision in UTC |
| 11 | +- Color-coded log output (when not running in Docker) |
| 12 | +- File logging with a consistent naming pattern |
| 13 | +- Integration with pytest's output capture |
| 14 | +- Support for distributed test execution with pytest-xdist |
| 15 | + |
| 16 | +## Custom Log Levels |
| 17 | + |
| 18 | +In addition to the standard Python log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL), we've added the following custom levels: |
| 19 | + |
| 20 | +| Level | Numeric Value | Purpose | |
| 21 | +|-------|--------------|---------| |
| 22 | +| VERBOSE | 15 | For messages more detailed than INFO but less verbose than DEBUG | |
| 23 | +| FAIL | 35 | For test failures and related issues (between WARNING and ERROR) | |
| 24 | + |
| 25 | +## Using the Logger |
| 26 | + |
| 27 | +### Getting a Logger |
| 28 | + |
| 29 | +To use the custom logger in your code, import the `get_logger` function from the logging module: |
| 30 | + |
| 31 | +```python |
| 32 | +from pytest_plugins.logging import get_logger |
| 33 | + |
| 34 | +# Create a logger with your module's name |
| 35 | +logger = get_logger(__name__) |
| 36 | +``` |
| 37 | + |
| 38 | +### Logging at Different Levels |
| 39 | + |
| 40 | +You can use all standard Python log methods plus our custom methods: |
| 41 | + |
| 42 | +```python |
| 43 | +# Standard log levels |
| 44 | +logger.debug("Detailed debug information") |
| 45 | +logger.info("General information") |
| 46 | +logger.warning("Warning message") |
| 47 | +logger.error("Error message") |
| 48 | +logger.critical("Critical failure") |
| 49 | + |
| 50 | +# Custom log levels |
| 51 | +logger.verbose("More detailed than INFO, less than DEBUG") |
| 52 | +logger.fail("Test failure or similar issue") |
| 53 | +``` |
| 54 | + |
| 55 | +### When to Use Each Level |
| 56 | + |
| 57 | +- **DEBUG (10)**: For very detailed diagnostic information useful for debugging |
| 58 | +- **VERBOSE (15)**: For information that's useful during development but more detailed than INFO |
| 59 | +- **INFO (20)**: For general information about program operation |
| 60 | +- **WARNING (30)**: For potential issues that don't prevent program execution |
| 61 | +- **FAIL (35)**: For test failures and related issues |
| 62 | +- **ERROR (40)**: For errors that prevent an operation from completing |
| 63 | +- **CRITICAL (50)**: For critical errors that may prevent the program from continuing |
| 64 | + |
| 65 | +## Configuration |
| 66 | + |
| 67 | +### Setting Log Level on the Command Line |
| 68 | + |
| 69 | +You can adjust the log level when running pytest with the `--eest-log-level` option: |
| 70 | + |
| 71 | +```bash |
| 72 | +consume engine --input=latest@stable --eest-log-level=VERBOSE -s --sim.limit=".*chainid.*" |
| 73 | +``` |
| 74 | + |
| 75 | +The argument accepts both log level names (e.g., "DEBUG", "VERBOSE", "INFO") and numeric values. |
| 76 | + |
| 77 | +Adding pytest's `-s` flag writes the logging messages to the terminal; otherwise output will be written to the log file that is reported in the test session header at the end of the test session. |
| 78 | + |
| 79 | +### Log File Output |
| 80 | + |
| 81 | +Log files are automatically created in the `logs/` directory with a naming pattern that includes: |
| 82 | + |
| 83 | +- The command name, e.g. `consume`, |
| 84 | +- An optional subcommand (e.g., `engine`), |
| 85 | +- A timestamp in UTC, |
| 86 | +- The worker ID (when using pytest-xdist). |
| 87 | + |
| 88 | +Example: `consume-engine-20240101-123456-main.log` |
| 89 | + |
| 90 | +The log file path is displayed in the pytest header and summary. |
| 91 | + |
| 92 | +### Using the Standalone Configuration in Non-Pytest Projects |
| 93 | + |
| 94 | +The logging module can also be used in non-pytest projects by using the `configure_logging` function: |
| 95 | + |
| 96 | +```python |
| 97 | +from pytest_plugins.logging import configure_logging, get_logger |
| 98 | + |
| 99 | +# Configure logging with custom settings |
| 100 | +configure_logging( |
| 101 | + log_level="VERBOSE", |
| 102 | + log_file="my_application.log", |
| 103 | + log_to_stdout=True, |
| 104 | + log_format="%(asctime)s [%(levelname)s] %(name)s: %(message)s", |
| 105 | + use_color=True |
| 106 | +) |
| 107 | + |
| 108 | +# Get a logger and use it |
| 109 | +logger = get_logger(__name__) |
| 110 | +logger.verbose("Logging configured in standalone application!") |
| 111 | +``` |
| 112 | + |
| 113 | +The `configure_logging` function accepts the following parameters: |
| 114 | + |
| 115 | +- `log_level`: A string or numeric log level (default: "INFO") |
| 116 | +- `log_file`: Path to a log file, or None to disable file logging (default: None) |
| 117 | +- `log_to_stdout`: Whether to log to stdout (default: True) |
| 118 | +- `log_format`: The format string for log messages (default: "%(asctime)s [%(levelname)s] %(name)s: %(message)s") |
| 119 | +- `use_color`: Whether to use colors in stdout output, or None to auto-detect (default: None) |
| 120 | + |
| 121 | +## Implementation Details |
| 122 | + |
| 123 | +### The EESTLogger Class |
| 124 | + |
| 125 | +The `EESTLogger` class extends Python's `Logger` class to add the custom log methods. The main module logger is automatically configured to use this class. |
| 126 | + |
| 127 | +### Formatters |
| 128 | + |
| 129 | +Two formatter classes are available: |
| 130 | + |
| 131 | +- `UTCFormatter`: Formats timestamps with millisecond precision in UTC |
| 132 | +- `ColorFormatter`: Extends `UTCFormatter` to add ANSI colors to log level names in terminal output |
| 133 | + |
| 134 | +### Pytest and Hive Integration |
| 135 | + |
| 136 | +The logging module includes several pytest hooks to: |
| 137 | + |
| 138 | +- Configure logging at the start of a test session |
| 139 | +- Record logs during test execution |
| 140 | +- Display the log file path in the test report |
| 141 | +- Ensure logs are captured properly during fixture teardown |
| 142 | + |
| 143 | +The `hive_pytest` plugin has been extended to propagate the logs to the `hiveview` UI via the test case's `details` ("description" in `hiveview`). |
| 144 | + |
| 145 | +## Example Usage |
| 146 | + |
| 147 | +A complete example of using the logging system in a `consume` test (or plugin): |
| 148 | + |
| 149 | +```python |
| 150 | +from pytest_plugins.logging import get_logger |
| 151 | + |
| 152 | +# Get a properly typed logger for your module |
| 153 | +logger = get_logger(__name__) |
| 154 | + |
| 155 | +def test_something(): |
| 156 | + # Use standard log levels |
| 157 | + logger.debug("Setting up test variables") |
| 158 | + logger.info("Starting test") |
| 159 | + |
| 160 | + # Use custom log levels |
| 161 | + logger.verbose("Additional details about test execution") |
| 162 | + |
| 163 | + # Log warnings or errors as needed |
| 164 | + if something_concerning: |
| 165 | + logger.warning("Something looks suspicious") |
| 166 | + |
| 167 | + if something_failed: |
| 168 | + logger.fail("Test condition not met") |
| 169 | + assert False, "Test failed" |
| 170 | + |
| 171 | + # Log successful completion |
| 172 | + logger.info("Test completed successfully") |
| 173 | +``` |
| 174 | + |
| 175 | +All log messages will be displayed according to the configured log level and captured in the log file. |
0 commit comments