Separating stderr based on task

Say I have:

  1. a background task BT which may write to stderr
  2. a main task MT which may also write to stderr

I care about stderr from (MT) but not from (BT) and would like to hide things that come from (BT) as they’re unrelated (long standing HTTP issue…).
If that helps, I know the error messages that may happen on BT which would have to be hidden.

Toy example:

@sync begin
  @spawn begin
    for _ in 1:10
      @error "I don't want to see this"
      sleep(0.1)
    end
  end
  @spawn begin
    for _ in 1:10
      @warn "I do want to see this"
      @error "I also want to see this"
      sleep(0.1)
    end
  end
end

I’m under the impression that redirect_stderr is a global setting, so best I could try to think of is to have a redirect_stderr for the whole @sync block and somehow filter out what gets written to it on the fly but I’ve not been able to do this properly. For the toy code above, the following seems to work but I don’t think it’s generally correct:

@sync begin
  bk_stderr = stderr
  @spawn begin
    for _ in 1:10
      redirect_stderr()
      @error "I don't want to see this"
      sleep(0.1)
    end
  end
  @spawn begin
    for _ in 1:10
      redirect_stderr(bk_stderr)
      @warn "I do want to see this"
      @error "I also want to see this"
      sleep(0.1)
    end
  end
end

(of course it’d be easier if BT did not write to stderr in the first place, and this may happen, but in the meantime it’d be nice to have an alternative).

After a bit of trial and error, it seems that LoggingExtras.jl provides most of what I was looking for (in particular a fairly straightforward way to filter out some logs based on their provenance and leaving everything else the same).

I don’t think it fully resolves the original question as knowing whether a message came from a specific Task requires additional work but in my case I know that everything from that Task is also from a specific module (HTTP) and so can filter on that.