Why is the logger Infof method not logging as expected?

I’m working on a Go application using a popular logger package and trying to use the Infof method for formatted (structured-style) logging. My environment is Go 1.18 on Ubuntu 20.04.

When I use Infof with dynamic values, I either don’t see any output in the console/log file or the formatting doesn’t behave as expected. I’ve tried different format strings, but the logs still don’t appear correctly.

How can I properly use Infof, and could this be related to logger configuration or log level settings?

The Infof method works similarly to fmt.Printf. If formatting does not behave correctly, the most common issue is a mismatch between format specifiers and the values passed.

Example of correct usage:

log.Infof("User %s logged in at %s", username, time.Now().Format(time.RFC3339))

Make sure:

%s is used for strings

%d is used for integers

%v is used for generic values

The number of format specifiers matches the number of arguments

Incorrect example (will behave unexpectedly):

log.Infof("User %d logged in", username) // %d used for string

If format specifiers don’t match argument types, output may look incorrect or incomplete.

If logs are not appearing at all, the issue is very likely related to log level configuration.

Many logger packages suppress logs below a certain level. If the logger is set to Warn or Error, Infof logs will not appear.

Make sure your logger level allows Info logs:

logger.SetLevel(logger.InfoLevel)
logger.SetOutput(os.Stdout)

Check:

The current log level configuration

Whether environment variables override the level

Whether logs are being written to a file instead of console

Sometimes logs are working correctly but being written to a different output destination.