How to surpress direct output to stdout if logging enabled - Python API
OngoingI would like to suppress direct logging to stdout if logging is enabled. The reason is that for an example below (modified mip.py example):
import sys
import logging
import time
from gurobipy import *
def main(argv):
try:
# Create a new model
m = Model("mip1")
# Create variables
x = m.addVar(vtype=GRB.BINARY, name="x")
y = m.addVar(vtype=GRB.BINARY, name="y")
z = m.addVar(vtype=GRB.BINARY, name="z")
# Set objective
m.setObjective(x + y + 2 * z, GRB.MAXIMIZE)
# Add constraint: x + 2 y + 3 z <= 4
m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
# Add constraint: x + y >= 1
m.addConstr(x + y >= 1, "c1")
m.optimize()
for v in m.getVars():
print('%s %g' % (v.varName, v.x))
print('Obj: %g' % m.objVal)
except GurobiError as e:
print('Error code ' + str(e.errno) + ": " + str(e))
except AttributeError:
print('Encountered an attribute error')
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
datefmt='%Y/%m/%d %I:%M:%S %p',
level=(logging.INFO))
logging.Formatter.converter = time.gmtime
main(sys.argv)
I am getting the following output:
Optimize a model with 2 rows, 3 columns and 5 nonzeros
2019/11/24 11:27:02 AM INFO: Optimize a model with 2 rows, 3 columns and 5 nonzeros
Variable types: 0 continuous, 3 integer (3 binary)
Coefficient statistics:
2019/11/24 11:27:02 AM INFO: Variable types: 0 continuous, 3 integer (3 binary)
Matrix range [1e+00, 3e+00]
2019/11/24 11:27:02 AM INFO: Coefficient statistics:
Objective range [1e+00, 2e+00]
2019/11/24 11:27:02 AM INFO: Matrix range [1e+00, 3e+00]
Bounds range [1e+00, 1e+00]
2019/11/24 11:27:02 AM INFO: Objective range [1e+00, 2e+00]
RHS range [1e+00, 4e+00]
2019/11/24 11:27:02 AM INFO: Bounds range [1e+00, 1e+00]
Found heuristic solution: objective 2.0000000
2019/11/24 11:27:02 AM INFO: RHS range [1e+00, 4e+00]
Presolve removed 2 rows and 3 columns
2019/11/24 11:27:02 AM INFO: Found heuristic solution: objective 2.0000000
2019/11/24 11:27:02 AM INFO: Presolve removed 2 rows and 3 columns
Presolve time: 0.00s
2019/11/24 11:27:02 AM INFO: Presolve time: 0.00s
Presolve: All rows and columns removed
2019/11/24 11:27:02 AM INFO: Presolve: All rows and columns removed
2019/11/24 11:27:02 AM INFO:
Explored 0 nodes (0 simplex iterations) in 0.00 seconds
2019/11/24 11:27:02 AM INFO: Explored 0 nodes (0 simplex iterations) in 0.00 seconds
Thread count was 1 (of 12 available processors)
2019/11/24 11:27:02 AM INFO: Thread count was 1 (of 12 available processors)
2019/11/24 11:27:02 AM INFO:
Solution count 2: 3 2
2019/11/24 11:27:02 AM INFO: Solution count 2: 3 2
Optimal solution found (tolerance 1.00e-04)
2019/11/24 11:27:02 AM INFO:
Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
2019/11/24 11:27:02 AM INFO: Optimal solution found (tolerance 1.00e-04)
2019/11/24 11:27:02 AM INFO: Best objective 3.000000000000e+00, best bound 3.000000000000e+00, gap 0.0000%
x 1
y 0
z 1
Obj: 3
where each log line is repeated. I tried to play with parameters "OutputFlag" and "LogToConsole" but without a luck. When I disable them, both outputs are disabled.
-
Hi Grzegorz,
A workaround could be to define a dedicated logfile that your logger uses to print to. You could then ignore the stdout from Gurobi and only monitor the logfile.
To do this you just need to specify a filename in the basicConfig call: logging.basicConfig(filename="info.log")
I hope this helps.
Cheers,
Matthias0 -
I am facing the same issue. Using a file is not a real solution: I would like my application to either log to console or to file. So if I select console I get a polluted output. I guess some of the output is not piped via the `logging` module.
Any other suggestions?
0
Please sign in to leave a comment.
Comments
2 comments