Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
Use JSON format for logs if not interactive terminal
Matthew Holt committed
Mar 16, 2020 at 14:22 UTC
ae86f6dd91b471067082a48d14d94b868e07474c
1 file changed
+18
-11
logging.go
+18
-11
@@ -27,6 +27,7 @@ import (
27
28
"go.uber.org/zap"
29
"go.uber.org/zap/zapcore"
30
+ "golang.org/x/crypto/ssh/terminal"
31
)
32
33
func init() {
@@ -35,12 +36,14 @@ func init() {
36
RegisterModule(DiscardWriter{})
37
}
38
38
-// Logging facilitates logging within Caddy.
39
+// Logging facilitates logging within Caddy. The default log is
40
+// called "default" and you can customize it. You can also define
41
+// additional logs.
42
//
43
// By default, all logs at INFO level and higher are written to
44
// standard error ("stderr" writer) in a human-readable format
42
-// ("console" encoder). The default log is called "default" and
43
-// you can customize it. You can also define additional logs.
45
+// ("console" encoder if stdout is an interactive terminal, "json"
46
+// encoder otherwise).
47
//
48
// All defined logs accept all log entries by default, but you
49
// can filter by level and module/logger names. A logger's name
@@ -50,10 +53,10 @@ func init() {
53
// "http.handlers", because all HTTP handler module names have
54
// that prefix.
55
//
53
-// Caddy logs (except the sink) are mostly zero-allocation, so
54
-// they are very high-performing in terms of memory and CPU time.
55
-// Enabling sampling can further increase throughput on extremely
56
-// high-load servers.
56
+// Caddy logs (except the sink) are zero-allocation, so they are
57
+// very high-performing in terms of memory and CPU time. Enabling
58
+// sampling can further increase throughput on extremely high-load
59
+// servers.
60
type Logging struct {
61
// Sink is the destination for all unstructured logs emitted
62
// from Go's standard library logger. These logs are common
@@ -660,11 +663,15 @@ func newDefaultProductionLog() (*defaultCustomLog, error) {
663
664
func newDefaultProductionLogEncoder() zapcore.Encoder {
665
encCfg := zap.NewProductionEncoderConfig()
663
- encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
664
- encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
665
- encoder.AppendString(ts.UTC().Format("2006/01/02 15:04:05.000"))
666
+ if terminal.IsTerminal(int(os.Stdout.Fd())) {
667
+ // if interactive terminal, make output more human-readable by default
668
+ encCfg.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
669
+ encoder.AppendString(ts.UTC().Format("2006/01/02 15:04:05.000"))
670
+ }
671
+ encCfg.EncodeLevel = zapcore.CapitalColorLevelEncoder
672
+ return zapcore.NewConsoleEncoder(encCfg)
673
}
667
- return zapcore.NewConsoleEncoder(encCfg)
674
+ return zapcore.NewJSONEncoder(encCfg)
675
}
676
677
// Log returns the current default logger.