# Consistent way to suppress solver output

**URL:** https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437
**Category:** Optimization (Mathematical)
**Created:** [February 4, 2019, 4:55pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437 "2019-02-04T16:55:20Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ndinsmore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndinsmore/32/7433_2.png) [@ndinsmore](https://discourse.julialang.org/u/ndinsmore)
#### Post date: [February 4, 2019, 4:55pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/1 "2019-02-04T16:55:21Z")

</div>

Is there a consistent way to suppress the solver output in JuMP v0.19 or MOI? It isn’t clear to me if there is.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [February 4, 2019, 6:55pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/2 "2019-02-04T18:55:41Z")

</div>

Not really; it’s all handled through each individual solver’s options API. As a hack, I suppose you could use

```julia
redirect_stdout(devnull) do
    # code here
end

```

but that doesn’t remove the overhead of the solver printing status info (which may or may not be a problem).

---

<div class="post-metadata">

### Author: ![anon43133060](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@anon43133060](https://discourse.julialang.org/u/anon43133060)
#### Post date: [February 4, 2019, 9:06pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/3 "2019-02-04T21:06:07Z")

</div>

> [@tkoolen](#):
>
> redirect\_stdout(devnull) do

`devnull` is of type `IO` not `IOStream`:

```julia
ERROR: MethodError: no method matching redirect_stderr(::Base.DevNull)
Closest candidates are:
  redirect_stderr() at stream.jl:1001
  redirect_stderr(::Union{IOStream, Base.LibuvStream}) at stream.jl:995
  redirect_stderr(::Function, ::Any) at stream.jl:1051

```

What’s the best way to convert from `IO` to `IOStream`?

---

<div class="post-metadata">

### Author: ![ndinsmore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndinsmore/32/7433_2.png) [@ndinsmore](https://discourse.julialang.org/u/ndinsmore)
#### Post date: [February 4, 2019, 9:28pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/4 "2019-02-04T21:28:21Z")

</div>

The solution I ended up with was:  
`redirect_stdout((()->optimize!(model)),open("/dev/null", "w"))`

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [February 4, 2019, 9:32pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/5 "2019-02-04T21:32:09Z")

</div>

Yep. Sorry, should have tested my code. Related issue: [Proposal: Allow DevNull to be used in redirect\_stdout · Issue #22879 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/22879).

---

<div class="post-metadata">

### Author: ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)
#### Post date: [January 31, 2020, 10:32am UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/6 "2020-01-31T10:32:25Z")

</div>

Just came across this old problem. If you want to use parts of the output you can redirect stdout to a string:

```julia
function capture_stdout(f)
    stdout_orig = stdout
    (rd, wr) = redirect_stdout()
    f()
    close(wr)
    redirect_stdout(stdout_orig)
    read(rd, String)
end

s = capture_stdout() do
    optimize!(model)
end

#print the summary (which starts with "Number of Iterations")
try
    s[findlast("Number of Iterations", s)[1]:end] |> print
catch
    print(s)
end

```

EDIT:  
Digged a bit further and found that most solvers support some kind of [loglevel setting](https://www.isys.uni-stuttgart.de/lehre/lehrveranstaltungen/nmopt/Aufgaben/JuMP_Kurzbeschreibung.pdf), e.g.:

- Ipopt: “print\_level”
- Cbc: “LogLevel”
- Clp: “logLevel”

These options can be set either when initialising the model, e.g.

```julia
model = Model(with_optimizer(Ipopt.Optimizer, tol = 1e-8, max_iter = 1000, print_level = 1))`

```

or afterwards using the MOI Api (which is currently not so nicely documented …)

```julia
MOI.set(model, MOI.RawParameter("print_level"), 1)

```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [October 27, 2020, 9:57am UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/7 "2020-10-27T09:57:32Z")

</div>

Is this a portable solution or it works only in Linux/Mac ?

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [October 27, 2020, 7:24pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/8 "2020-10-27T19:24:15Z")

</div>

Use [`set_silent`](https://jump.dev/JuMP.jl/stable/solvers/#JuMP.set_silent):

```nohighlight
model = Model(Ipopt.Optimizer)
set_silent(model)

```

---

<div class="post-metadata">

### Author: ![MAJegh](https://avatars.discourse-cdn.com/v4/letter/m/8edcca/32.png) [@MAJegh](https://discourse.julialang.org/u/MAJegh)
#### Post date: [December 26, 2021, 9:17pm UTC](https://discourse.julialang.org/t/consistent-way-to-suppress-solver-output/20437/9 "2021-12-26T21:17:56Z")

</div>

Had some trouble with this where output was still printed.  
It was due to the C level output not being flushed before the stdout gets restored.  
So might be helpful to call :

> Base.Libc.flush\_cstdio()

Can use the following to print to a file as well :

```julia
open("/dev/null", "w") do f
    redirect_stdout(f) do
    # redirect_stdio(stdout=f,stderr=f) do
        optimize!(model)
        Base.Libc.flush_cstdio()
    end
end

```
