optapy / optapy

OptaPy is an AI constraint solver for Python to optimize planning and scheduling problems.

Home Page:https://www.optapy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON logging

mmmvvvppp opened this issue · comments

Is there any way to make OptaPy log with a JSON format? It seems the handler in the optaplanner_python_logger is disabled, otherwise I'd be trying to provide a custom handler, e.g. https://pypi.org/project/python-json-logger/

This is complicated, since we don't actually use a Python logger, but a Java logger which our Python logger acts as a wrapper for:

class OptaPyLogger(__original_logging_class):
def __init__(self, name):
super().__init__(name)
subpackage = name[len('optapy'):]
self.java_logger_name = f'org.optaplanner{subpackage}'
def setLevel(self, level):
from org.optaplanner.optapy.logging import PythonLoggingToLogbackAdapter
PythonLoggingToLogbackAdapter.setLevel(self.java_logger_name, level)
def isEnabledFor(self, level):
from org.optaplanner.optapy.logging import PythonLoggingToLogbackAdapter
PythonLoggingToLogbackAdapter.isEnabledFor(self.java_logger_name, level)
def getEffectiveLevel(self):
from org.optaplanner.optapy.logging import PythonLoggingToLogbackAdapter
PythonLoggingToLogbackAdapter.getEffectiveLevel(self.java_logger_name)
def getChild(self, suffix):
return OptaPyLogger(f'{self.name}.{suffix}')
def addFilter(self, filter):
raise NotImplementedError(f'Cannot add filter to {self.java_logger_name} logger')
def removeFilter(self, filter):
raise NotImplementedError(f'Cannot remove filter from {self.java_logger_name} logger')
def addHandler(self, hdlr):
raise NotImplementedError(f'Cannot add handler to {self.java_logger_name} logger')
def removeHandler(self, hdlr):
raise NotImplementedError(f'Cannot remove handler from {self.java_logger_name} logger')

Having the Java logger call the Python logger on the solve thread will probably massively decrease performance, due to FFI calls being so costly and Python having an interpreter lock. Since solving mostly happen in Java (since constraints are translated to Java with jpyinterpreter), having the Java logger call the Python logger in a separate thread should not be as costly, but I would need to write a custom appender for logback/slf4j.