refactor: use LoggerFactory instead of patching pion/logging

This commit is contained in:
Siyuan Miao 2025-04-10 21:50:54 +02:00
parent 621484a33e
commit d52052b289
13 changed files with 145 additions and 744 deletions

View File

@ -290,7 +290,7 @@ func runWebsocketClient() error {
dialCtx, cancelDial := context.WithTimeout(context.Background(), CloudWebSocketConnectTimeout)
scopedLogger := websocketLogger.With().
Str("source", wsURL.String()).
Str("source", wsURL.Host).
Str("sourceType", "cloud").
Logger()

38
go.mod
View File

@ -2,15 +2,14 @@ module github.com/jetkvm/kvm
go 1.23.0
toolchain go1.24.1
require (
github.com/Masterminds/semver/v3 v3.3.0
github.com/beevik/ntp v1.3.1
github.com/coder/websocket v1.8.13
github.com/coreos/go-oidc/v3 v3.11.0
github.com/creack/pty v1.1.23
github.com/gin-gonic/gin v1.9.1
github.com/gin-contrib/logger v1.2.5
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/gwatts/rootcerts v0.0.0-20240401182218-3ab9db955caf
github.com/hanwen/go-fuse/v2 v2.5.1
@ -22,41 +21,39 @@ require (
github.com/prometheus/client_golang v1.21.0
github.com/prometheus/common v0.62.0
github.com/psanford/httpreadat v0.1.0
github.com/rs/zerolog v1.34.0
github.com/vishvananda/netlink v1.3.0
go.bug.st/serial v1.6.2
golang.org/x/crypto v0.31.0
golang.org/x/net v0.33.0
golang.org/x/crypto v0.36.0
golang.org/x/net v0.38.0
)
replace github.com/pojntfx/go-nbd v0.3.2 => github.com/chemhack/go-nbd v0.0.0-20241006125820-59e45f5b1e7b
replace github.com/pion/logging v0.2.2 => ./internal/pion/logging
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/bytedance/sonic v1.13.2 // indirect
github.com/bytedance/sonic/loader v0.2.4 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/cloudwego/base64x v0.1.5 // indirect
github.com/creack/goselect v0.1.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/gin-contrib/sse v1.0.0 // indirect
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/go-playground/validator/v10 v10.26.0 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/pilebones/go-udev v0.9.0 // indirect
github.com/pion/datachannel v1.5.9 // indirect
github.com/pion/dtls/v3 v3.0.3 // indirect
@ -73,15 +70,14 @@ require (
github.com/pion/turn/v4 v4.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rs/zerolog v1.34.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
github.com/vishvananda/netns v0.0.4 // indirect
github.com/wlynxg/anet v0.0.5 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/arch v0.15.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.21.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
golang.org/x/text v0.23.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

69
go.sum
View File

@ -4,20 +4,18 @@ github.com/beevik/ntp v1.3.1 h1:Y/srlT8L1yQr58kyPWFPZIxRL8ttx2SRIpVYJqZIlAM=
github.com/beevik/ntp v1.3.1/go.mod h1:fT6PylBq86Tsq23ZMEe47b7QQrZfYBFPnpzt0a9kJxw=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ=
github.com/bytedance/sonic v1.13.2/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chemhack/go-nbd v0.0.0-20241006125820-59e45f5b1e7b h1:dSbDgy72Y1sjLPWLv7vs0fMFuhMBMViiT9PJZiZWZNs=
github.com/chemhack/go-nbd v0.0.0-20241006125820-59e45f5b1e7b/go.mod h1:SehHnbi2e8NiSAKby42Itm8SIoS7b+wAprsfPH3qgYk=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE=
github.com/coder/websocket v1.8.13/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
github.com/coreos/go-oidc/v3 v3.11.0 h1:Ia3MxdwpSw702YW0xgfmP1GVCMA9aEFWu12XUZ3/OtI=
@ -30,12 +28,14 @@ github.com/creack/pty v1.1.23/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfv
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=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
github.com/gin-contrib/logger v1.2.5 h1:qVQI4omayQecuN4zX9ZZnsOq7w9J/ZLds3J/FMn8ypM=
github.com/gin-contrib/logger v1.2.5/go.mod h1:/bj+vNMuA2xOEQ1aRHoJ1m9+uyaaXIAxQTvM2llsc6I=
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
github.com/go-jose/go-jose/v4 v4.0.2 h1:R3l3kkBds16bO7ZFAEEcofK0MkrAJt3jlJznWZG0nvk=
github.com/go-jose/go-jose/v4 v4.0.2/go.mod h1:WVf9LFMHh/QVrmqrOfqun0C45tMe3RoiKJMPvgWwLfY=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
@ -44,10 +44,10 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k=
github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@ -65,8 +65,8 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/klauspost/compress v1.17.11 h1:In6xLpyWOi1+C7tXUUWv2ot1QvBjxevKAaI6IXrJmUc=
github.com/klauspost/compress v1.17.11/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@ -96,8 +96,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pilebones/go-udev v0.9.0 h1:N1uEO/SxUwtIctc0WLU0t69JeBxIYEYnj8lT/Nabl9Q=
github.com/pilebones/go-udev v0.9.0/go.mod h1:T2eI2tUSK0hA2WS5QLjXJUfQkluZQu+18Cqvem3CaXI=
github.com/pion/datachannel v1.5.9 h1:LpIWAOYPyDrXtU+BW7X0Yt/vGtYxtXQ8ql7dFfYUVZA=
@ -175,31 +175,27 @@ github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU=
github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
go.bug.st/serial v1.6.2 h1:kn9LRX3sdm+WxWKufMlIRndwGfPWsH1/9lCWXQCasq8=
go.bug.st/serial v1.6.2/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw=
golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.32.0 h1:s77OFDvIQeibCmezSnk/q6iAfkdiQaJi4VzroCFrN20=
golang.org/x/sys v0.32.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
google.golang.org/protobuf v1.36.1 h1:yBPeRvTftaleIgM3PZ/WBIZ7XM/eEYAaEyCwvyjq/gk=
google.golang.org/protobuf v1.36.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
@ -207,4 +203,3 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View File

@ -1,15 +0,0 @@
module github.com/pion/logging
go 1.20
require github.com/stretchr/testify v1.10.0
require (
github.com/davecgh/go-spew v1.1.1 // 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/rs/zerolog v1.34.0 // indirect
golang.org/x/sys v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@ -1,25 +0,0 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
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.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/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/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
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.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,272 +0,0 @@
// This is an internal package to patch the Pion logging library to use the
// zerolog instead of the log package.
package logging
import (
"fmt"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
"github.com/rs/zerolog"
)
var defaultOutput io.Writer = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
PartsOrder: []string{"time", "level", "scope", "component", "message"},
FieldsExclude: []string{"scope", "component"},
FormatPartValueByName: func(value interface{}, name string) string {
val := fmt.Sprintf("%s", value)
if name == "component" {
if value == nil {
return "-"
}
}
return val
},
}
func SetDefaultOutput(output io.Writer) {
defaultOutput = output
}
// Use this abstraction to ensure thread-safe access to the logger's io.Writer.
// (which could change at runtime).
type loggerWriter struct {
sync.RWMutex
output io.Writer
}
func (lw *loggerWriter) SetOutput(output io.Writer) {
lw.Lock()
defer lw.Unlock()
lw.output = output
}
func (lw *loggerWriter) Write(data []byte) (int, error) {
lw.RLock()
defer lw.RUnlock()
return lw.output.Write(data)
}
type zerologEventLogger func() *zerolog.Event
// DefaultLeveledLogger encapsulates functionality for providing logging at.
// user-defined levels.
type DefaultLeveledLogger struct {
level LogLevel
writer *zerolog.Logger
trace zerologEventLogger
debug zerologEventLogger
info zerologEventLogger
warn zerologEventLogger
err zerologEventLogger
}
func (ll *DefaultLeveledLogger) GetLogger() *zerolog.Logger {
return ll.writer
}
// WithTraceLogger is a chainable configuration function which sets the
// Trace-level logger.
func (ll *DefaultLeveledLogger) WithTraceLogger(log zerologEventLogger) *DefaultLeveledLogger {
ll.trace = log
return ll
}
// WithDebugLogger is a chainable configuration function which sets the
// Debug-level logger.
func (ll *DefaultLeveledLogger) WithDebugLogger(log zerologEventLogger) *DefaultLeveledLogger {
ll.debug = log
return ll
}
// WithInfoLogger is a chainable configuration function which sets the
// Info-level logger.
func (ll *DefaultLeveledLogger) WithInfoLogger(log zerologEventLogger) *DefaultLeveledLogger {
ll.info = log
return ll
}
// WithWarnLogger is a chainable configuration function which sets the
// Warn-level logger.
func (ll *DefaultLeveledLogger) WithWarnLogger(log zerologEventLogger) *DefaultLeveledLogger {
ll.warn = log
return ll
}
// WithErrorLogger is a chainable configuration function which sets the
// Error-level logger.
func (ll *DefaultLeveledLogger) WithErrorLogger(log zerologEventLogger) *DefaultLeveledLogger {
ll.err = log
return ll
}
// WithOutput is a chainable configuration function which sets the logger's
// logging output to the supplied io.Writer.
func (ll *DefaultLeveledLogger) WithOutput(output io.Writer) *DefaultLeveledLogger {
ll.writer.Output(output)
return ll
}
// SetLevel sets the logger's logging level.
func (ll *DefaultLeveledLogger) SetLevel(newLevel LogLevel) {
ll.level.Set(newLevel)
}
// Trace emits the preformatted message if the logger is at or below LogLevelTrace.
func (ll *DefaultLeveledLogger) Trace(msg string) {
ll.trace().Msgf(msg)
}
// Tracef formats and emits a message if the logger is at or below LogLevelTrace.
func (ll *DefaultLeveledLogger) Tracef(format string, args ...interface{}) {
ll.trace().Msgf(format, args...)
}
// Debug emits the preformatted message if the logger is at or below LogLevelDebug.
func (ll *DefaultLeveledLogger) Debug(msg string) {
ll.debug().Msgf(msg)
}
// Debugf formats and emits a message if the logger is at or below LogLevelDebug.
func (ll *DefaultLeveledLogger) Debugf(format string, args ...interface{}) {
ll.debug().Msgf(format, args...)
}
// Info emits the preformatted message if the logger is at or below LogLevelInfo.
func (ll *DefaultLeveledLogger) Info(msg string) {
ll.info().Msgf(msg)
}
// Infof formats and emits a message if the logger is at or below LogLevelInfo.
func (ll *DefaultLeveledLogger) Infof(format string, args ...interface{}) {
ll.info().Msgf(format, args...)
}
// Warn emits the preformatted message if the logger is at or below LogLevelWarn.
func (ll *DefaultLeveledLogger) Warn(msg string) {
ll.warn().Msgf(msg)
}
// Warnf formats and emits a message if the logger is at or below LogLevelWarn.
func (ll *DefaultLeveledLogger) Warnf(format string, args ...interface{}) {
ll.warn().Msgf(format, args...)
}
// Error emits the preformatted message if the logger is at or below LogLevelError.
func (ll *DefaultLeveledLogger) Error(msg string) {
ll.err().Msgf(msg)
}
// Errorf formats and emits a message if the logger is at or below LogLevelError.
func (ll *DefaultLeveledLogger) Errorf(format string, args ...interface{}) {
ll.err().Msgf(format, args...)
}
// NewDefaultLeveledLoggerForScope returns a configured LeveledLogger.
func NewDefaultLeveledLoggerForScope(scope string, level LogLevel, writer io.Writer) *DefaultLeveledLogger {
if writer == nil {
writer = defaultOutput
}
z := zerolog.New(writer).Level(toZerologLevel(level)).With().Timestamp()
// scope will be changed to the component name if it's from the pion library
_, file, _, _ := runtime.Caller(2)
if strings.Contains(file, "github.com/pion/") {
z = z.Str("scope", "pion").Str("component", scope)
} else {
z = z.Str("scope", scope)
}
zerologWriter := z.Logger()
logger := &DefaultLeveledLogger{
writer: &zerologWriter,
level: level,
}
return logger.
WithTraceLogger(zerologWriter.Trace).
WithDebugLogger(zerologWriter.Debug).
WithInfoLogger(zerologWriter.Info).
WithWarnLogger(zerologWriter.Warn).
WithErrorLogger(zerologWriter.Error)
}
// DefaultLoggerFactory define levels by scopes and creates new DefaultLeveledLogger.
type DefaultLoggerFactory struct {
Writer io.Writer
DefaultLogLevel LogLevel
ScopeLevels map[string]LogLevel
}
// NewDefaultLoggerFactory creates a new DefaultLoggerFactory.
func NewDefaultLoggerFactory() *DefaultLoggerFactory {
factory := DefaultLoggerFactory{}
factory.DefaultLogLevel = LogLevelError
factory.ScopeLevels = make(map[string]LogLevel)
factory.Writer = defaultOutput
logLevels := map[string]LogLevel{
"DISABLE": LogLevelDisabled,
"ERROR": LogLevelError,
"WARN": LogLevelWarn,
"INFO": LogLevelInfo,
"DEBUG": LogLevelDebug,
"TRACE": LogLevelTrace,
}
for name, level := range logLevels {
env := os.Getenv(fmt.Sprintf("PION_LOG_%s", name))
if env == "" {
env = os.Getenv(fmt.Sprintf("PIONS_LOG_%s", name))
}
if env == "" {
continue
}
if strings.ToLower(env) == "all" {
if factory.DefaultLogLevel < level {
factory.DefaultLogLevel = level
}
continue
}
scopes := strings.Split(strings.ToLower(env), ",")
for _, scope := range scopes {
factory.ScopeLevels[scope] = level
}
}
return &factory
}
// NewLogger returns a configured LeveledLogger for the given, argsscope.
func (f *DefaultLoggerFactory) NewLogger(scope string) LeveledLogger {
logLevel := f.DefaultLogLevel
if f.ScopeLevels != nil {
scopeLevel, found := f.ScopeLevels[scope]
if found {
logLevel = scopeLevel
}
}
return NewDefaultLeveledLoggerForScope(scope, logLevel, f.Writer)
}

View File

@ -1,261 +0,0 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package logging_test
import (
"bytes"
"os"
"strings"
"testing"
"github.com/pion/logging"
"github.com/stretchr/testify/assert"
)
func testNoDebugLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()
var outBuf bytes.Buffer
logger.WithOutput(&outBuf)
logger.Debug("this shouldn't be logged")
assert.GreaterOrEqual(t, 0, outBuf.Len(), "Debug was logged when it shouldn't have been")
logger.Debugf("this shouldn't be logged")
assert.GreaterOrEqual(t, 0, outBuf.Len(), "Debug was logged when it shouldn't have been")
}
func testDebugLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()
var outBuf bytes.Buffer
logger.WithOutput(&outBuf)
dbgMsg := "this is a debug message"
logger.Debug(dbgMsg)
assert.Truef(t, strings.Contains(outBuf.String(), dbgMsg),
"Expected to find %q in %q, but didn't", dbgMsg, outBuf.String())
assert.Truef(t, strings.Contains(outBuf.String(), dbgMsg),
"Expected to find %q in %q, but didn't", dbgMsg, outBuf.String())
logger.Debugf(dbgMsg) // nolint: govet
assert.Truef(t, strings.Contains(outBuf.String(), dbgMsg),
"Expected to find %q in %q, but didn't", dbgMsg, outBuf.String())
}
func testWarnLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()
var outBuf bytes.Buffer
logger.WithOutput(&outBuf)
warnMsg := "this is a warning message"
logger.Warn(warnMsg)
assert.Truef(t, strings.Contains(outBuf.String(), warnMsg),
"Expected to find %q in %q, but didn't", warnMsg, outBuf.String())
logger.Warnf(warnMsg) // nolint: govet
assert.Truef(t, strings.Contains(outBuf.String(), warnMsg),
"Expected to find %q in %q, but didn't", warnMsg, outBuf.String())
}
func testErrorLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()
var outBuf bytes.Buffer
logger.WithOutput(&outBuf)
errMsg := "this is an error message"
logger.Error(errMsg)
assert.Truef(t, strings.Contains(outBuf.String(), errMsg),
"Expected to find %q in %q but didn't", errMsg, outBuf.String())
logger.Errorf(errMsg) // nolint: govet
assert.Truef(t, strings.Contains(outBuf.String(), errMsg),
"Expected to find %q in %q but didn't", errMsg, outBuf.String())
}
func testTraceLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()
var outBuf bytes.Buffer
logger.WithOutput(&outBuf)
traceMsg := "trace message"
logger.Trace(traceMsg)
assert.Truef(t, strings.Contains(outBuf.String(), traceMsg),
"Expected to find %q in %q but didn't", traceMsg, outBuf.String())
logger.Tracef(traceMsg) // nolint: govet
assert.Truef(t, strings.Contains(outBuf.String(), traceMsg),
"Expected to find %q in %q but didn't", traceMsg, outBuf.String())
}
func testInfoLevel(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()
var outBuf bytes.Buffer
logger.WithOutput(&outBuf)
infoMsg := "info message"
logger.Info(infoMsg)
assert.Truef(t, strings.Contains(outBuf.String(), infoMsg),
"Expected to find %q in %q but didn't", infoMsg, outBuf.String())
logger.Infof(infoMsg) // nolint: govet
assert.Truef(t, strings.Contains(outBuf.String(), infoMsg),
"Expected to find %q in %q but didn't", infoMsg, outBuf.String())
}
func testAllLevels(t *testing.T, logger *logging.DefaultLeveledLogger) {
t.Helper()
testDebugLevel(t, logger)
testWarnLevel(t, logger)
testErrorLevel(t, logger)
testTraceLevel(t, logger)
testInfoLevel(t, logger)
}
func TestDefaultLoggerFactory(t *testing.T) {
factory := logging.DefaultLoggerFactory{
Writer: os.Stderr,
DefaultLogLevel: logging.LogLevelWarn,
ScopeLevels: map[string]logging.LogLevel{
"foo": logging.LogLevelDebug,
},
}
logger := factory.NewLogger("baz")
bazLogger, ok := logger.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Invalid logger type")
testNoDebugLevel(t, bazLogger)
testWarnLevel(t, bazLogger)
logger = factory.NewLogger("foo")
fooLogger, ok := logger.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Invalid logger type")
testDebugLevel(t, fooLogger)
}
func TestDefaultLogger(t *testing.T) {
logger := logging.
NewDefaultLeveledLoggerForScope("test1", logging.LogLevelWarn, os.Stderr)
testNoDebugLevel(t, logger)
testWarnLevel(t, logger)
testErrorLevel(t, logger)
}
func TestNewDefaultLoggerFactory(t *testing.T) {
factory := logging.NewDefaultLoggerFactory()
disabled := factory.NewLogger("DISABLE")
errorLevel := factory.NewLogger("ERROR")
warnLevel := factory.NewLogger("WARN")
infoLevel := factory.NewLogger("INFO")
debugLevel := factory.NewLogger("DEBUG")
traceLevel := factory.NewLogger("TRACE")
disabledLogger, ok := disabled.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Missing disabled logger")
errorLogger, ok := errorLevel.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Missing error logger")
warnLogger, ok := warnLevel.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Missing warn logger")
infoLogger, ok := infoLevel.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Missing info logger")
debugLogger, ok := debugLevel.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Missing debug logger")
traceLogger, ok := traceLevel.(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Missing trace logger")
testNoDebugLevel(t, disabledLogger)
testNoDebugLevel(t, errorLogger)
testNoDebugLevel(t, warnLogger)
testNoDebugLevel(t, infoLogger)
testNoDebugLevel(t, debugLogger)
testNoDebugLevel(t, traceLogger)
}
func TestNewDefaultLoggerFactoryLogAll(t *testing.T) {
t.Setenv("PION_LOG_ERROR", "all")
t.Setenv("PION_LOG_WARN", "all")
t.Setenv("PION_LOG_INFO", "all")
t.Setenv("PION_LOG_DEBUG", "all")
t.Setenv("PION_LOG_TRACE", "all")
factory := logging.NewDefaultLoggerFactory()
testAPI, ok := factory.NewLogger("test").(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Invalid logger factory type")
testAllLevels(t, testAPI)
}
func TestNewDefaultLoggerFactorySpecifcScopes(t *testing.T) {
t.Setenv("PION_LOG_DEBUG", "feature,rtp-logger")
factory := logging.NewDefaultLoggerFactory()
feature, ok := factory.NewLogger("feature").(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Invalid logger factory type")
rtp, ok := factory.NewLogger("rtp-logger").(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Invalid logger factory type")
noScope, ok := factory.NewLogger("no-scope").(*logging.DefaultLeveledLogger)
assert.True(t, ok, "Invalid logger factory type")
testDebugLevel(t, feature)
testDebugLevel(t, rtp)
testNoDebugLevel(t, noScope)
}
func TestSetLevel(t *testing.T) {
logger := logging.
NewDefaultLeveledLoggerForScope("testSetLevel", logging.LogLevelWarn, os.Stderr)
testNoDebugLevel(t, logger)
logger.SetLevel(logging.LogLevelDebug)
testDebugLevel(t, logger)
}
func TestLogLevel(t *testing.T) {
logLevel := logging.LogLevelDisabled
logLevel.Set(logging.LogLevelError)
assert.Equal(t, logging.LogLevelError, logLevel.Get(), "LogLevel was not set to LogLevelError")
}
func TestLogLevelString(t *testing.T) {
expected := map[logging.LogLevel]string{
logging.LogLevelDisabled: "Disabled",
logging.LogLevelError: "Error",
logging.LogLevelWarn: "Warn",
logging.LogLevelInfo: "Info",
logging.LogLevelDebug: "Debug",
logging.LogLevelTrace: "Trace",
logging.LogLevel(999): "UNKNOWN",
}
for level, expectedStr := range expected {
assert.Equal(t, expectedStr, level.String())
}
}
func TestNewDefaultLoggerStderr(t *testing.T) {
logger := logging.NewDefaultLeveledLoggerForScope("test", logging.LogLevelWarn, nil)
testNoDebugLevel(t, logger)
testWarnLevel(t, logger)
testErrorLevel(t, logger)
}

View File

@ -1,97 +0,0 @@
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package logging
import (
"sync/atomic"
"github.com/rs/zerolog"
)
// LogLevel represents the level at which the logger will emit log messages.
type LogLevel int32
// Set updates the LogLevel to the supplied value.
func (ll *LogLevel) Set(newLevel LogLevel) {
atomic.StoreInt32((*int32)(ll), int32(newLevel))
}
// Get retrieves the current LogLevel value.
func (ll *LogLevel) Get() LogLevel {
return LogLevel(atomic.LoadInt32((*int32)(ll)))
}
func (ll LogLevel) String() string {
switch ll {
case LogLevelDisabled:
return "Disabled"
case LogLevelError:
return "Error"
case LogLevelWarn:
return "Warn"
case LogLevelInfo:
return "Info"
case LogLevelDebug:
return "Debug"
case LogLevelTrace:
return "Trace"
default:
return "UNKNOWN"
}
}
const (
// LogLevelDisabled completely disables logging of any events.
LogLevelDisabled LogLevel = iota
// LogLevelError is for fatal errors which should be handled by user code,
// but are logged to ensure that they are seen.
LogLevelError
// LogLevelWarn is for logging abnormal, but non-fatal library operation.
LogLevelWarn
// LogLevelInfo is for logging normal library operation (e.g. state transitions, etc.).
LogLevelInfo
// LogLevelDebug is for logging low-level library information (e.g. internal operations).
LogLevelDebug
// LogLevelTrace is for logging very low-level library information (e.g. network traces).
LogLevelTrace
)
func toZerologLevel(level LogLevel) zerolog.Level {
switch level {
case LogLevelDisabled:
return zerolog.Disabled
case LogLevelError:
return zerolog.ErrorLevel
case LogLevelWarn:
return zerolog.WarnLevel
case LogLevelInfo:
return zerolog.InfoLevel
case LogLevelDebug:
return zerolog.DebugLevel
case LogLevelTrace:
return zerolog.TraceLevel
default:
return zerolog.NoLevel
}
}
// LeveledLogger is the basic pion Logger interface.
type LeveledLogger interface {
Trace(msg string)
Tracef(format string, args ...interface{})
Debug(msg string)
Debugf(format string, args ...interface{})
Info(msg string)
Infof(format string, args ...interface{})
Warn(msg string)
Warnf(format string, args ...interface{})
Error(msg string)
Errorf(format string, args ...interface{})
GetLogger() *zerolog.Logger
}
// LoggerFactory is the basic pion LoggerFactory interface.
type LoggerFactory interface {
NewLogger(scope string) LeveledLogger
}

View File

@ -8,7 +8,6 @@ import (
"sync"
"time"
"github.com/pion/logging"
"github.com/rs/zerolog"
)
@ -70,12 +69,12 @@ type UsbGadget struct {
const configFSPath = "/sys/kernel/config"
const gadgetPath = "/sys/kernel/config/usb_gadget"
var defaultLogger = logging.NewDefaultLoggerFactory().NewLogger("usbgadget").GetLogger()
var defaultLogger = zerolog.New(os.Stdout).Level(zerolog.InfoLevel)
// NewUsbGadget creates a new UsbGadget.
func NewUsbGadget(name string, enabledDevices *Devices, config *Config, logger *zerolog.Logger) *UsbGadget {
if logger == nil {
logger = defaultLogger
logger = &defaultLogger
}
if enabledDevices == nil {

91
log.go
View File

@ -2,19 +2,39 @@ package kvm
import (
"fmt"
"io"
"os"
"strings"
"sync"
"time"
"github.com/pion/logging"
"github.com/rs/zerolog"
)
const defaultLogLevel = zerolog.ErrorLevel
// we use logging framework from pion
// ref: https://github.com/pion/webrtc/wiki/Debugging-WebRTC
var rootLogger = logging.NewDefaultLoggerFactory().NewLogger("jetkvm").GetLogger()
var (
defaultLogOutput io.Writer = zerolog.ConsoleWriter{
Out: os.Stdout,
TimeFormat: time.RFC3339,
PartsOrder: []string{"time", "level", "scope", "component", "message"},
FieldsExclude: []string{"scope", "component"},
FormatPartValueByName: func(value interface{}, name string) string {
val := fmt.Sprintf("%s", value)
if name == "component" {
if value == nil {
return "-"
}
}
return val
},
}
defaultLogLevel = zerolog.ErrorLevel
rootLogger = zerolog.New(defaultLogOutput).With().
Str("scope", "jetkvm").
Timestamp().
Stack().
Logger()
)
var (
scopeLevels map[string]zerolog.Level
@ -29,13 +49,13 @@ var (
ntpLogger = getLogger("ntp")
displayLogger = getLogger("display")
usbLogger = getLogger("usb")
ginLogger = getLogger("gin")
)
func updateLogLevel() {
scopeLevelMutex.Lock()
defer scopeLevelMutex.Unlock()
defaultLevel := defaultLogLevel
logLevels := map[string]zerolog.Level{
"DISABLE": zerolog.Disabled,
"NOLEVEL": zerolog.NoLevel,
@ -66,8 +86,8 @@ func updateLogLevel() {
}
if strings.ToLower(env) == "all" {
if defaultLevel < level {
defaultLevel = level
if defaultLogLevel > level {
defaultLogLevel = level
}
continue
@ -92,5 +112,58 @@ func getLogger(scope string) zerolog.Logger {
return l.Level(level)
}
return l
return l.Level(defaultLogLevel)
}
type pionLogger struct {
logger *zerolog.Logger
}
// Print all messages except trace.
func (c pionLogger) Trace(msg string) {
c.logger.Trace().Msg(msg)
}
func (c pionLogger) Tracef(format string, args ...interface{}) {
c.logger.Trace().Msgf(format, args...)
}
func (c pionLogger) Debug(msg string) {
c.logger.Debug().Msg(msg)
}
func (c pionLogger) Debugf(format string, args ...interface{}) {
c.logger.Debug().Msgf(format, args...)
}
func (c pionLogger) Info(msg string) {
c.logger.Info().Msg(msg)
}
func (c pionLogger) Infof(format string, args ...interface{}) {
c.logger.Info().Msgf(format, args...)
}
func (c pionLogger) Warn(msg string) {
c.logger.Warn().Msg(msg)
}
func (c pionLogger) Warnf(format string, args ...interface{}) {
c.logger.Warn().Msgf(format, args...)
}
func (c pionLogger) Error(msg string) {
c.logger.Error().Msg(msg)
}
func (c pionLogger) Errorf(format string, args ...interface{}) {
c.logger.Error().Msgf(format, args...)
}
// customLoggerFactory satisfies the interface logging.LoggerFactory
// This allows us to create different loggers per subsystem. So we can
// add custom behavior.
type pionLoggerFactory struct{}
func (c pionLoggerFactory) NewLogger(subsystem string) logging.LeveledLogger {
logger := getLogger(subsystem).With().
Str("scope", "pion").
Str("component", subsystem).
Logger()
return pionLogger{logger: &logger}
}
var defaultLoggerFactory = &pionLoggerFactory{}

View File

@ -146,7 +146,8 @@ func startMDNS() error {
}
mDNSConn, err = mdns.Server(ipv4.NewPacketConn(l4), ipv6.NewPacketConn(l6), &mdns.Config{
LocalNames: []string{"jetkvm.local"}, //TODO: make it configurable
LocalNames: []string{"jetkvm.local"}, //TODO: make it configurable
LoggerFactory: defaultLoggerFactory,
})
if err != nil {
mDNSConn = nil

7
web.go
View File

@ -15,6 +15,7 @@ import (
"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
gin_logger "github.com/gin-contrib/logger"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/pion/webrtc/v4"
@ -66,7 +67,11 @@ func setupRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
gin.DisableConsoleColor()
r := gin.Default()
r.Use(gin_logger.SetLogger(
gin_logger.WithLogger(func(*gin.Context, zerolog.Logger) zerolog.Logger {
return ginLogger
}),
))
staticFS, _ := fs.Sub(staticFiles, "static")
// Add a custom middleware to set cache headers for images

View File

@ -65,7 +65,9 @@ func (s *Session) ExchangeOffer(offerStr string) (string, error) {
}
func newSession(config SessionConfig) (*Session, error) {
webrtcSettingEngine := webrtc.SettingEngine{}
webrtcSettingEngine := webrtc.SettingEngine{
LoggerFactory: defaultLoggerFactory,
}
iceServer := webrtc.ICEServer{}
if config.IsCloud {