How to add custom label with dynamic value in k6 logs with log-output forwarding?

The default message format for k6 with console.log is

time="2023-07-19T10:40:20Z" level=info msg="hello" source=console

I want to modify this message and let’s say add a custom label like trace=1234 method=login

I want to do this because the logs will be sent to Loki and then I can search by labels like method

How can I achieve this?

I know, I can achieve this by creating a custom javascript extension with a custom log method. I have tried this like following for testing purposes:

    package tracelogger
    
    import (
        "fmt"
    	"time"
    
        "go.k6.io/k6/js/modules"
    )
    
    // init is called by the Go runtime at application startup.
    func init() {
        modules.Register("k6/x/tracelogger", new(TraceLogger))
    }
    
    type TraceLogger struct{
    }
    
    func (l *TraceLogger) Log() {
    		currentTime := time.Now()
    		formattedTime := currentTime.Format("2006-01-02T15:04:05Z07:00")
    		fmt.Printf("time=\"%s\" level=info trace=1234 method=abc msg=\"%s\" source=console", formattedTime,  "hello")
    }

Now in the JS test file

    import tracelogger from 'k6/x/tracelogger';
    
    export const options = {};
    
    export function setup() { }
    
    export default function LoadTest() {
      tracelogger.log();
    }

It does print the output but in the standard console as expected

    time="2023-07-19T12:08:18Z" level=info trace=1234 method=abc msg="hello" source=console

Now the problem is I am also forwarding the log output to Loki

    k6 run --log-output=loki=http://localhost:3100/loki/api/v1/push,label.qa=k6 test.js

The log does not get forwarded to Loki, how do I log so that the log gets forwarded to loki?

I also tried with --log-format=raw and direct console.log

    console.log(`trace=1234 msg="aaaa"`);

but the labelling doesn’t work correctly in loki

enter image description here