charmbracelet/log

expose `(*Logger).log`

tmm1 opened this issue · 1 comments

tmm1 commented

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", ...)

You could use function variables:

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