#Logging to the TUI while developing module

1 messages · Page 1 of 1 (latest)

cyan seal
#

This is my first attempt to use dagger and I'm trying to print arbitrary values using fmt.Printf (quick and dirty development logging) to the TUI console but they always get printed on top. I want to print progress values like state changes inside for loops along with the execution of functions like withExec.
Is this possible?
I've checked the documentation, Go SDK and examples but I don't find anything like this, I might be trying to use the framwork in a wrong way but I want to be sure.

Thanks!

agile quail
vocal aurora
#

Hey @agile quail do you have an example of that? Here's what I see in my simple test

// Test logging inside of a for loop
func (m *GoLogging) ForLoopTest(ctx context.Context) {
    container := dag.Container().From("alpine:latest")

    for i := 1; i <= 5; i++ {
        fmt.Printf("Inside the loop for Number: %d\n", i)
        container.WithExec([]string{"echo", fmt.Sprintf("Number: %d", i)}).Stdout(ctx)
    }
}

all my fm.Printf lines are grouped together in the log, doing -vvv does not really make a difference in this example.

cyan seal
#

@vocal aurora That's what I'm getting: the output of fmt.Printf always appears on top instead of being alongside the container output

agile quail
# vocal aurora Hey <@336241811179962368> do you have an example of that? Here's what I see in m...

@vocal aurora @cyan seal you're right. I was testing with a very simple example and I wasn't getting the same output you're getting. I think what's happening here is that whatever the function prints gets assigned as a Log of the function span. That's why you don't see it below each WithExec since those are effectively different spans.

Not sure if there's a "proper" way to handle this. cc @wind zinc ?

vocal aurora
# agile quail <@920499459484418068> <@633803605459075102> you're right. I was testing with a ...

I was trying to add otel spans (which I assume is the "right" way to do it) but I couldn't get this to show me anything at all.


// Test logging inside of a for loop
func (m *GoLogging) ForLoopTest(ctx context.Context) {
    container := dag.Container().From("alpine:latest")

    for i := 1; i <= 5; i++ {
        span := trace.SpanFromContext(ctx)
        span.AddEvent(fmt.Sprintf("Inside the loop for Number: %d", i))
        container.WithExec([]string{"echo", fmt.Sprintf("Number: %d", i)}).Stdout(ctx)
    }
}
#

But its very possible I am not doing otel correctly 😇

agile quail
vocal aurora
wind zinc
cyan seal
#

@agile quail As you said the output of fmt.Printf becomes part of the output of my function instead of the loog that has the WithExec calls. Is ther a log function or something like that?
@vocal aurora I don't really understand what "otel" means. Could you point me to some documentation about it?

vocal aurora
cyan seal
vocal aurora
cyan seal
vocal aurora
cyan seal
#

@wind zinc @vocal aurora the otel code you gave me did what I wanted!

    ctx, span := Tracer().Start(ctx, "running unit tests with Node "+version)
    defer telemetry.End(span, func() error { return rerr })

The only detail I have is that I don't know where rerr comes from so to test I put nil in the End function to make it work

wind zinc
#

nice - that rerr value is just an error that you can return if you want the span (as it's called) to show up with an errored state

agile quail