Skip to content

Commit 081f84c

Browse files
authored
Merge pull request hoisie#194 from hoisie/color-output
Add the 'ColorOutput' server config option
2 parents 062368c + 128d5dd commit 081f84c

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

server.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type ServerConfig struct {
2727
CookieSecret string
2828
RecoverPanic bool
2929
Profiler bool
30+
ColorOutput bool
3031
}
3132

3233
// Server represents a web.go server.
@@ -284,10 +285,18 @@ func (s *Server) logRequest(ctx Context, sTime time.Time) {
284285
client = req.RemoteAddr
285286
}
286287

287-
fmt.Fprintf(&logEntry, "%s - \033[32;1m %s %s\033[0m - %v", client, req.Method, requestPath, duration)
288+
if s.Config.ColorOutput {
289+
fmt.Fprintf(&logEntry, "%s - \x1b[32;1m%s %s\x1b[0m - %v", client, req.Method, requestPath, duration)
290+
} else {
291+
fmt.Fprintf(&logEntry, "%s - %s %s - %v", client, req.Method, requestPath, duration)
292+
}
288293

289294
if len(ctx.Params) > 0 {
290-
fmt.Fprintf(&logEntry, " - \033[37;1mParams: %v\033[0m\n", ctx.Params)
295+
if s.Config.ColorOutput {
296+
fmt.Fprintf(&logEntry, " - \x1b[37;1mParams: %v\x1b[0m\n", ctx.Params)
297+
} else {
298+
fmt.Fprintf(&logEntry, " - Params: %v\n", ctx.Params)
299+
}
291300
}
292301

293302
ctx.Server.Logger.Print(logEntry.String())

web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ func SetLogger(logger *log.Logger) {
265265
// Config is the configuration of the main server.
266266
var Config = &ServerConfig{
267267
RecoverPanic: true,
268+
ColorOutput: true,
268269
}
269270

270271
var mainServer = NewServer()

web_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,45 @@ func TestDuplicateHeader(t *testing.T) {
540540
}
541541
}
542542

543+
// test that output contains ASCII color codes by default
544+
func TestColorOutputDefault(t *testing.T) {
545+
s := NewServer()
546+
var logOutput bytes.Buffer
547+
logger := log.New(&logOutput, "", 0)
548+
s.Logger = logger
549+
s.Get("/test", func() string {
550+
return "test"
551+
})
552+
req := buildTestRequest("GET", "/test", "", nil, nil)
553+
var buf bytes.Buffer
554+
iob := ioBuffer{input: nil, output: &buf}
555+
c := scgiConn{wroteHeaders: false, req: req, headers: make(map[string][]string), fd: &iob}
556+
s.Process(&c, req)
557+
if !strings.Contains(logOutput.String(), "\x1b") {
558+
t.Fatalf("The default log output does not seem to be colored")
559+
}
560+
}
561+
562+
// test that output contains ASCII color codes by default
563+
func TestNoColorOutput(t *testing.T) {
564+
s := NewServer()
565+
s.Config.ColorOutput = false
566+
var logOutput bytes.Buffer
567+
logger := log.New(&logOutput, "", 0)
568+
s.Logger = logger
569+
s.Get("/test", func() string {
570+
return "test"
571+
})
572+
req := buildTestRequest("GET", "/test", "", nil, nil)
573+
var buf bytes.Buffer
574+
iob := ioBuffer{input: nil, output: &buf}
575+
c := scgiConn{wroteHeaders: false, req: req, headers: make(map[string][]string), fd: &iob}
576+
s.Process(&c, req)
577+
if strings.Contains(logOutput.String(), "\x1b") {
578+
t.Fatalf("The log contains color escape codes")
579+
}
580+
}
581+
543582
func BuildBasicAuthCredentials(user string, pass string) string {
544583
s := user + ":" + pass
545584
return "Basic " + base64.StdEncoding.EncodeToString([]byte(s))

0 commit comments

Comments
 (0)