charmbracelet / log

A minimal, colorful Go logging library 🪵
MIT License
2.4k stars 67 forks source link

expose `(*Logger).log` #44

Closed tmm1 closed 1 year ago

tmm1 commented 1 year ago

It would be nice to be able to vary log level programatically

For example the same message in some cases might be debug, but in other cases a warning.

if rejectFailure {
  log.Error("Request NOK", "url", url, "status", status, "query", query)
} else {
  log.Debug("Request NOK", "url", url, "status", status, "query", query)
}

vs

log.Log(lo.If(rejectFailure, log.ErrorLevel).Else(log.DebugLevel), "Request NOK", ...)

I guess now that I think about it, an alternative already possible is to vary the function pointer..

lo.If(rejectFailure, log.Error).Else(log.Debug)("Request NOK", ...)
aymanbagabas commented 1 year ago

You could use function variables:

logger := log.Debug
if rejectFailure {
  logger = log.Error
}
logger("Request NOK", "url", url, ...)