Logging and Monitoring API¶
SpindleX provides a comprehensive logging and performance monitoring system.
Core Logging¶
spindlex.logging.logger ¶
Main logging interface for SpindleX.
Classes¶
SSHLogger ¶
Enhanced logger for SpindleX with security and performance features.
Source code in spindlex/logging/logger.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
Methods:¶
__init__(name, logger=None) ¶
Initialize SSH logger.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Logger name | required |
logger | Optional[Logger] | Existing logger instance (if None, creates new one) | None |
Source code in spindlex/logging/logger.py
critical(msg, *args, **kwargs) ¶
debug(msg, *args, **kwargs) ¶
error(msg, *args, **kwargs) ¶
exception(msg, *args, **kwargs) ¶
info(msg, *args, **kwargs) ¶
performance_metric(operation, duration, **kwargs) ¶
Log performance metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | str | Operation name | required |
duration | float | Operation duration in seconds | required |
**kwargs | Any | Additional metric data | {} |
Source code in spindlex/logging/logger.py
protocol_debug(direction, message_type, data, **kwargs) ¶
Log protocol-level debugging information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
direction | str | 'sent' or 'received' | required |
message_type | str | SSH message type | required |
data | dict[str, Any] | Message data | required |
**kwargs | Any | Additional debug data | {} |
Source code in spindlex/logging/logger.py
security_event(event_type, message, client_ip='unknown', username='unknown', **kwargs) ¶
Log security-related event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_type | str | Type of security event (auth_success, auth_failure, etc.) | required |
message | str | Event description | required |
client_ip | str | Client IP address | 'unknown' |
username | str | Username involved in event | 'unknown' |
**kwargs | Any | Additional event data | {} |
Source code in spindlex/logging/logger.py
Functions:¶
configure_logging(level=logging.INFO, format_type='standard', output_file=None, security_file=None, performance_file=None, sanitize=True, json_format=False) ¶
Configure SpindleX logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level | Union[str, int] | Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) | INFO |
format_type | str | Format type ('standard', 'debug', 'json') | 'standard' |
output_file | Optional[str] | Main log file path | None |
security_file | Optional[str] | Security events log file path | None |
performance_file | Optional[str] | Performance metrics log file path | None |
sanitize | bool | Whether to sanitize sensitive information | True |
json_format | bool | Whether to use JSON formatting for main logs | False |
Source code in spindlex/logging/logger.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
get_logger(name) ¶
Get or create SSH logger instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name | str | Logger name | required |
Returns:
| Type | Description |
|---|---|
SSHLogger | SSHLogger instance |
Source code in spindlex/logging/logger.py
Sanitization¶
spindlex.logging.sanitizer ¶
Log sanitization utilities for security-sensitive information.
Classes¶
LogSanitizer ¶
Sanitizes log messages to prevent sensitive information leakage.
Source code in spindlex/logging/sanitizer.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
Methods:¶
sanitize_dict(data) classmethod ¶
Sanitize a dictionary by redacting sensitive keys and values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data | dict[str, Any] | Dictionary to sanitize | required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | Sanitized dictionary |
Source code in spindlex/logging/sanitizer.py
sanitize_message(message) classmethod ¶
Sanitize a log message by redacting sensitive information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message | str | The log message to sanitize | required |
Returns:
| Type | Description |
|---|---|
str | Sanitized message with sensitive data redacted |
Source code in spindlex/logging/sanitizer.py
SanitizingFilter ¶
Bases: Filter
Logging filter that redacts sensitive information from all log records.
Source code in spindlex/logging/sanitizer.py
Functions:¶
configure_sanitizing_logging(logger_name='') ¶
Install SanitizingFilter on a logger so that all records it handles (including those propagated from the spindlex logger) are sanitized.
Call once at application startup, typically with no arguments to install the filter on the root logger:
import spindlex
spindlex.configure_sanitizing_logging()
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger_name | str | Logger to protect (default | '' |
Source code in spindlex/logging/sanitizer.py
Formatters and Handlers¶
spindlex.logging.formatters ¶
Custom log formatters for SpindleX.
Classes¶
DebugFormatter ¶
Bases: SSHFormatter
Detailed formatter for debugging with protocol information.
Source code in spindlex/logging/formatters.py
JSONFormatter ¶
Bases: Formatter
JSON log formatter for structured logging.
Source code in spindlex/logging/formatters.py
Methods:¶
__init__(sanitize=True, include_extra=True) ¶
Initialize JSON formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sanitize | bool | Whether to sanitize sensitive information | True |
include_extra | bool | Whether to include extra fields from LogRecord | True |
Source code in spindlex/logging/formatters.py
format(record) ¶
Format log record as JSON.
Source code in spindlex/logging/formatters.py
SSHFormatter ¶
Bases: Formatter
Standard SpindleX log formatter with security sanitization.
Source code in spindlex/logging/formatters.py
Methods:¶
__init__(fmt=None, datefmt=None, sanitize=True) ¶
Initialize SSH formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt | Optional[str] | Log format string | None |
datefmt | Optional[str] | Date format string | None |
sanitize | bool | Whether to sanitize sensitive information | True |
Source code in spindlex/logging/formatters.py
SecurityFormatter ¶
Bases: SSHFormatter
Enhanced formatter for security-related events.
Source code in spindlex/logging/formatters.py
spindlex.logging.handlers ¶
Custom log handlers for SpindleX.
Classes¶
PerformanceHandler ¶
Bases: Handler
Handler for performance metrics and timing information.
Source code in spindlex/logging/handlers.py
Methods:¶
__init__(filename=None, json_format=True) ¶
Initialize performance handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | Optional[str] | Log file path (if None, logs to console) | None |
json_format | bool | Whether to use JSON formatting | True |
Source code in spindlex/logging/handlers.py
close() ¶
emit(record) ¶
Emit performance log record.
Source code in spindlex/logging/handlers.py
SecurityHandler ¶
Bases: Handler
Handler for security-related events with special formatting.
Source code in spindlex/logging/handlers.py
Methods:¶
__init__(filename=None, max_bytes=10 * 1024 * 1024, backup_count=5) ¶
Initialize security handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename | Optional[str] | Log file path (if None, logs to console) | None |
max_bytes | int | Maximum file size before rotation | 10 * 1024 * 1024 |
backup_count | int | Number of backup files to keep | 5 |
Source code in spindlex/logging/handlers.py
close() ¶
emit(record) ¶
Emit log record to appropriate handler.
Source code in spindlex/logging/handlers.py
Performance Monitoring¶
spindlex.logging.monitoring ¶
Performance monitoring and metrics collection for SpindleX.
Classes¶
ConnectionMetrics dataclass ¶
Metrics for SSH connection operations.
Source code in spindlex/logging/monitoring.py
CryptoTimer ¶
Specialized timer for cryptographic operations.
Source code in spindlex/logging/monitoring.py
Methods:¶
__init__(monitor=None) ¶
Initialize crypto timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monitor | Optional[PerformanceMonitor] | Performance monitor instance (uses global if None) | None |
Source code in spindlex/logging/monitoring.py
time_crypto_operation(operation, algorithm, key_size=None, **metadata) ¶
Time a cryptographic operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | str | Type of crypto operation (encrypt, decrypt, sign, verify, etc.) | required |
algorithm | str | Cryptographic algorithm name | required |
key_size | Optional[int] | Key size in bits (optional) | None |
**metadata | Any | Additional metadata | {} |
Source code in spindlex/logging/monitoring.py
time_decryption(cipher, data_size) ¶
time_encryption(cipher, data_size) ¶
time_key_exchange(algorithm) ¶
time_key_generation(algorithm, key_size) ¶
time_signature(algorithm, key_size) ¶
time_verification(algorithm, key_size) ¶
PerformanceMetric dataclass ¶
PerformanceMonitor ¶
Performance monitoring and metrics collection system.
Source code in spindlex/logging/monitoring.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
Methods:¶
__init__(max_metrics=10000) ¶
Initialize performance monitor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_metrics | int | Maximum number of metrics to keep in memory | 10000 |
Source code in spindlex/logging/monitoring.py
clear_metrics(connection_id=None) ¶
Clear stored metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id | Optional[str] | Clear metrics for specific connection (optional) | None |
Source code in spindlex/logging/monitoring.py
get_connection_metrics(connection_id) ¶
Get metrics for a specific connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id | str | Unique connection identifier | required |
Returns:
| Type | Description |
|---|---|
ConnectionMetrics | ConnectionMetrics instance |
Source code in spindlex/logging/monitoring.py
get_operation_stats(operation) ¶
Get statistical summary for an operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | str | Operation name | required |
Returns:
| Type | Description |
|---|---|
dict[str, float] | Dictionary with min, max, mean, median statistics |
Source code in spindlex/logging/monitoring.py
get_recent_metrics(operation=None, limit=100) ¶
Get recent performance metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | Optional[str] | Filter by operation name (optional) | None |
limit | int | Maximum number of metrics to return | 100 |
Returns:
| Type | Description |
|---|---|
list[PerformanceMetric] | List of recent metrics |
Source code in spindlex/logging/monitoring.py
increment_connection_counter(connection_id, counter_name, amount=1) ¶
Increment a connection counter metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id | str | Connection identifier | required |
counter_name | str | Name of counter to increment | required |
amount | int | Amount to increment by | 1 |
Source code in spindlex/logging/monitoring.py
print_summary() ¶
Print a human-readable summary of all recorded performance metrics.
Source code in spindlex/logging/monitoring.py
record_metric(operation, duration, **metadata) ¶
Record a performance metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | str | Operation name | required |
duration | float | Operation duration in seconds | required |
**metadata | Any | Additional metric metadata | {} |
Source code in spindlex/logging/monitoring.py
time_operation(operation, **metadata) ¶
Context manager for timing operations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation | str | Operation name | required |
**metadata | Any | Additional metadata to record | {} |
Source code in spindlex/logging/monitoring.py
update_connection_metric(connection_id, metric_name, value) ¶
Update a specific connection metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_id | str | Connection identifier | required |
metric_name | str | Name of metric to update | required |
value | Union[float, int, str] | New metric value | required |
Source code in spindlex/logging/monitoring.py
ProtocolAnalyzer ¶
Analyzer for SSH protocol debugging and performance analysis.
Source code in spindlex/logging/monitoring.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | |
Methods:¶
__init__(monitor=None) ¶
Initialize protocol analyzer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monitor | Optional[PerformanceMonitor] | Performance monitor instance (uses global if None) | None |
Source code in spindlex/logging/monitoring.py
clear_stats() ¶
get_message_stats() ¶
Get statistics for protocol messages.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]] | Dictionary with message statistics |
Source code in spindlex/logging/monitoring.py
record_message(direction, message_type, size, connection_id) ¶
Record SSH protocol message for analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
direction | str | 'sent' or 'received' | required |
message_type | str | SSH message type | required |
size | int | Message size in bytes | required |
connection_id | str | Connection identifier | required |
Source code in spindlex/logging/monitoring.py
Functions:¶
get_performance_monitor() ¶
get_protocol_analyzer() ¶
timed_operation(operation_name, **metadata) ¶
Decorator for timing function execution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation_name | str | Name of the operation being timed | required |
**metadata | Any | Additional metadata to record | {} |