File size: 1,969 Bytes
e516f1f | 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 | """
Verbose logger utility for controlling print output based on verbosity level.
Verbosity Levels:
0 = Silent: No output except errors
1 = Minimal: Only essential information (iterations, final results)
2 = Standard: Standard progress information (default)
3 = Debug: All debug information including optimization steps
"""
class VerboseLogger:
"""Logger with configurable verbosity level."""
# Verbosity level constants
SILENT = 0
MINIMAL = 1
STANDARD = 2
DEBUG = 3
def __init__(self, level=STANDARD):
"""
Initialize logger with specified verbosity level.
Args:
level (int): Verbosity level (0-3)
"""
self.level = max(0, min(3, level)) # Clamp to 0-3
def set_level(self, level):
"""Set the verbosity level."""
self.level = max(0, min(3, level))
def silent(self, *args, **kwargs):
"""Print only if level >= SILENT (always, for errors)."""
if self.level >= self.SILENT:
print(*args, **kwargs)
def minimal(self, *args, **kwargs):
"""Print only if level >= MINIMAL."""
if self.level >= self.MINIMAL:
print(*args, **kwargs)
def standard(self, *args, **kwargs):
"""Print only if level >= STANDARD."""
if self.level >= self.STANDARD:
print(*args, **kwargs)
def debug(self, *args, **kwargs):
"""Print only if level >= DEBUG."""
if self.level >= self.DEBUG:
print(*args, **kwargs)
def error(self, *args, **kwargs):
"""Always print errors regardless of verbosity level."""
print(*args, **kwargs)
# Global logger instance
_global_logger = VerboseLogger()
def get_logger():
"""Get the global logger instance."""
return _global_logger
def set_verbose_level(level):
"""Set the global logger verbosity level."""
_global_logger.set_level(level)
|