-
Notifications
You must be signed in to change notification settings - Fork 3.7k
implement python logger wrapper #7713
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
Changes from all commits
fe775af
838d6ae
ffe130b
2aa9c02
4cb8627
2085231
2dd8f74
e82dab9
3da66f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,106 @@ static ProducerConfiguration& ProducerConfiguration_setCryptoKeyReader(ProducerC | |
| return conf; | ||
| } | ||
|
|
||
| class LoggerWrapper: public Logger { | ||
| PyObject* _pyLogger; | ||
| int _currentPythonLogLevel = _getLogLevelValue(Logger::LEVEL_INFO); | ||
|
|
||
| void _updateCurrentPythonLogLevel() { | ||
| PyGILState_STATE state = PyGILState_Ensure(); | ||
|
|
||
| try { | ||
| _currentPythonLogLevel = py::call_method<int>(_pyLogger, "getEffectiveLevel"); | ||
| } catch (py::error_already_set e) { | ||
| PyErr_Print(); | ||
| } | ||
|
|
||
| PyGILState_Release(state); | ||
| }; | ||
|
|
||
| int _getLogLevelValue(Level level) { | ||
| return 10 + (level * 10); | ||
| } | ||
|
|
||
| public: | ||
|
|
||
| LoggerWrapper(const std::string &logger, PyObject* pyLogger) { | ||
| _pyLogger = pyLogger; | ||
| Py_XINCREF(_pyLogger); | ||
|
|
||
| _updateCurrentPythonLogLevel(); | ||
| } | ||
|
|
||
| LoggerWrapper(const LoggerWrapper& other) { | ||
| _pyLogger = other._pyLogger; | ||
| Py_XINCREF(_pyLogger); | ||
| } | ||
|
|
||
| LoggerWrapper& operator=(const LoggerWrapper& other) { | ||
lbenc135 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _pyLogger = other._pyLogger; | ||
| Py_XINCREF(_pyLogger); | ||
| return *this; | ||
| } | ||
|
|
||
| virtual ~LoggerWrapper() { | ||
| Py_XDECREF(_pyLogger); | ||
| } | ||
|
|
||
| bool isEnabled(Level level) { | ||
| return _getLogLevelValue(level) >= _currentPythonLogLevel; | ||
| } | ||
|
|
||
| void log(Level level, int line, const std::string& message) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file and line information is not logged, but I'm not familiar with the code enought to decide what is more appropriate. From the point of developer I would preffer the file and line information in the log message. Probably someone more familiar with this code should speek his opinion. Otherwise looks good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to show example and you incentive to help with decission.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not having this info only truly matters if the Python It should be possible to create a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you @Bklyn, please consider approving the PR |
||
| PyGILState_STATE state = PyGILState_Ensure(); | ||
|
|
||
| try { | ||
| switch (level) { | ||
| case Logger::LEVEL_DEBUG: | ||
| py::call_method<void>(_pyLogger, "debug", message.c_str()); | ||
| break; | ||
| case Logger::LEVEL_INFO: | ||
| py::call_method<void>(_pyLogger, "info", message.c_str()); | ||
| break; | ||
| case Logger::LEVEL_WARN: | ||
| py::call_method<void>(_pyLogger, "warning", message.c_str()); | ||
| break; | ||
| case Logger::LEVEL_ERROR: | ||
| py::call_method<void>(_pyLogger, "error", message.c_str()); | ||
| break; | ||
| } | ||
|
|
||
| } catch (py::error_already_set e) { | ||
| PyErr_Print(); | ||
| } | ||
|
|
||
| PyGILState_Release(state); | ||
| } | ||
| }; | ||
|
|
||
| class LoggerWrapperFactory : public LoggerFactory { | ||
| static LoggerWrapperFactory* _instance; | ||
| PyObject* _pyLogger; | ||
|
|
||
| public: | ||
| LoggerWrapperFactory(py::object pyLogger) { | ||
| _pyLogger = pyLogger.ptr(); | ||
| Py_XINCREF(_pyLogger); | ||
| } | ||
|
|
||
| virtual ~LoggerWrapperFactory() { | ||
| Py_XDECREF(_pyLogger); | ||
| } | ||
|
|
||
| Logger* getLogger(const std::string &fileName) { | ||
| return new LoggerWrapper(fileName, _pyLogger); | ||
| } | ||
| }; | ||
|
|
||
| static ClientConfiguration& ClientConfiguration_setLogger(ClientConfiguration& conf, py::object logger) { | ||
| conf.setLogger(new LoggerWrapperFactory(logger)); | ||
| return conf; | ||
| } | ||
|
|
||
|
|
||
| void export_config() { | ||
| using namespace boost::python; | ||
|
|
||
|
|
@@ -110,6 +210,7 @@ void export_config() { | |
| .def("tls_allow_insecure_connection", &ClientConfiguration::isTlsAllowInsecureConnection) | ||
| .def("tls_allow_insecure_connection", &ClientConfiguration::setTlsAllowInsecureConnection, return_self<>()) | ||
| .def("tls_validate_hostname", &ClientConfiguration::setValidateHostName, return_self<>()) | ||
| .def("set_logger", &ClientConfiguration_setLogger, return_self<>()) | ||
| ; | ||
|
|
||
| class_<ProducerConfiguration>("ProducerConfiguration") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.