# Is it possible to suppress a message printed by a function?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 10, 2022, 11:01am UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506 "2022-12-10T11:01:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 10, 2022, 11:01am UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/1 "2022-12-10T11:01:05Z")

</div>

Hello,

In R there are two function to print a message in the console: `cat` and `message`. The `message` function is better because it is possible to turn off the message it displays (with the `suppressMessages` function). This is useful in a Rmarkdown or Quarto document, there’s a chunk option to suppress the messages.

I did a function which prints some messages with `printstyled`. Is it possible to turn off these messages? Obviously I could add an argument `messages = true/false` in my function but this is not what I want.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [December 10, 2022, 11:50am UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/2 "2022-12-10T11:50:56Z")

</div>

You could make it dependent on a global variable. Probably that’s similar to what happens in the others languages.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 10, 2022, 12:47pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/3 "2022-12-10T12:47:06Z")

</div>

Have you looked at [this](https://docs.julialang.org/en/v1/stdlib/Logging/)?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 10, 2022, 2:35pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/4 "2022-12-10T14:35:53Z")

</div>

Are you looking for something like this:

```julia
julia> function output(x)
       printstyled(x;color=1)
       end
output (generic function with 1 method)

julia> output("Hello")
Hello
julia> so = stdout
Base.TTY(Base.Libc.WindowsRawSocket(0x0000000000000274) open, 0 bytes waiting)

julia> redirect_stdout(devnull)
Base.DevNull()

julia> output("Hello")

julia> redirect_stdout(so)
Base.TTY(Base.Libc.WindowsRawSocket(0x0000000000000274) open, 0 bytes waiting)

julia> output("Hello")
Hello

```

---

<div class="post-metadata">

**Author:** ![dylanxyz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dylanxyz/32/36646_2.png) [@dylanxyz](https://discourse.julialang.org/u/dylanxyz)\
**Post date:** [December 10, 2022, 8:50pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/5 "2022-12-10T20:50:51Z")

</div>

A macro based on @oheil answer:

```julia
julia> macro noprint(expr)
           quote
               let so = stdout
                   redirect_stdout(devnull)
                   res = $(esc(expr))
                   redirect_stdout(so)
                   res
               end
           end
       end
@noprint (macro with 1 method)

julia> output(x) = printstyled(x; color=1)
output (generic function with 1 method)

julia> output("Hello")
Hello
julia> @noprint output("Hello")

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 10, 2022, 9:27pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/6 "2022-12-10T21:27:31Z")

</div>

> [@\_stla](#):
>
> The `message` function is better because it is possible to turn off the message it displays (with the `suppressMessages` function).

The analogue of this in Julia is the [`Logging` standard library](https://docs.julialang.org/en/v1/stdlib/Logging/), which allows lots of control over message visibility and archiving.

For example:

```julia
julia> for i = 1:5
          @info "Reached iteration $i"
       end
[ Info: Reached iteration 1
[ Info: Reached iteration 2
[ Info: Reached iteration 3
[ Info: Reached iteration 4
[ Info: Reached iteration 5

julia> global_logger(ConsoleLogger(Warn));

julia> for i = 1:5
          @info "Reached iteration $i"
       end # no output anymore!

julia> 

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 10, 2022, 10:13pm UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/7 "2022-12-10T22:13:11Z")

</div>

> [@stevengj](#):
>
> The analogue of this in Julia is the [`Logging` standard library](https://docs.julialang.org/en/v1/stdlib/Logging/), which allows lots of control over message visibility and archiving.

See also [https://julialogging.github.io/](https://julialogging.github.io/) for more documentation and very useful extensions. In particular the [LoggingExtras](https://julialogging.github.io/package-overview/#loggingextras-overview) package.

---

<div class="post-metadata">

**Author:** ![\_stla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/_stla/32/35613_2.png) [@\_stla](https://discourse.julialang.org/u/_stla)\
**Post date:** [December 12, 2022, 12:48am UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/8 "2022-12-12T00:48:21Z")

</div>

Looks good. Is it possible to print in color with `@info`?

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [December 12, 2022, 1:05am UTC](https://discourse.julialang.org/t/is-it-possible-to-suppress-a-message-printed-by-a-function/91506/9 "2022-12-12T01:05:18Z")

</div>

[Based on this](https://julialogging.github.io/reference/loggingextras/#LoggingExtras.FormatLogger):

```julia
julia> using Logging, LoggingExtras

julia> log_fmtr = FormatLogger() do io, args
           printstyled(io, "$(args.level): $(args.message)\n"; color=1)
       end
FormatLogger(var"#186#187"(), Base.TTY(Base.Libc.WindowsRawSocket(0x0000000000000270) open, 0 bytes waiting), true)

julia> with_logger(log_fmtr) do; @info "This is in red." end
Info: This is in red.

```
