disable colored log lines when output is not a terminal (#1477) (#2050)

This commit is contained in:
Alessandro Ros 2023-07-11 20:08:00 +02:00 committed by GitHub
parent 3e8a1674ab
commit 5e0b9b8426
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 37 additions and 20 deletions

1
go.mod
View File

@ -26,6 +26,7 @@ require (
github.com/stretchr/testify v1.8.4 github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.11.0 golang.org/x/crypto v0.11.0
golang.org/x/net v0.12.0 golang.org/x/net v0.12.0
golang.org/x/term v0.10.0
gopkg.in/yaml.v2 v2.4.0 gopkg.in/yaml.v2 v2.4.0
) )

2
go.sum
View File

@ -255,6 +255,8 @@ golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo= golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo= golang.org/x/term v0.9.0/go.mod h1:M6DEAAIenWoTxdKrOltXcmDY3rSplQUkrvaDU5FcQyo=
golang.org/x/term v0.10.0 h1:3R7pNqamzBraeqj/Tj8qt1aQ2HpmlC+Cx/qL/7hn4/c=
golang.org/x/term v0.10.0/go.mod h1:lpqdcUyK/oCiQxvxVrppt5ggO2KCZ5QblwqPnfZ6d5o=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

View File

@ -1,5 +1,9 @@
package logger package logger
import (
"time"
)
// Destination is a log destination. // Destination is a log destination.
type Destination int type Destination int
@ -15,6 +19,6 @@ const (
) )
type destination interface { type destination interface {
log(Level, string, ...interface{}) log(time.Time, Level, string, ...interface{})
close() close()
} }

View File

@ -3,6 +3,7 @@ package logger
import ( import (
"bytes" "bytes"
"os" "os"
"time"
) )
type destinationFile struct { type destinationFile struct {
@ -21,9 +22,9 @@ func newDestinationFile(filePath string) (destination, error) {
}, nil }, nil
} }
func (d *destinationFile) log(level Level, format string, args ...interface{}) { func (d *destinationFile) log(t time.Time, level Level, format string, args ...interface{}) {
d.buf.Reset() d.buf.Reset()
writeTime(&d.buf, false) writeTime(&d.buf, t, false)
writeLevel(&d.buf, level, false) writeLevel(&d.buf, level, false)
writeContent(&d.buf, format, args) writeContent(&d.buf, format, args)
d.file.Write(d.buf.Bytes()) d.file.Write(d.buf.Bytes())

View File

@ -3,20 +3,27 @@ package logger
import ( import (
"bytes" "bytes"
"os" "os"
"time"
"golang.org/x/term"
) )
type destinationStdout struct { type destinationStdout struct {
useColor bool
buf bytes.Buffer buf bytes.Buffer
} }
func newDestionationStdout() destination { func newDestionationStdout() destination {
return &destinationStdout{} return &destinationStdout{
useColor: term.IsTerminal(int(os.Stdout.Fd())),
}
} }
func (d *destinationStdout) log(level Level, format string, args ...interface{}) { func (d *destinationStdout) log(t time.Time, level Level, format string, args ...interface{}) {
d.buf.Reset() d.buf.Reset()
writeTime(&d.buf, true) writeTime(&d.buf, t, d.useColor)
writeLevel(&d.buf, level, true) writeLevel(&d.buf, level, d.useColor)
writeContent(&d.buf, format, args) writeContent(&d.buf, format, args)
os.Stdout.Write(d.buf.Bytes()) os.Stdout.Write(d.buf.Bytes())
} }

View File

@ -3,6 +3,7 @@ package logger
import ( import (
"bytes" "bytes"
"io" "io"
"time"
) )
type destinationSysLog struct { type destinationSysLog struct {
@ -21,9 +22,9 @@ func newDestinationSyslog() (destination, error) {
}, nil }, nil
} }
func (d *destinationSysLog) log(level Level, format string, args ...interface{}) { func (d *destinationSysLog) log(t time.Time, level Level, format string, args ...interface{}) {
d.buf.Reset() d.buf.Reset()
writeTime(&d.buf, false) writeTime(&d.buf, t, false)
writeLevel(&d.buf, level, false) writeLevel(&d.buf, level, false)
writeContent(&d.buf, format, args) writeContent(&d.buf, format, args)
d.syslog.Write(d.buf.Bytes()) d.syslog.Write(d.buf.Bytes())

View File

@ -74,12 +74,11 @@ func itoa(i int, wid int) []byte {
return b[bp:] return b[bp:]
} }
func writeTime(buf *bytes.Buffer, doColor bool) { func writeTime(buf *bytes.Buffer, t time.Time, useColor bool) {
var intbuf bytes.Buffer var intbuf bytes.Buffer
// date // date
now := time.Now() year, month, day := t.Date()
year, month, day := now.Date()
intbuf.Write(itoa(year, 4)) intbuf.Write(itoa(year, 4))
intbuf.WriteByte('/') intbuf.WriteByte('/')
intbuf.Write(itoa(int(month), 2)) intbuf.Write(itoa(int(month), 2))
@ -88,7 +87,7 @@ func writeTime(buf *bytes.Buffer, doColor bool) {
intbuf.WriteByte(' ') intbuf.WriteByte(' ')
// time // time
hour, min, sec := now.Clock() hour, min, sec := t.Clock()
intbuf.Write(itoa(hour, 2)) intbuf.Write(itoa(hour, 2))
intbuf.WriteByte(':') intbuf.WriteByte(':')
intbuf.Write(itoa(min, 2)) intbuf.Write(itoa(min, 2))
@ -96,38 +95,38 @@ func writeTime(buf *bytes.Buffer, doColor bool) {
intbuf.Write(itoa(sec, 2)) intbuf.Write(itoa(sec, 2))
intbuf.WriteByte(' ') intbuf.WriteByte(' ')
if doColor { if useColor {
buf.WriteString(color.RenderString(color.Gray.Code(), intbuf.String())) buf.WriteString(color.RenderString(color.Gray.Code(), intbuf.String()))
} else { } else {
buf.WriteString(intbuf.String()) buf.WriteString(intbuf.String())
} }
} }
func writeLevel(buf *bytes.Buffer, level Level, doColor bool) { func writeLevel(buf *bytes.Buffer, level Level, useColor bool) {
switch level { switch level {
case Debug: case Debug:
if doColor { if useColor {
buf.WriteString(color.RenderString(color.Debug.Code(), "DEB")) buf.WriteString(color.RenderString(color.Debug.Code(), "DEB"))
} else { } else {
buf.WriteString("DEB") buf.WriteString("DEB")
} }
case Info: case Info:
if doColor { if useColor {
buf.WriteString(color.RenderString(color.Green.Code(), "INF")) buf.WriteString(color.RenderString(color.Green.Code(), "INF"))
} else { } else {
buf.WriteString("INF") buf.WriteString("INF")
} }
case Warn: case Warn:
if doColor { if useColor {
buf.WriteString(color.RenderString(color.Warn.Code(), "WAR")) buf.WriteString(color.RenderString(color.Warn.Code(), "WAR"))
} else { } else {
buf.WriteString("WAR") buf.WriteString("WAR")
} }
case Error: case Error:
if doColor { if useColor {
buf.WriteString(color.RenderString(color.Error.Code(), "ERR")) buf.WriteString(color.RenderString(color.Error.Code(), "ERR"))
} else { } else {
buf.WriteString("ERR") buf.WriteString("ERR")
@ -150,7 +149,9 @@ func (lh *Logger) Log(level Level, format string, args ...interface{}) {
lh.mutex.Lock() lh.mutex.Lock()
defer lh.mutex.Unlock() defer lh.mutex.Unlock()
t := time.Now()
for _, dest := range lh.destinations { for _, dest := range lh.destinations {
dest.log(level, format, args...) dest.log(t, level, format, args...)
} }
} }