# Access logged values

**URL:** https://discourse.julialang.org/t/access-logged-values/16185
**Category:** General Usage
**Tags:** question
**Created:** [October 11, 2018, 3:39pm UTC](https://discourse.julialang.org/t/access-logged-values/16185 "2018-10-11T15:39:04Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 11, 2018, 3:39pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/1 "2018-10-11T15:39:05Z")

</div>

I am trying to use the new `Logging` system. I did a `@warn "a warning" x y z`, where `x,y,z` are complicated structures that I want to analyze. I see that `x,y,z` get printed with the warning. Is there a way to access their values programmatically?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 11, 2018, 3:54pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/2 "2018-10-11T15:54:17Z")

</div>

Are you trying to access the content of the logged message or the content of the variables?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 11, 2018, 3:57pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/3 "2018-10-11T15:57:13Z")

</div>

The contents of the variables, as they were when the message was generated.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 11, 2018, 4:07pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/4 "2018-10-11T16:07:09Z")

</div>

Well that’s going to depend on what fields are available for your struct. What type/package/struct are you using/talking about?

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 11, 2018, 4:14pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/5 "2018-10-11T16:14:28Z")

</div>

For the purposes of this question, I think we can assume that `x` is a `Vector` (for example). I want to access this value of `x` when the message was generated.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 11, 2018, 4:49pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/6 "2018-10-11T16:49:51Z")

</div>

Well that’s just regular indexing - see [here](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing-1).

It would be helpful if you could provide more information about what you’re trying to do though…

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 11, 2018, 5:16pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/7 "2018-10-11T17:16:21Z")

</div>

I think I wasn’t clear enough. Here is an example.

```julia
function f(x)
y = 2x
@warn "something wrong" y
end

```

Then at the REPL, or somewhereelse

```julia
julia> f(2)
┌ Warning: something wrong
│ y = 4
└ @ Main REPL[1]:3

```

At this point, how do I access the value of `y` programmatically? Is there something like `LoggedValues[:y]` that returns the value of `y` when the message was issued?

Suppose that `y` here is actually a complicated struct, so just looking at the printed value is no help.

---

<div class="post-metadata">

### Author: ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)
#### Post date: [October 15, 2018, 3:33pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/8 "2018-10-15T15:33:01Z")

</div>

Maybe I am confused here. I interpreted the following quote from the docs:

> Second, 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. For example, to attach the local array variable `A` and the sum of a vector `v` as the key `s` you can use. (…)

as implying that there would be a way for me to access logged objects programmatically. For example, if something goes wrong in a computation involving a large matrix, I want to access the matrix programmatically (not just print it) to see what is going on.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 15, 2018, 4:26pm UTC](https://discourse.julialang.org/t/access-logged-values/16185/9 "2018-10-15T16:26:56Z")

</div>

No, that key-value pairing is just for printing and is not generally accessible after the message has been generated. If you would make that available later on after logging, you have to hang onto that old state somehow, which would be quite inefficent for some objects that would be logged.

You can check the code that’s doing that in the default `SimpleLogger` [here](https://github.com/JuliaLang/julia/blob/d64ccdb7c1d1fb3370be1c0c1b96bd8bb6f5fc12/base/logging.jl#L541-L543).

If you’re trying to do [error handling](https://docs.julialang.org/en/v1/manual/control-flow/#Exception-Handling-1), you’d be better off doing the handling explicitly in a `try-catch`-block (or just throw an error and let the user handle it). The logging messages aren’t meant to be used that way - they’re generally only for user side informational purposes about an internal state - not for deciding on how to continue. If the struct you’re logging is too big, why log the whole struct?
