charmbracelet / log

A minimal, colorful Go logging library 🪵
MIT License
2.29k stars 66 forks source link

Colors not working when redirecting stdout to file #35

Closed tmm1 closed 1 year ago

tmm1 commented 1 year ago

I have a simple go program that logs to stderr, and produces some output that is redirected to a file.

It seems color detection is based on stdout status instead of stderr status?

tmm1 commented 1 year ago

To clarify further.. the logs are still styled with brightness, but not colors. When I redirect stderr to a file, then it comes through correctly without any styling at all.

aymanbagabas commented 1 year ago

This seems to be a bug in the current used version of Termenv. For the time being, you could work around this bug using

package main

import (
  ...
  "github.com/muesli/termenv"
  "github.com/charmbracelet/lipgloss"
)

func main() {
  lipgloss.SetColorProfile(termenv.TrueColor)
  // or
  lipgloss.SetColorProfile(termenv.ANSI256)
}
tmm1 commented 1 year ago

Thanks, that works for now.

I tried to figure out where the bug is in termenv but didn't have much luck. LMK if you discover any more details.

aymanbagabas commented 1 year ago

Log uses Lipgloss v0.6 which uses Termenv v0.11. Termenv assumes using os.Stdout and falls back to no-colors if it's not a TTY. https://github.com/muesli/termenv/blob/v0.11.0/termenv.go#L45

tmm1 commented 1 year ago

Aha okay that explains it. I was looking at the code for master and couldn't see that.

muesli commented 1 year ago

Note: you can typically enforce color output by setting CLICOLOR_FORCE to 1.

aymanbagabas commented 1 year ago

Fixed in https://github.com/charmbracelet/log/commit/a2100d4992f68bc3d146c025ee81d850b5003162

tmm1 commented 1 year ago

Thanks, confirmed fixed!