Skip to content

Commit ac60a09

Browse files
iwpndgdey
authored andcommitted
feat: structured logs with uber/zap
As discussed in #831 I added the option to use structured logs with uber/zap while falling back to the currently implemented default if --logger is not specifically used. Log levels conform to the current implementation with the exception of log level TRACE that is not supported by zap. I use Debug as a fall back here.
1 parent fe0f0e5 commit ac60a09

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+10610
-17
lines changed

cmd/tegola/cmd/root.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,25 @@ import (
1616
var (
1717
logLevel string
1818
configFile string
19+
logger string
1920
// parsed config
2021
conf config.Config
2122

2223
// require cache
2324
RequireCache bool
2425
)
2526

27+
func validateSupportedLoggers(logger string) error {
28+
switch logger {
29+
case log.STANDARD:
30+
return nil
31+
case log.ZAP:
32+
return nil
33+
default:
34+
return fmt.Errorf("invalid logger %s", logger)
35+
}
36+
}
37+
2638
func getLogLevelFromString(level string) (log.Level, error) {
2739
switch level {
2840
case "TRACE":
@@ -46,6 +58,8 @@ func init() {
4658
"path or http url to a config file, or \"-\" for stdin")
4759
RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "INFO",
4860
"set log level to: TRACE, DEBUG, INFO, WARN or ERROR")
61+
RootCmd.PersistentFlags().StringVar(&logger, "logger", log.STANDARD,
62+
"set logger to: standard, zap - default: standard")
4963

5064
// server
5165
serverCmd.Flags().StringVarP(&serverPort, "port", "p", ":8080", "port to bind tile server to")
@@ -57,7 +71,6 @@ func init() {
5771
RootCmd.AddCommand(cachecmd.Cmd)
5872
// version
5973
RootCmd.AddCommand(versionCmd)
60-
6174
}
6275

6376
var RootCmd = &cobra.Command{
@@ -76,11 +89,17 @@ func rootCmdValidatePersistent(cmd *cobra.Command, args []string) (err error) {
7689
build.Commands = append(build.Commands, cmdName)
7790
return nil
7891
default:
79-
return initConfig(configFile, requireCache, logLevel)
92+
return initConfig(configFile, requireCache, logLevel, logger)
8093
}
8194
}
8295

83-
func initConfig(configFile string, cacheRequired bool, logLevel string) (err error) {
96+
func initConfig(configFile string, cacheRequired bool, logLevel string, logger string) (err error) {
97+
err = validateSupportedLoggers(logger)
98+
if err != nil {
99+
return err
100+
}
101+
log.SetLogger(logger)
102+
84103
// set log level before the first log is called
85104
level, err := getLogLevelFromString(logLevel)
86105
if err != nil {

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/pborman/uuid v1.2.0
2424
github.com/prometheus/client_golang v1.9.0
2525
github.com/theckman/goconstraint v1.10.1-0.20180216224824-e867bde6e4e1
26+
go.uber.org/zap v1.21.0
2627
gopkg.in/go-playground/colors.v1 v1.0.2-0.20150924111726-b53ecfb39623
2728
)
2829

@@ -47,9 +48,11 @@ require (
4748
github.com/prometheus/common v0.15.0 // indirect
4849
github.com/prometheus/procfs v0.2.0 // indirect
4950
github.com/spf13/pflag v1.0.1 // indirect
51+
go.uber.org/atomic v1.7.0 // indirect
52+
go.uber.org/multierr v1.6.0 // indirect
5053
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
5154
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
5255
golang.org/x/text v0.3.6 // indirect
53-
golang.org/x/tools v0.0.0-20200507205054-480da3ebd79c // indirect
56+
golang.org/x/tools v0.1.5 // indirect
5457
google.golang.org/protobuf v1.23.0 // indirect
5558
)

go.sum

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQ
3636
github.com/aws/aws-sdk-go v1.27.0 h1:0xphMHGMLBrPMfxR2AmVjZKcMEESEgWF8Kru94BNByk=
3737
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
3838
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
39+
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
3940
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
4041
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
4142
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
@@ -385,6 +386,7 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
385386
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
386387
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
387388
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
389+
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
388390
github.com/zenazn/goji v0.9.0/go.mod h1:7S9M489iMyHBNxwZnk9/EHS098H4/F6TATF2mIxtB1Q=
389391
go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
390392
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
@@ -395,13 +397,20 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
395397
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
396398
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
397399
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
400+
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
401+
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
402+
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
398403
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
399404
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
400405
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
406+
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
407+
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
401408
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
402409
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
403410
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
404411
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
412+
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
413+
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
405414
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
406415
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
407416
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@@ -424,6 +433,7 @@ golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHl
424433
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
425434
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
426435
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
436+
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
427437
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
428438
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
429439
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -444,6 +454,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
444454
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
445455
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
446456
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
457+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
458+
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
447459
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
448460
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
449461
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -452,6 +464,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ
452464
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
453465
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
454466
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
467+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
455468
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
456469
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
457470
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
@@ -479,6 +492,8 @@ golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7w
479492
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
480493
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
481494
golang.org/x/sys v0.0.0-20201214210602-f9fddec55a1e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
495+
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
496+
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
482497
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
483498
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
484499
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
@@ -510,6 +525,8 @@ golang.org/x/tools v0.0.0-20200103221440-774c71fcf114/go.mod h1:TB2adYChydJhpapK
510525
golang.org/x/tools v0.0.0-20200113040837-eac381796e91/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
511526
golang.org/x/tools v0.0.0-20200507205054-480da3ebd79c h1:TDspWmUQsjdWzrHnd5imfaJSfhR4AO/R7kG++T2cONw=
512527
golang.org/x/tools v0.0.0-20200507205054-480da3ebd79c/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
528+
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
529+
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
513530
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
514531
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
515532
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -564,10 +581,12 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
564581
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
565582
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
566583
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
584+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
567585
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
568586
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
569587
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
570588
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
589+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
571590
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
572591
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
573592
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

internal/log/log.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package log
22

33
import (
44
"fmt"
5-
"io"
65
"sync"
76
)
87

@@ -16,14 +15,25 @@ type Interface interface {
1615
Info(...interface{})
1716
Debug(...interface{})
1817
Trace(...interface{})
19-
SetOutput(io.Writer)
2018
}
2119

2220
func init() {
23-
logger = standard
21+
SetLogger(STANDARD)
2422
SetLogLevel(INFO)
2523
}
2624

25+
func SetLogger(n string) {
26+
switch n {
27+
case ZAP:
28+
buildZapLogger()
29+
logger = zapLogger
30+
case STANDARD:
31+
logger = standard
32+
default:
33+
logger = standard
34+
}
35+
}
36+
2737
type Level int
2838

2939
// Allows the ordering of severity to be checked
@@ -36,6 +46,12 @@ const (
3646
FATAL
3747
)
3848

49+
// Supported Loggers
50+
const (
51+
STANDARD string = "standard"
52+
ZAP string = "zap"
53+
)
54+
3955
var (
4056
logger Interface
4157
level Level
@@ -67,10 +83,6 @@ func (lvl Level) String() string {
6783
}
6884
}
6985

70-
func SetOutput(w io.Writer) {
71-
logger.SetOutput(w)
72-
}
73-
7486
func GetLogLevel() Level {
7587
return level
7688
}

internal/log/log_test.go

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"regexp"
88
"strings"
99
"testing"
10+
11+
"go.uber.org/zap"
12+
"go.uber.org/zap/zapcore"
13+
"go.uber.org/zap/zaptest/observer"
1014
)
1115

1216
const ExpectedFilename = "log_test.go"
@@ -117,7 +121,8 @@ func testLoggingF(tc testLoggingFTCase) (string, func(*testing.T)) {
117121
name := fmt.Sprintf("%s %s %s", tc.loggerLevel, tc.msgLevel, msg)
118122
return name, func(t *testing.T) {
119123
testOut := bytes.NewBufferString("")
120-
SetOutput(testOut)
124+
standard.SetOutput(testOut)
125+
121126
SetLogLevel(tc.loggerLevel)
122127

123128
loggerCalls[tc.msgLevel](tc.msg, tc.msgArgs...)
@@ -216,7 +221,8 @@ func testLogging(tc testLoggingFTCase) (string, func(*testing.T)) {
216221
name := fmt.Sprintf("%s %s %s", tc.loggerLevel, tc.msgLevel, msg)
217222
return name, func(t *testing.T) {
218223
testOut := bytes.NewBufferString("")
219-
SetOutput(testOut)
224+
standard.SetOutput(testOut)
225+
220226
SetLogLevel(tc.loggerLevel)
221227

222228
loggerCalls[tc.msgLevel](tc.msgArgs...)
@@ -273,3 +279,88 @@ func TestLogging(t *testing.T) {
273279
}
274280

275281
}
282+
283+
func TestLogJSON(t *testing.T) {
284+
type tcase struct {
285+
level Level
286+
format string
287+
args string
288+
fops func(string, ...interface{})
289+
expMsg string
290+
expLvl zapcore.Level
291+
}
292+
293+
fn := func(tc tcase) func(*testing.T) {
294+
return func(t *testing.T) {
295+
observedZapCore, observedLogs := observer.New(zap.DebugLevel)
296+
zS := zap.New(observedZapCore).Sugar()
297+
zapLogger = &ZapLogger{zS}
298+
logger = zapLogger
299+
300+
SetLogLevel(tc.level)
301+
302+
tc.fops(tc.format, tc.args)
303+
304+
if observedLogs.Len() < 1 {
305+
t.Fatal("Should've observed logs")
306+
}
307+
308+
allLogs := observedLogs.All()
309+
310+
if allLogs[0].Message != tc.expMsg {
311+
t.Fatalf("expected message: %v, got: %v", tc.expMsg, allLogs[0].Message)
312+
}
313+
314+
if allLogs[0].Level != tc.expLvl {
315+
t.Fatalf("expected level: %v, got: %v", tc.expLvl, allLogs[0].Level)
316+
}
317+
}
318+
}
319+
320+
tests := map[string]tcase{
321+
"Infof": {
322+
level: INFO,
323+
format: "Infof json %v",
324+
args: "logger",
325+
fops: func(format string, args ...interface{}) {
326+
Infof(format, args...)
327+
},
328+
expMsg: "Infof json logger",
329+
expLvl: zapcore.InfoLevel,
330+
},
331+
"Debugf": {
332+
level: DEBUG,
333+
format: "Debugf json %v",
334+
args: "logger",
335+
fops: func(format string, args ...interface{}) {
336+
Debugf(format, args...)
337+
},
338+
expMsg: "Debugf json logger",
339+
expLvl: zapcore.DebugLevel,
340+
},
341+
"Warnf": {
342+
level: WARN,
343+
format: "Warnf json %v",
344+
args: "logger",
345+
fops: func(format string, args ...interface{}) {
346+
Warnf(format, args...)
347+
},
348+
expMsg: "Warnf json logger",
349+
expLvl: zapcore.WarnLevel,
350+
},
351+
"Errorf": {
352+
level: ERROR,
353+
format: "Errorf json %v",
354+
args: "logger",
355+
fops: func(format string, args ...interface{}) {
356+
Errorf(format, args...)
357+
},
358+
expMsg: "Errorf json logger",
359+
expLvl: zapcore.ErrorLevel,
360+
},
361+
}
362+
363+
for name, tc := range tests {
364+
t.Run(name, fn(tc))
365+
}
366+
}

0 commit comments

Comments
 (0)