# VSCodeServer.VSCodeLogger freeze when changed logged and ripristinated

**URL:** https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870
**Category:** VS Code
**Created:** [March 20, 2024, 11:45am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870 "2024-03-20T11:45:10Z")
**Posts on this page:** 10
**Page:** 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: [March 20, 2024, 11:45am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/1 "2024-03-20T11:45:10Z")

</div>

In a console this work as expected:

```julia
julia> using Logging

julia> old_logger = Logging.current_logger()
ConsoleLogger(IOBuffer(data=UInt8[...], readable=false, writable=false, seekable=false, append=false, size=0, maxsize=0, ptr=1, mark=-1), Info, Logging.default_metafmt, true, 0, Dict{Any, Int64}())

julia> oldstdout = stdout
Base.TTY(RawFD(15) open, 0 bytes waiting)

julia> redirect_stdout(devnull)
Base.DevNull()

julia> global_logger(NullLogger())
ConsoleLogger(IOBuffer(data=UInt8[...], readable=false, writable=false, seekable=false, append=false, size=0, maxsize=0, ptr=1, mark=-1), Info, Logging.default_metafmt, true, 0, Dict{Any, Int64}())

julia> println("print1")

julia> @info "info1"

julia> redirect_stdout(oldstdout)
Base.TTY(RawFD(15) open, 0 bytes waiting)

julia> global_logger(old_logger)
Base.CoreLogging.NullLogger()

julia> println("print2")
print2

julia> @info "info2"
[ Info: info2

```

However in VSCode the default logger is `VSCodeServer.VSCodeLogger` and when I try to set it back it freeze and I need to kill Julia (I have tried to deepcopy it, but no changes):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/4/b4007fffddd616101f01b6aac5ef57e561958999.png)

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 20, 2024, 4:58pm UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/2 "2024-03-20T16:58:13Z")

</div>

Heh, this is an interesting edge case. Your code is assuming that `old_logger` was a global logger previously, but that’s not actually the case – in VS Code, user code is evaluated in a `with_logger(VSCodeLogger())` context. The more correct thing here would be `old_logger = Logging.global_logger()`, which does work.

With [fix: prevent infinite recursion for incorrect logger setups by pfitzseb · Pull Request #3572 · julia-vscode/julia-vscode · GitHub](https://github.com/julia-vscode/julia-vscode/pull/3572) we’ll print a warning in this case and fall back to a normal `ConsoleLogger` though.

---

<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: [March 22, 2024, 1:57am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/3 "2024-03-22T01:57:03Z")

</div>

> [@pfitzseb](#):
>
> The more correct thing here would be `old_logger = Logging.global_logger()`, which does work.

I don’t understand your post… that’s indeed what I do in my code…

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 22, 2024, 8:26am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/4 "2024-03-22T08:26:39Z")

</div>

No? Your code has `old_logger = Logging.current_logger()`.

---

<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: [March 22, 2024, 9:19am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/5 "2024-03-22T09:19:54Z")

</div>

> [@pfitzseb](#):
>
> global\_logger

Yes, sorry. This works both on console/VSCode:

```julia
using Logging
old_logger = Logging.global_logger() #current_logger()
oldstdout = stdout
redirect_stdout(devnull)
global_logger(NullLogger())
println("print1")
@info "info1"
redirect_stdout(oldstdout)
global_logger(old_logger)
println("print2")
@info "info2"

```

Still I haven’t understood why `current_logger()` doesn’t work… the idea is I don’t know which is the Logger used, I set the current used in a variable, I set the current logger to NullLogger() and then I set it back to whatever it was before using the saved variable…

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 22, 2024, 9:27am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/6 "2024-03-22T09:27:37Z")

</div>

So the extension runs all of your code like this:

```julia
with_logger(VSCodeLogger()) do
    eval(user_code)
end

```

without modifying the global logger at all. `current_logger` for your `user_code` will be `VSCodeLogger`, but `global_logger` is still the default `ConsoleLogger`. `VSCodeLogger` will forward all log messages it cannot handle to the `global_logger`, which causes a stack overflow if it’s also set to `VSCodeLogger`.

The issue with your code is that you’re reading “local” state and setting global state.

---

<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: [March 22, 2024, 10:13am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/7 "2024-03-22T10:13:38Z")

</div>

There is no function in Julia to set the local/current logger instead of setting the global one, right? (I have tried `current_logger(logger)` but of course it doesn’t exist 🙂 )

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 22, 2024, 10:57am UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/8 "2024-03-22T10:57:32Z")

</div>

There is not. Any chance you can use `with_logger` instead to wrap your code?

---

<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: [March 22, 2024, 1:42pm UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/9 "2024-03-22T13:42:09Z")

</div>

yes, sure… it was just to keep it simmetric with the stderr stuff…

thanks again!

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [March 22, 2024, 1:53pm UTC](https://discourse.julialang.org/t/vscodeserver-vscodelogger-freeze-when-changed-logged-and-ripristinated/111870/10 "2024-03-22T13:53:10Z")

</div>

`redirect_std*` also has a method that takes a function, so you can do

```julia
with_logger(NullLogger()) do
  redirect_stdout(devnull) do
    println("hi")
    @info "hi"
  end
end

```
