No colors for non-interactive shells
This commit is contained in:
parent
e5fe697fa7
commit
51290b4f92
|
@ -49,7 +49,7 @@ func (l *Logger) Format(level logLevel, tmpl string, args ...interface{}) string
|
|||
}
|
||||
|
||||
// Find the theme color to use.
|
||||
if l.Config.Colors != NoColor {
|
||||
if interactive && l.Config.Colors != NoColor {
|
||||
var (
|
||||
primary ThemeColor
|
||||
secondary ThemeColor
|
||||
|
|
9
golog.go
9
golog.go
|
@ -1,8 +1,11 @@
|
|||
package golog
|
||||
|
||||
import (
|
||||
"os"
|
||||
"sync"
|
||||
"text/template"
|
||||
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
// An internal map of named loggers. This allows for GetLogger() to be called
|
||||
|
@ -11,10 +14,16 @@ import (
|
|||
var (
|
||||
loggers map[string]*Logger
|
||||
loggerMutex sync.Mutex
|
||||
|
||||
interactive bool // is an interactive shell
|
||||
)
|
||||
|
||||
func init() {
|
||||
loggers = map[string]*Logger{}
|
||||
|
||||
// Detect if we're running in an interactive shell, so we can globally
|
||||
// disable colors when redirecting to a log file.
|
||||
interactive = terminal.IsTerminal(int(os.Stdout.Fd()))
|
||||
}
|
||||
|
||||
// Logger stores the configuration for a named logger instance.
|
||||
|
|
|
@ -3,6 +3,8 @@ package golog
|
|||
import "testing"
|
||||
|
||||
func TestColors(t *testing.T) {
|
||||
interactive = true // fake an interactive TTY to test color outputs
|
||||
|
||||
log := GetLogger("test")
|
||||
|
||||
// Helper function to emit all the log types.
|
||||
|
|
Loading…
Reference in New Issue
Block a user