1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/internal/logext/writer.go
Carlos Alexandro Becker fe7e2123bd
feat: replacing the log library (#3139)
* feat: replacing logs

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: tests et al

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* feat: update termenv/lipgloss

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* wip: output

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: pin dep

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: update

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: tests

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: deps

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* fix: dep

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2022-06-21 21:11:15 -03:00

84 lines
1.5 KiB
Go

package logext
import (
"bytes"
"io"
"os"
"strings"
"github.com/caarlos0/log"
)
// Output type of the log output.
type Output int
const (
// Info usually is used with stdout.
Info Output = iota
// Error usually is used with stderr.
Error
)
// NewWriter creates a new log writer.
func NewWriter(fields log.Fields, out Output) io.Writer {
return NewConditionalWriter(fields, out, false)
}
// NewConditionalWriter creates a new log writer that only writes when the given condition is met or debug is enabled.
func NewConditionalWriter(fields log.Fields, out Output, condition bool) io.Writer {
if condition || isDebug() {
return logWriter{
ctx: newLogger(fields),
out: out,
}
}
return io.Discard
}
type logWriter struct {
ctx *log.Entry
out Output
}
func (w logWriter) Write(p []byte) (int, error) {
for _, line := range strings.Split(toString(p), "\n") {
switch w.out {
case Info:
w.ctx.Info(line)
case Error:
w.ctx.Warn(line)
}
}
return len(p), nil
}
func newLogger(fields log.Fields) *log.Entry {
handler := log.New(currentWriter())
handler.IncreasePadding()
return handler.WithFields(fields)
}
func currentWriter() io.Writer {
logger, ok := log.Log.(*log.Logger)
if !ok {
return os.Stderr
}
return logger.Writer
}
func isDebug() bool {
return logLevel() == log.DebugLevel
}
func logLevel() log.Level {
if logger, ok := log.Log.(*log.Logger); ok {
return logger.Level
}
return log.InfoLevel
}
func toString(b []byte) string {
return string(bytes.TrimSuffix(b, []byte("\n")))
}