I’m trying to add some logging to an existing codebase.
I am trying to add some timing information to these logs.
Logs are quite simple, they are just println
statements wrapped by some functions.
I would like to be able to print floating point values in a fixed width format, so that many lines of logs are relatively easy to read.
Is this possible?
println("$(datetimenow), $(interval), $(logtext))
Here, interval
is a floating point value with some timing information.
Sukera
2
You can use the Printf
stblib for that:
4 Likes
Alternatively, it might be possible to achieve something reasonable without a package using Julia’s base functions round()
and rpad()
.
For example:
aux(x, k, n) = rpad(round(x, digits=k), n)
datetimenow, interval, logtext = eachcol(rand(10,3))
k, n = 4, 11
print("$(rpad("datetimenow", n)) $(rpad("interval", n)) $(rpad("logtext", n))")
foreach((x,y,z) -> println("$(aux(x,k,n)) $(aux(y,k,n)) $(aux(z,k,n))"), datetimenow, interval, logtext)
# result:
datetimenow interval logtext
0.4307 0.6669 0.417
0.2751 0.137 0.8011
0.438 0.1341 0.2163
0.7719 0.6215 0.9263
0.0244 0.6474 0.5079
0.6669 0.2355 0.1
0.2717 0.5071 0.1892
0.8993 0.9825 0.9196
0.419 0.3236 0.2753
0.2838 0.3741 0.1114
1 Like
aplavin
4
PyFormattedStrings.jl provides Python f-string format in Julia, quite convenient for this purpose:
julia> x = 123.4567
julia> y = "abc"
julia> f"{x:<10.2f} | {y:8s}"
"123.46 | abc"
4 Likes