Skip to content

Commit

Permalink
disable colored log lines when output is not a terminal (#1477)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Jul 11, 2023
1 parent 3e8a167 commit 630741c
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.11.0
golang.org/x/net v0.12.0
golang.org/x/term v0.10.0
gopkg.in/yaml.v2 v2.4.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
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.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
6 changes: 5 additions & 1 deletion internal/logger/destination.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package logger

import (
"time"
)

// Destination is a log destination.
type Destination int

Expand All @@ -15,6 +19,6 @@ const (
)

type destination interface {
log(Level, string, ...interface{})
log(time.Time, Level, string, ...interface{})
close()
}
5 changes: 3 additions & 2 deletions internal/logger/destination_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logger
import (
"bytes"
"os"
"time"
)

type destinationFile struct {
Expand All @@ -21,9 +22,9 @@ func newDestinationFile(filePath string) (destination, error) {
}, 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()
writeTime(&d.buf, false)
writeTime(&d.buf, t, false)
writeLevel(&d.buf, level, false)
writeContent(&d.buf, format, args)
d.file.Write(d.buf.Bytes())
Expand Down
15 changes: 11 additions & 4 deletions internal/logger/destination_stdout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ package logger
import (
"bytes"
"os"
"time"

"golang.org/x/term"
)

type destinationStdout struct {
useColor bool

buf bytes.Buffer
}

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()
writeTime(&d.buf, true)
writeLevel(&d.buf, level, true)
writeTime(&d.buf, t, d.useColor)
writeLevel(&d.buf, level, d.useColor)
writeContent(&d.buf, format, args)
os.Stdout.Write(d.buf.Bytes())
}
Expand Down
5 changes: 3 additions & 2 deletions internal/logger/destination_syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logger
import (
"bytes"
"io"
"time"
)

type destinationSysLog struct {
Expand All @@ -21,9 +22,9 @@ func newDestinationSyslog() (destination, error) {
}, 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()
writeTime(&d.buf, false)
writeTime(&d.buf, t, false)
writeLevel(&d.buf, level, false)
writeContent(&d.buf, format, args)
d.syslog.Write(d.buf.Bytes())
Expand Down
23 changes: 12 additions & 11 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,11 @@ func itoa(i int, wid int) []byte {
return b[bp:]
}

func writeTime(buf *bytes.Buffer, doColor bool) {
func writeTime(buf *bytes.Buffer, t time.Time, useColor bool) {
var intbuf bytes.Buffer

// date
now := time.Now()
year, month, day := now.Date()
year, month, day := t.Date()
intbuf.Write(itoa(year, 4))
intbuf.WriteByte('/')
intbuf.Write(itoa(int(month), 2))
Expand All @@ -88,46 +87,46 @@ func writeTime(buf *bytes.Buffer, doColor bool) {
intbuf.WriteByte(' ')

// time
hour, min, sec := now.Clock()
hour, min, sec := t.Clock()
intbuf.Write(itoa(hour, 2))
intbuf.WriteByte(':')
intbuf.Write(itoa(min, 2))
intbuf.WriteByte(':')
intbuf.Write(itoa(sec, 2))
intbuf.WriteByte(' ')

if doColor {
if useColor {
buf.WriteString(color.RenderString(color.Gray.Code(), intbuf.String()))
} else {
buf.WriteString(intbuf.String())
}
}

func writeLevel(buf *bytes.Buffer, level Level, doColor bool) {
func writeLevel(buf *bytes.Buffer, level Level, useColor bool) {
switch level {
case Debug:
if doColor {
if useColor {
buf.WriteString(color.RenderString(color.Debug.Code(), "DEB"))
} else {
buf.WriteString("DEB")
}

case Info:
if doColor {
if useColor {
buf.WriteString(color.RenderString(color.Green.Code(), "INF"))
} else {
buf.WriteString("INF")
}

case Warn:
if doColor {
if useColor {
buf.WriteString(color.RenderString(color.Warn.Code(), "WAR"))
} else {
buf.WriteString("WAR")
}

case Error:
if doColor {
if useColor {
buf.WriteString(color.RenderString(color.Error.Code(), "ERR"))
} else {
buf.WriteString("ERR")
Expand All @@ -150,7 +149,9 @@ func (lh *Logger) Log(level Level, format string, args ...interface{}) {
lh.mutex.Lock()
defer lh.mutex.Unlock()

t := time.Now()

for _, dest := range lh.destinations {
dest.log(level, format, args...)
dest.log(t, level, format, args...)
}
}

0 comments on commit 630741c

Please sign in to comment.