Skip to content

Commit

Permalink
Merge pull request #120 from ownmfa/bconway_update_alog_backend_slog
Browse files Browse the repository at this point in the history
Update hlog backend to use log/slog
  • Loading branch information
bconway authored Aug 22, 2023
2 parents 495429c + 7bdca87 commit 01c7fac
Show file tree
Hide file tree
Showing 82 changed files with 1,960 additions and 8,487 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ install:
-ldflags="-w" ./$${x}; done

lint:
# https://github.com/dominikh/go-tools/issues/1433
# go install honnef.co/go/tools/cmd/staticcheck@latest
go install honnef.co/go/tools/cmd/staticcheck@master
go install honnef.co/go/tools/cmd/staticcheck@latest
staticcheck -version
# staticcheck defaults are all,-ST1000,-ST1003,-ST1016,-ST1020,-ST1021,-ST1022
staticcheck -checks all ./...
Expand Down
4 changes: 2 additions & 2 deletions build/deploy/hermes/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: "3"
services:
hermes-api:
image: ghcr.io/ownmfa/hermes:aafe23c5
image: ghcr.io/ownmfa/hermes:c8374a87
command: hermes-api
restart: on-failure
depends_on:
Expand Down Expand Up @@ -35,7 +35,7 @@ services:
- "traefik.http.services.hermes-grpc.loadbalancer.server.scheme=h2c"

hermes-notifier:
image: ghcr.io/ownmfa/hermes:aafe23c5
image: ghcr.io/ownmfa/hermes:c8374a87
command: hermes-notifier
restart: on-failure
environment:
Expand Down
2 changes: 1 addition & 1 deletion cmd/hermes-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
cfg := config.New()

hlog.SetDefault(hlog.NewJSON().WithLevel(cfg.LogLevel).WithField("service",
hlog.SetDefault(hlog.NewJSON(cfg.LogLevel).WithField("service",
api.ServiceName))
metric.SetStatsD(cfg.StatsDAddr, api.ServiceName)

Expand Down
2 changes: 1 addition & 1 deletion cmd/hermes-notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func main() {
cfg := config.New()

hlog.SetDefault(hlog.NewJSON().WithLevel(cfg.LogLevel).WithField("service",
hlog.SetDefault(hlog.NewJSON(cfg.LogLevel).WithField("service",
notifier.ServiceName))
metric.SetStatsD(cfg.StatsDAddr, notifier.ServiceName)

Expand Down
15 changes: 6 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ go 1.21.0

require (
github.com/NYTimes/gziphandler v1.1.1
github.com/google/uuid v1.3.0
github.com/gregdel/pushover v1.2.0
github.com/google/uuid v1.3.1
github.com/gregdel/pushover v1.2.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2
github.com/jackc/pgx/v5 v5.4.3
github.com/jellydator/ttlcache/v3 v3.0.1
github.com/mennanov/fmutils v0.2.0
github.com/nsqio/go-nsq v1.1.0
github.com/ownmfa/api/go v1.0.27
github.com/redis/go-redis/v9 v9.0.5
github.com/rs/zerolog v1.30.0
github.com/redis/go-redis/v9 v9.1.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/smira/go-statsd v1.3.2
github.com/stretchr/testify v1.8.4
Expand All @@ -33,16 +32,14 @@ require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto v0.0.0-20230814215434-ca7cfce7776a // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230814215434-ca7cfce7776a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230814215434-ca7cfce7776a // indirect
google.golang.org/genproto v0.0.0-20230821184602-ccc8af3d0e93 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230821184602-ccc8af3d0e93 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230821184602-ccc8af3d0e93 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
45 changes: 14 additions & 31 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
github.com/bsm/ginkgo/v2 v2.9.5 h1:rtVBYPs3+TC5iLUVOis1B9tjLTup7Cj5IfzosKtvTJ0=
github.com/bsm/ginkgo/v2 v2.9.5/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y=
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand All @@ -21,7 +20,6 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBFApVqftFV6k087DA=
github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
Expand All @@ -47,10 +45,10 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gregdel/pushover v1.2.0 h1:SLnpvJijUyEZvkJNyrldGhFhryYgQYlThSLpB5Oqt5k=
github.com/gregdel/pushover v1.2.0/go.mod h1:EcaO66Nn1StkpEm1iKtBTV3d2A16SoMsVER1PthX7to=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gregdel/pushover v1.2.1 h1:IPPJCdzXz60gMqnlzS0ZAW5z5aS1gI4nU+YM0Pe+ssA=
github.com/gregdel/pushover v1.2.1/go.mod h1:EcaO66Nn1StkpEm1iKtBTV3d2A16SoMsVER1PthX7to=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2 h1:dygLcbEBA+t/P7ck6a8AkXv6juQ4cK0RHBoh32jxhHM=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2/go.mod h1:Ap9RLCIJVtgQg1/BBgVEfypOAySvvlcpcVQkSzJCH4Y=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
Expand All @@ -65,30 +63,19 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mennanov/fmutils v0.2.0 h1:Hw/iuQPdKtiB2B9YYh+NX8iv7U7eQu1rICPjr8NvxSo=
github.com/mennanov/fmutils v0.2.0/go.mod h1:DE+qeI9Xy5s1GA4trgq8H26jr5DgJ4a9+0D1DPVCqyk=
github.com/nsqio/go-nsq v1.1.0 h1:PQg+xxiUjA7V+TLdXw7nVrJ5Jbl3sN86EhGCQj4+FYE=
github.com/nsqio/go-nsq v1.1.0/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY=
github.com/ownmfa/api/go v1.0.27 h1:fOruRrzqnAJezhSSxpZjB8wd0djA+ii98x+eI6fI6GE=
github.com/ownmfa/api/go v1.0.27/go.mod h1:99PmUKxcZdiNGTHyJRlDkennbI7OWYiuRny6u9zbpkI=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o=
github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/redis/go-redis/v9 v9.1.0 h1:137FnGdk+EQdCbye1FW+qOEcY5S+SpY9T0NiuqvtfMY=
github.com/redis/go-redis/v9 v9.1.0/go.mod h1:urWj3He21Dj5k4TK1y59xH8Uj6ATueP8AH1cY3lZl4c=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
github.com/smira/go-statsd v1.3.2 h1:1EeuzxNZ/TD9apbTOFSM9nulqfcsQFmT4u1A2DREabI=
Expand Down Expand Up @@ -125,10 +112,6 @@ golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand All @@ -146,12 +129,12 @@ google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
google.golang.org/genproto v0.0.0-20230814215434-ca7cfce7776a h1:+pp9kgkk8qyqFeQyXoDBFI4U+YYB15cmRkQ3pi6v/h4=
google.golang.org/genproto v0.0.0-20230814215434-ca7cfce7776a/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
google.golang.org/genproto/googleapis/api v0.0.0-20230814215434-ca7cfce7776a h1:ICnet6R+XBkBfhCeApORkkEZbXfPUIHmqOC+5MdMtaQ=
google.golang.org/genproto/googleapis/api v0.0.0-20230814215434-ca7cfce7776a/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230814215434-ca7cfce7776a h1:5rTPHLf5eLPfqGvw3fLpEmUpko2Ky91ft14LxGs5BZc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230814215434-ca7cfce7776a/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/genproto v0.0.0-20230821184602-ccc8af3d0e93 h1:zv6ieVm8jNcN33At1+APsRISkRgynuWUxUhv6G123jY=
google.golang.org/genproto v0.0.0-20230821184602-ccc8af3d0e93/go.mod h1:yZTlhN0tQnXo3h00fuXNCxJdLdIdnVFVBaRJ5LWBbw4=
google.golang.org/genproto/googleapis/api v0.0.0-20230821184602-ccc8af3d0e93 h1:v4jF/cZj3rPjpsS7a+TEb8RofwMmPnfn+QiPkDyduDA=
google.golang.org/genproto/googleapis/api v0.0.0-20230821184602-ccc8af3d0e93/go.mod h1:KjSP20unUpOx5kyQUFa7k4OJg0qeJ7DEZflGDu2p6Bk=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230821184602-ccc8af3d0e93 h1:OpqhBqmDJdwQbcZeN2G+wAjNq6xnmZHrmS+//OOUxT8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230821184602-ccc8af3d0e93/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
Expand Down
51 changes: 24 additions & 27 deletions pkg/hlog/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,36 @@ package hlog

import (
"fmt"
"sync"
"sync/atomic"
)

// Since logger is global and may be replaced, locking is required.
var (
logger Logger
loggerMu sync.Mutex
)
// logger is an atomic value of type Logger.
var logger atomic.Value

// NewConsole returns a new Logger with console formatting at the debug level.
func NewConsole() Logger {
return newZlogConsole()
// NewConsole returns a new Logger with console formatting at the specified
// level.
func NewConsole(level string) Logger {
return newStlogConsole(level)
}

// NewJSON returns a new Logger with JSON formatting at the debug level.
func NewJSON() Logger {
return newZlogJSON()
// NewJSON returns a new Logger with JSON formatting at the specified level.
func NewJSON(level string) Logger {
return newStlogJSON(level)
}

// Default returns the default logger, which is thread-safe.
func Default() Logger {
loggerMu.Lock()
defer loggerMu.Unlock()
l, ok := logger.Load().(Logger)
if !ok {
panic("Default logger.Load: false")
}

return logger
return l
}

// SetDefault sets a new default logger.
func SetDefault(l Logger) {
loggerMu.Lock()
defer loggerMu.Unlock()

logger = l
logger.Store(l)
}

// WithField returns a derived Logger from the default Logger with a string
Expand All @@ -44,42 +41,42 @@ func WithField(key, val string) Logger {
}

// Debug logs a new message with debug level.
func Debug(v ...interface{}) {
func Debug(v ...any) {
Default().Debug(fmt.Sprint(v...))
}

// Debugf logs a new formatted message with debug level.
func Debugf(format string, v ...interface{}) {
func Debugf(format string, v ...any) {
Default().Debugf(format, v...)
}

// Info logs a new message with info level.
func Info(v ...interface{}) {
func Info(v ...any) {
Default().Info(fmt.Sprint(v...))
}

// Infof logs a new formatted message with info level.
func Infof(format string, v ...interface{}) {
func Infof(format string, v ...any) {
Default().Infof(format, v...)
}

// Error logs a new message with error level.
func Error(v ...interface{}) {
func Error(v ...any) {
Default().Error(fmt.Sprint(v...))
}

// Errorf logs a new formatted message with error level.
func Errorf(format string, v ...interface{}) {
func Errorf(format string, v ...any) {
Default().Errorf(format, v...)
}

// Fatal logs a new message with fatal level followed by a call to os.Exit(1).
func Fatal(v ...interface{}) {
func Fatal(v ...any) {
Default().Fatal(fmt.Sprint(v...))
}

// Fatalf logs a new formatted message with fatal level followed by a call to
// os.Exit(1).
func Fatalf(format string, v ...interface{}) {
func Fatalf(format string, v ...any) {
Default().Fatalf(format, v...)
}
23 changes: 12 additions & 11 deletions pkg/hlog/default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package hlog

import (
"fmt"
"strconv"
"testing"

"github.com/ownmfa/hermes/pkg/test/random"
Expand Down Expand Up @@ -31,7 +32,7 @@ func TestDefault(t *testing.T) {
}

func TestDefaultConsole(t *testing.T) {
SetDefault(NewConsole())
SetDefault(NewConsole("DEBUG"))

for i := 0; i < 5; i++ {
lTest := i
Expand All @@ -51,7 +52,7 @@ func TestDefaultConsole(t *testing.T) {
}

func TestDefaultJSON(t *testing.T) {
SetDefault(NewJSON())
SetDefault(NewJSON("DEBUG"))

for i := 0; i < 5; i++ {
lTest := i
Expand All @@ -73,21 +74,21 @@ func TestDefaultJSON(t *testing.T) {
func TestDefaultWithField(t *testing.T) {
t.Parallel()

logger := WithField(random.String(10), random.String(10))
t.Logf("logger: %#v", logger)

for i := 0; i < 5; i++ {
lTest := i

t.Run(fmt.Sprintf("Can log %v with string", lTest), func(t *testing.T) {
t.Parallel()

logger.Debug("Debug")
logger.Debugf("Debugf: %v", lTest)
logger.Info("Info")
logger.Infof("Infof: %v", lTest)
logger.Error("Error")
logger.Errorf("Errorf: %v", lTest)
logField := WithField(strconv.Itoa(lTest), random.String(10))
t.Logf("logField: %#v", logField)

logField.Debug("Debug")
logField.Debugf("Debugf: %v", lTest)
logField.Info("Info")
logField.Infof("Infof: %v", lTest)
logField.Error("Error")
logField.Errorf("Errorf: %v", lTest)
// Do not test Fatal* due to os.Exit.
})
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/hlog/init.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package hlog

import (
"github.com/rs/zerolog"
)

func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs

SetDefault(NewConsole())
SetDefault(NewConsole("DEBUG"))
}
Loading

0 comments on commit 01c7fac

Please sign in to comment.