@@ -181,10 +181,38 @@ common in the IDAES framework.
181
181
Logging Solver Output
182
182
---------------------
183
183
184
- The solver output can be captured and directed to a logger using the
185
- ``idaes.logger.solver_log(logger, level) `` context manager, which uses
186
- ``pyomo.common.tee.capture_output() `` to temporarily redirect
187
- ``sys.stdout `` and ``sys.stderr `` to a string buffer. The ``logger ``
184
+ The solver output can be captured directly as part of the ``solve ``
185
+ command by passing in a ``LogStream `` object and specifying the
186
+ preferred logger and logging level. This can be combined with other
187
+ supported ``tee `` values. Note that this **ONLY ** works with the newest
188
+ version of the Pyomo solvers (also known as ``v2 `` solvers).
189
+
190
+ *Example *
191
+
192
+ .. testcode ::
193
+
194
+ import sys, logging
195
+ import pyomo.environ as pyo
196
+ from pyomo.common.log import LogStream
197
+
198
+ logger = logging.getLogger("logging.demo")
199
+
200
+ # Only available with v2 solvers
201
+ solver = pyo.SolverFactory("ipopt_v2")
202
+
203
+ model = pyo.ConcreteModel()
204
+ model.x = pyo.Var()
205
+ model.y = pyo.Var()
206
+ model.x.fix(3)
207
+ model.c = pyo.Constraint(expr=model.y==model.x**2)
208
+
209
+ # Direct all output to a logger
210
+ res = solver.solve(model, tee=LogStream(level=logging.INFO, logger=logger))
211
+ # Direct output to both stdout and a logger
212
+ res = solver.solve(model, tee=[sys.stdout, LogStream(level=logging.INFO, logger=logger)])
213
+
214
+ If you **MUST ** use version 1 solvers, solver output can be captured using the
215
+ ``idaes.logger.solver_log(logger, level) `` context manager. The ``logger ``
188
216
argument is the logger to log to, and the ``level `` argument is the
189
217
level at which records are sent to the logger. The output is logged by a
190
218
separate logging thread, so output can be logged as it is produced
0 commit comments