charmbracelet / log

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

Allow the user to force color output on non-tty devices #63

Closed mgale closed 1 year ago

mgale commented 1 year ago

I used this workflow when using io.Pipe, allowing me to redirect the logging output to a viewport.

bashbunni commented 1 year ago

Hey @mgale do you mind providing an example of how you used the changes in this PR so we can test? Thank you!

mgale commented 1 year ago

Hey @mgale do you mind providing an example of how you used the changes in this PR so we can test? Thank you!

Hello @bashbunni sorry for this drive by PR, also I should have added tests.

I tried adding tests on my own but things only work correctly when I execute the tests from the command line using go test. If I try executing the tests via vscode the colour output is always disabled.

How I am using this functionality...

  1. In my CLI app I have a viewport where I want to stream events as things are happening (making API calls, etc)
  2. I wanted to use the log format / output as it is setup already with timestamps and colours.

I created an events file:

f, err := os.OpenFile(ltEventLog, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
...
eventLogger := log.NewWithOptions(f, log.Options{ReportTimestamp: true, ForceColors: true})
eventLogger.Info(...)

Then I have a separate go routine that is using tail.TailFile, reads lines from the file and updates the view by doing a p.Send() method on a tea.Program.

aymanbagabas commented 1 year ago

Hi @mgale, could you try this PR? It takes a simpler approach to force the color output https://github.com/charmbracelet/log/pull/69

mgale commented 1 year ago

Hi @mgale, could you try this PR? It takes a simpler approach to force the color output #69

Hello, FYI I am on vacation for the month of August however I managed to give this a quick test and it worked for me. Thank you.

mgale commented 1 year ago

Closing PR in favor of: https://github.com/charmbracelet/log/pull/69