Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON logging #149

Open
mmmvvvppp opened this issue Feb 7, 2023 · 1 comment
Open

JSON logging #149

mmmvvvppp opened this issue Feb 7, 2023 · 1 comment

Comments

@mmmvvvppp
Copy link

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/

@Christopher-Chianelli
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants