charmbracelet/log

No color when io.Multiwriter is used

cooperspencer opened this issue · 4 comments

Hi,
I was trying to couple log with io.Multiwriter and noticed that the colors are gone.

package main

import (
	"io"
	"os"
	"path"
	"time"

	"github.com/charmbracelet/log"
	"gopkg.in/natefinch/lumberjack.v2"
)

func NewRollingFile(dir string) io.Writer {
	if dir != "" {
		if err := os.MkdirAll(dir, 0o744); err != nil {
			log.Error(err, "path", dir)

			return nil
		}
	} else {
		dir = "."
	}

	return &lumberjack.Logger{
		Filename: path.Join(dir, "test"),
		MaxAge:   7, // days
	}
}

func main() {
	writers := []io.Writer{}
	writers = append(writers, os.Stderr)
	writers = append(writers, NewRollingFile("dir"))
	writer := io.MultiWriter(writers...)
	logger := log.NewWithOptions(writer, log.Options{
		ReportCaller:    true,
		ReportTimestamp: true,
		TimeFormat:      time.Kitchen,
	})
	logger.Info("this is a test")
}

#69 will probably solve this

M1ndo commented

Can confirm this issue still persists as no fixes have been proposed yet.

#69 is now part of v0.2.5

this solves the issue for me, thanks