You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are having some trouble with pyomo not respecting our custom logging configuration, and unconditionally emitting its logs to stdout in a hardcoded format.
Consider, for example, how pyomo ignores a Python logging configuration that sends all logs into a StringIO buffer with a custom format message:
Unfortunately, the "pyomo" logger emits its log to both stdout and the string_io buffer...Additionally, the log it emits to stdout also does not respect the custom format configured in the logging.basicConfig call---the message pyomo emits to stdout doesn't have custom as its prefix.
I think the root cause of this issue is that pyomo adds a custom handler to itself in common/log.py:
Note It is strongly advised that you do not add any handlers other than NullHandler to your library’s loggers. This is because the configuration of handlers is the prerogative of the application developer who uses your library. The application developer knows their target audience and what handlers are most appropriate for their application: if you add handlers ‘under the hood’, you might well interfere with their ability to carry out unit tests and deliver logs which suit their requirements.
In our application, for example, we want all log messages to have a custom JSON format and be sent to stderr. But, we wind up with many logs from pyomo in stdout without our custom JSON formatting.
We can work around this problem by preemptively running import pyomo.environ and then clobbering all of the handlers on the "pyomo" logger, but---of course---that solution is pretty brittle.
Thanks for your help.
The text was updated successfully, but these errors were encountered:
Hello,
Thank you for all the hard work on Pyomo!
We are having some trouble with
pyomo
not respecting our custom logging configuration, and unconditionally emitting its logs tostdout
in a hardcoded format.Consider, for example, how
pyomo
ignores a Python logging configuration that sends all logs into aStringIO
buffer with a custom format message:Unfortunately, the
"pyomo"
logger emits its log to bothstdout
and thestring_io
buffer...Additionally, the log it emits tostdout
also does not respect the custom format configured in thelogging.basicConfig
call---the messagepyomo
emits to stdout doesn't havecustom
as its prefix.I think the root cause of this issue is that
pyomo
adds a custom handler to itself incommon/log.py
:pyomo/pyomo/common/log.py
Lines 165 to 171 in 263d5b1
This custom handler, evidently, is responsible for the log messages that always wind up in
stdout
in the wrong format irrespective of the users's choices. The Python docs advise against adding a handler in a library's logger because it means that developers are unable to configure logging to suit their needs:In our application, for example, we want all log messages to have a custom JSON format and be sent to
stderr
. But, we wind up with many logs frompyomo
instdout
without our custom JSON formatting.We can work around this problem by preemptively running
import pyomo.environ
and then clobbering all of the handlers on the"pyomo"
logger, but---of course---that solution is pretty brittle.Thanks for your help.
The text was updated successfully, but these errors were encountered: