Logging for later analysis

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:

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!

SimpleLogger takes an argument of type IO.

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

1 Like

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