Repository navigation
Expand file tree
/
Copy pathlogger.py
More file actions
358 lines (275 loc) · 10.8 KB
/
Copy pathlogger.py
File metadata and controls
358 lines (275 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#!/usr/bin/env python3
"""
Centralized Logging Module for Counterscarp Engine.
Provides production-quality logging with configurable output formats,
levels, and optional file logging. Supports both text and JSON output
formats with optional colored console output.
Example:
>>> from logger import get_logger
>>> logger = get_logger(__name__)
>>> logger.info("Scan started")
>>> logger.warning("Pattern match in comment skipped")
"""
import os
import sys
import json
import threading
import logging
from logging.handlers import RotatingFileHandler
from typing import Optional, Dict, Any, Union
from datetime import datetime
# Optional colorama import for colored console output
try:
from colorama import init as colorama_init, Fore, Style
COLORAMA_AVAILABLE = True
colorama_init(autoreset=True)
except ImportError:
COLORAMA_AVAILABLE = False
Fore = Style = None
# Default configuration
DEFAULT_LOG_LEVEL = "INFO"
DEFAULT_LOG_FORMAT = "text"
LOG_LEVELS = {
"DEBUG": logging.DEBUG,
"INFO": logging.INFO,
"WARNING": logging.WARNING,
"ERROR": logging.ERROR,
"CRITICAL": logging.CRITICAL,
}
# Color mapping for console output
COLOR_MAP = {
"DEBUG": "CYAN",
"INFO": "GREEN",
"WARNING": "YELLOW",
"ERROR": "RED",
"CRITICAL": "MAGENTA",
}
# ANSI color codes (fallback if colorama not available)
ANSI_COLORS = {
"RESET": "\033[0m",
"CYAN": "\033[36m",
"GREEN": "\033[32m",
"YELLOW": "\033[33m",
"RED": "\033[31m",
"MAGENTA": "\033[35m",
}
class ColoredFormatter(logging.Formatter):
"""Custom formatter that adds colors to console output.
Attributes:
use_color: Whether to use colored output.
"""
def __init__(self, fmt: Optional[str] = None, use_color: bool = True):
"""Initialize the formatter.
Args:
fmt: Format string for log messages.
use_color: Whether to apply colors to output.
"""
super().__init__(fmt)
self.use_color = use_color and (COLORAMA_AVAILABLE or os.name != "nt")
def format(self, record: logging.LogRecord) -> str:
"""Format the log record with optional color.
Args:
record: The log record to format.
Returns:
Formatted log message string.
"""
# Save original levelname
original_levelname = record.levelname
if self.use_color:
color_name = COLOR_MAP.get(record.levelname, "RESET")
if COLORAMA_AVAILABLE and Fore and Style:
color_attr = getattr(Fore, color_name, "")
reset_attr = Style.RESET_ALL
formatted = f"{color_attr}{record.levelname}{reset_attr}"
record.levelname = formatted
else:
color_code = ANSI_COLORS.get(color_name, ANSI_COLORS["RESET"])
reset_code = ANSI_COLORS["RESET"]
formatted = f"{color_code}{record.levelname}{reset_code}"
record.levelname = formatted
result = super().format(record)
record.levelname = original_levelname
return result
class JSONFormatter(logging.Formatter):
"""JSON formatter for structured logging.
Outputs log records as JSON lines for machine parsing.
"""
def format(self, record: logging.LogRecord) -> str:
"""Format the log record as JSON.
Args:
record: The log record to format.
Returns:
JSON string representation of the log record.
"""
log_data: Dict[str, Any] = {
"timestamp": datetime.fromtimestamp(record.created).isoformat(),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
}
# Add extra fields if present
if hasattr(record, "extra"):
log_data.update(record.extra)
# Add exception info if present
if record.exc_info:
log_data["exception"] = self.formatException(record.exc_info)
return json.dumps(log_data, default=str)
class TextFormatter(logging.Formatter):
"""Text formatter for human-readable logging.
Format: [2025-12-21 10:30:00] [WARNING] [heuristic_scanner] Message
"""
def __init__(self, fmt: Optional[str] = None):
"""Initialize the formatter.
Args:
fmt: Custom format string. Uses default if not provided.
"""
default_fmt = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"
super().__init__(fmt or default_fmt, datefmt="%Y-%m-%d %H:%M:%S")
# Global flag to track if logging has been configured
_logging_configured = False
_root_handlers: list = []
_logging_lock = threading.Lock()
def setup_logging(
level: Optional[Union[str, int]] = None,
format: Optional[str] = None,
log_file: Optional[str] = None,
use_color: bool = True,
) -> None:
"""Configure global logging settings.
This function sets up the root logger with the specified configuration.
It should be called once at application startup.
Args:
level: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL) or None
to use env var.
format: Output format ("text" or "json") or None to use env var.
log_file: Path to log file for file logging or None to use env var.
use_color: Whether to use colored console output.
Example:
>>> setup_logging(
... level="DEBUG", format="json", log_file="counterscarp.log"
... )
"""
global _logging_configured, _root_handlers
with _logging_lock:
# Get configuration from environment variables or defaults
if level is None:
level = os.environ.get("COUNTERSCARP_LOG_LEVEL", DEFAULT_LOG_LEVEL)
if format is None:
format = os.environ.get("COUNTERSCARP_LOG_FORMAT", DEFAULT_LOG_FORMAT)
if log_file is None:
log_file = os.environ.get("COUNTERSCARP_LOG_FILE")
# Convert level string to int if needed
if isinstance(level, str):
level = LOG_LEVELS.get(level.upper(), logging.INFO)
# Get root logger
root_logger = logging.getLogger()
root_logger.setLevel(level)
# Remove existing handlers to avoid duplicates
for handler in _root_handlers:
root_logger.removeHandler(handler)
_root_handlers = []
# Console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(level)
if format.lower() == "json":
console_handler.setFormatter(JSONFormatter())
else:
console_handler.setFormatter(ColoredFormatter(use_color=use_color))
root_logger.addHandler(console_handler)
_root_handlers.append(console_handler)
# File handler (optional) — uses RotatingFileHandler for log rotation
if log_file:
file_handler = RotatingFileHandler(
log_file,
maxBytes=10 * 1024 * 1024, # 10 MB per file
backupCount=7,
encoding="utf-8",
)
file_handler.setLevel(level)
# Use JSON for file logs if specified, otherwise plain text
if format.lower() == "json":
file_handler.setFormatter(JSONFormatter())
else:
file_handler.setFormatter(TextFormatter())
root_logger.addHandler(file_handler)
_root_handlers.append(file_handler)
_logging_configured = True
def get_logger(name: str) -> logging.Logger:
"""Get a logger instance with the specified name.
Creates a properly configured logger following Python best practices.
Uses the __name__ convention for hierarchical loggers.
Args:
name: The name for the logger (typically __name__).
Returns:
A configured Logger instance.
Example:
>>> logger = get_logger(__name__)
>>> logger.info("Processing started")
"""
global _logging_configured
# Auto-configure logging if not already done
if not _logging_configured:
setup_logging()
logger = logging.getLogger(name)
# Ensure the logger doesn't propagate to avoid duplicate messages
# when root logger has handlers
logger.propagate = True
return logger
def get_log_level() -> str:
"""Get the current log level from environment or default.
Returns:
The current log level string.
"""
return os.environ.get("COUNTERSCARP_LOG_LEVEL", DEFAULT_LOG_LEVEL)
def get_log_format() -> str:
"""Get the current log format from environment or default.
Returns:
The current log format ("text" or "json").
"""
return os.environ.get("COUNTERSCARP_LOG_FORMAT", DEFAULT_LOG_FORMAT)
# Convenience function for quick logging setup
def configure(
level: Optional[str] = None,
format: Optional[str] = None,
log_file: Optional[str] = None,
) -> None:
"""Alias for setup_logging for simpler imports.
Args:
level: Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL).
format: Output format ("text" or "json").
log_file: Path to log file for file logging.
Example:
>>> from logger import configure
>>> configure(level="DEBUG", format="text")
"""
setup_logging(level=level, format=format, log_file=log_file)
def append_stderr_log(stderr_text: str, tool_name: str, stderr_log_path: str) -> None:
"""Append tool stderr to the dedicated stderr log file.
Args:
stderr_text: The stderr output captured from the tool subprocess.
tool_name: Human-readable name of the tool (e.g. "slither").
stderr_log_path: Path to the file to append stderr output to.
"""
if not stderr_text or not stderr_log_path:
return
try:
with open(stderr_log_path, 'a', encoding='utf-8') as f:
f.write(f"\n[{datetime.now().isoformat()}] {tool_name}:\n")
f.write(stderr_text)
f.write("\n" + "=" * 60 + "\n")
except OSError:
pass # Don't crash the scan over a log write failure
if __name__ == "__main__":
# Demo/test code
configure(level="DEBUG", format="text")
test_logger = get_logger("counterscarp.test")
test_logger.debug("This is a debug message")
test_logger.info("This is an info message")
test_logger.warning("This is a warning message")
test_logger.error("This is an error message")
test_logger.critical("This is a critical message")
print("\n--- JSON Format Demo ---\n")
configure(level="DEBUG", format="json")
test_logger2 = get_logger("counterscarp.test.json")
test_logger2.info("JSON formatted log message")
test_logger2.warning("Pattern match in comment skipped")