# Including a variable outside a function in a log message

**URL:** https://discourse.julialang.org/t/including-a-variable-outside-a-function-in-a-log-message/84347
**Category:** General Usage
**Tags:** question, logging
**Created:** [July 17, 2022, 7:47am UTC](https://discourse.julialang.org/t/including-a-variable-outside-a-function-in-a-log-message/84347 "2022-07-17T07:47:15Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![florian](https://avatars.discourse-cdn.com/v4/letter/f/898d66/32.png) [@florian](https://discourse.julialang.org/u/florian)
#### Post date: [July 17, 2022, 7:47am UTC](https://discourse.julialang.org/t/including-a-variable-outside-a-function-in-a-log-message/84347/1 "2022-07-17T07:47:16Z")

</div>

Assume I have a function that does some calculation on a piece of data and I want to log when somewthing goes wrong:

```julia
struct MyData
    id::String
    number::Number
end

function main()

    data = [MyData("a", -1), MyData("b", 1)]

    for case in data
        id = case.id
        num = case.number
        
        my_calculation(num)
    end
    
end

function my_calculation(x::Number)

    if x < 0
        @warn "$(x) is smaller than zero!"
        return NaN
    end

    return sqrt(x)
end

main()

```

Can I somehow include the id of the data for which the calculation in the log message, without passing `id` as an argument to `my_calculation`? It seems “wrong” to do that since the calculation itself has nothing to do with the id.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [July 17, 2022, 9:54am UTC](https://discourse.julialang.org/t/including-a-variable-outside-a-function-in-a-log-message/84347/2 "2022-07-17T09:54:42Z")

</div>

No. If `my_calculation` isn’t given `case.id` somehow then it just has no access. If you can’t change that, you could `@warn` in `main` instead; you just need `my_calculation` to indicate something went wrong, like returning an indicator output (`NaN`, `nothing`) for `main` to check (`isnan`, `isnothing`) or `throw`ing an error for `main` to handle (`try catch finally`).
