#how to append prefix to variadic string argument?

8 messages · Page 1 of 1 (latest)

silent prawn
#

I want to add "[TRACE]" to my log function arguments, and/or eventually wrap them in {} etc... for formatting json logs eventually.
How might I most efficiently/effectively append a prefix and/or suffix without copying values more than I need to?

verbal tartan
#

make a function that does so and you call instead of direct?

high lily
#

Probably use slog and optionally pack additional info into the context. There are a lot of ctxlog packages out there that you could use or examine

#

If it's just in one package, a utility function would work, but if it's to fully replace your logging solution, you might prefer something more versatile.

red solar
#

You could create a wrapper for your logger like;

func logWithLevel(level, msg string, args ...any) {
    log.Printf(fmt.Sprintf("[%s] %s\n", strings.ToUpper(level), msg), args...)
}

UPDATE: If that is what you want to do that is. Agree with others here though - log/slog or any other structured logging lib is a better solution.

indigo ibex
#

Are you sure that you don't want a log level by the name of TRACE or something?

indigo ibex
#

I advise against coming up with custom solutions when log/slog offers structured logging. Don't modify text. Keep it structured and any additional information should be attributes, making it easy to search and read without modifying or packing information into messages.