# Logging for later analysis

**URL:** <https://discourse.julialang.org/t/logging-for-later-analysis/92315>\
**Category:** General Usage\
**Tags:** logging\
**Created:** [December 30, 2022, 12:48pm UTC](https://discourse.julialang.org/t/logging-for-later-analysis/92315 "2022-12-30T12:48:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gokhan\_Kof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gokhan_kof/32/43267_2.png) [@Gokhan\_Kof](https://discourse.julialang.org/u/Gokhan_Kof)\
**Post date:** [December 30, 2022, 12:48pm UTC](https://discourse.julialang.org/t/logging-for-later-analysis/92315/1 "2022-12-30T12:48:16Z")

</div>

Hi,

Inside `Logging` documentation, it says “the logging tools allow you to attach arbitrary data to each event as a set of key–value pairs. This allows you to capture local variables and other program state for later analysis”.

Well, I want to do that:

```julia
using Logging

times7 = Dict{Int, Int}()

function logtest(n::Int)
  for i in 1:n
    if i % 7 == 0
      @info "divisible by 7" times7[i] = (i / 7)^2
    end  
  end
end

logger = SimpleLogger()

with_logger(logger) do 
  logtest(100)
end

```

So, I want to view the logged records later on (`records` inside `logger`). Maybe to compute some statistics or plotting the algorithm history, etc. Is this possible?

Thanks!

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [December 30, 2022, 1:48pm UTC](https://discourse.julialang.org/t/logging-for-later-analysis/92315/2 "2022-12-30T13:48:29Z")

</div>

`SimpleLogger` takes an argument of type `IO`.

```julia
julia> using Logging

julia> iobuf = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> logger = SimpleLogger(iobuf)
Base.CoreLogging.SimpleLogger(IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=0, maxsize=Inf, ptr=1, mark=-1), Info, Dict{Any, Int64}())

julia> with_logger(logger) do
           logtest(100)
       end

julia> println(String(take!(iobuf)))
┌ Info: divisible by 7
│ times7[i] = 1.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 4.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 9.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 16.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 25.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 36.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 49.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 64.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 81.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 100.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 121.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 144.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 169.0
└ @ Main REPL[44]:4
┌ Info: divisible by 7
│ times7[i] = 196.0
└ @ Main REPL[44]:4

```

You could also just make a custom logger:  
[https://docs.julialang.org/en/v1/stdlib/Logging/#AbstractLogger-interface](https://docs.julialang.org/en/v1/stdlib/Logging/#AbstractLogger-interface)

---

<div class="post-metadata">

**Author:** ![Gokhan\_Kof](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gokhan_kof/32/43267_2.png) [@Gokhan\_Kof](https://discourse.julialang.org/u/Gokhan_Kof)\
**Post date:** [December 30, 2022, 2:27pm UTC](https://discourse.julialang.org/t/logging-for-later-analysis/92315/3 "2022-12-30T14:27:05Z")

</div>

No really what I meant but custom logger it is then. Thanks!
