# Update variable in logged message without printing a new line?

**URL:** https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755
**Category:** General Usage
**Created:** [December 28, 2019, 2:23pm UTC](https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755 "2019-12-28T14:23:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [December 28, 2019, 2:23pm UTC](https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755/1 "2019-12-28T14:23:00Z")

</div>

I’m wondering if it’s possible to update a logged message without printing a new line. I’d like to log some information to the console on each iteration of a loop but, rather than printing a new `@info` statement on each iteration, I’d like to simply update the existing message to reflect the new value of the variable.

For example, instead of this:

```julia
function testlog()
    i = 1
    for n in 1:5
        @info "Processing file number $i"
        i += 1
    end
end

julia> testlog()
[ Info: Processing file number 1
[ Info: Processing file number 2
[ Info: Processing file number 3
[ Info: Processing file number 4
[ Info: Processing file number 5

```

I’d like to be able to do something like this:

```julia
function testlog()
    i = 1
    @info "Processing file number $i"
    for n in 1:5
        i += 1
    end
end

julia> testlog()
[ Info: Processing file number 1 # This updates on each iteration

```

Is this possible? If not, is it possible to have the “processing file number #” print as part of the same `Info` statement? What I mean is, instead of the word “Info” showing up, can I just append the message to an existing `@info` event? It would look something like this:

```julia
[ Info: Processing file number 1
| Processing file number 2
| Processing file number 3
....

```

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [December 28, 2019, 2:50pm UTC](https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755/2 "2019-12-28T14:50:29Z")

</div>

Aware of [ProgressMeter.jl](https://github.com/timholy/ProgressMeter.jl)? It supports updating the message in place with arbitrary number of variables, nested loops, asynchronously.

---

<div class="post-metadata">

### Author: ![swissr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swissr/32/208_2.png) [@swissr](https://discourse.julialang.org/u/swissr)
#### Post date: [December 28, 2019, 10:31pm UTC](https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755/3 "2019-12-28T22:31:28Z")

</div>

For a quick and dirty alternative you could use an [ANSI escape code](https://en.wikipedia.org/wiki/Ansi_escape_codes#Escape_sequences) to move the cursor back 1000 positions after printing the string (we are at the start of the line again).

```julia
function testlog()
    i = 1
    for n in 1:5
        print("Processing file number $i\u001b[1000D")
        sleep(0.3) # simulate some work
        i += 1
    end
end

testlog()

```

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [December 28, 2019, 11:39pm UTC](https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755/4 "2019-12-28T23:39:21Z")

</div>

> Aware of [ProgressMeter.jl](https://github.com/timholy/ProgressMeter.jl)?

I was not aware, thanks!

> For a quick and dirty alternative you could use an [ANSI escape code](https://en.wikipedia.org/wiki/Ansi_escape_codes#Escape_sequences) to move the cursor back 1000 positions after printing the string (we are at the start of the line again).

This is clever, thank you 😉

---

<div class="post-metadata">

### Author: ![essenciary](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/essenciary/32/210469_2.png) [@essenciary](https://discourse.julialang.org/u/essenciary)
#### Post date: [December 29, 2019, 3:55pm UTC](https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755/5 "2019-12-29T15:55:51Z")

</div>

In Genie I successfully made use of the `REPL.Terminals` API to provide a status for loading the different parts of an app.

 ![My Movie](https://global.discourse-cdn.com/julialang/original/3X/f/e/fef0ca394af83b256f191a57759367fc4cb5ec71.gif)

The functionality is wrapped in a helper function which prints to screen:

```julia
function replprint(output::String, terminal;
                    newline::Int = 0, clearline::Int = 1, color::Symbol = :white, bold::Bool = false, sleep_time::Float64 = 0.2,
                    prefix::String = "", prefix_color::Symbol = :green, prefix_bold::Bool = true)
  if clearline > 0
    for i in 1:(clearline+1)
      REPL.Terminals.clear_line(terminal)
    end
  end

  isempty(prefix) || printstyled(prefix, color = prefix_color, bold = prefix_bold)
  printstyled(output, color = color, bold = bold)

  if newline > 0
    for i in 1:newline
      println()
    end
  end

  sleep(sleep_time)
end

```

The function is used like this:

```julia
function load(; context::Union{Module,Nothing} = nothing) :: Nothing
  t = Terminals.TTYTerminal("", stdin, stdout, stderr)

  replprint("initializers", t, clearline = 0, prefix = "Loading ")
  load_initializers(context = context)

  replprint("helpers", t, prefix = "Loading ")
  load_helpers()

  replprint("lib", t, prefix = "Loading ")
  load_libs()

  replprint("resources", t, prefix = "Loading ")
  load_resources()

  replprint("plugins", t, prefix = "Loading ")
  load_plugins(context = context)

  replprint("routes", t, prefix = "Loading ")
  load_routes_definitions(context = context)

  replprint("Ready! ", t, clearline = 2, color = :green, bold = :true)
  println()

  nothing
end

```

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [December 30, 2019, 12:49am UTC](https://discourse.julialang.org/t/update-variable-in-logged-message-without-printing-a-new-line/32755/6 "2019-12-30T00:49:43Z")

</div>

Thanks so much for sharing! I love Genie.jl, by the way 😍
