"""Logging configuration with custom formatting."""
import json
import logging
import numpy as np
LOG_LEVEL = logging.INFO
[docs]
class CustomHandler(logging.StreamHandler):
"""Custom handler for logging."""
def __init__(self):
"""Initialize custom handler."""
super().__init__()
self.FORMATS = None
handlers = logging.getLogger().handlers
handler_console = None
for h in handlers:
if isinstance(h, logging.StreamHandler):
handler_console = h
break
if handler_console is None:
handler_console = logging.StreamHandler()
if handler_console is not None:
# first we need to remove to avoid duplication
logging.getLogger().removeHandler(handler_console)
log = logging.getLogger(__name__)
log.setLevel(LOG_LEVEL)
log.addHandler(CustomHandler())
[docs]
class NumpyComplexEncoder(json.JSONEncoder):
"""Custom JSON encoder for numpy and complex types."""
[docs]
def default(self, obj):
"""Convert non-serializable types to serializable formats."""
if isinstance(obj, complex):
return {"real": obj.real, "imag": obj.imag}
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, np.complexfloating):
return {"real": obj.real, "imag": obj.imag}
return super().default(obj)