pax_global_header00006660000000000000000000000064145211421220014504gustar00rootroot0000000000000052 comment=78b8af5329abd1ba8695aad821f95fb2e7f4e651 zapr-1.3.0/000077500000000000000000000000001452114212200124615ustar00rootroot00000000000000zapr-1.3.0/.github/000077500000000000000000000000001452114212200140215ustar00rootroot00000000000000zapr-1.3.0/.github/dependabot.yaml000066400000000000000000000004211452114212200170070ustar00rootroot00000000000000version: 2 updates: - package-ecosystem: "gomod" directory: "/" schedule: interval: "daily" open-pull-requests-limit: 10 - package-ecosystem: "github-actions" directory: "/" schedule: # Check for updates to GitHub Actions every week interval: "weekly"zapr-1.3.0/.github/workflows/000077500000000000000000000000001452114212200160565ustar00rootroot00000000000000zapr-1.3.0/.github/workflows/apidiff.yaml000066400000000000000000000013701452114212200203450ustar00rootroot00000000000000name: Run apidiff on: [ pull_request ] jobs: apidiff: runs-on: ubuntu-latest if: github.base_ref steps: - name: Install Go uses: actions/setup-go@v4 with: go-version: 1.18.x - name: Add GOBIN to PATH run: echo "PATH=$(go env GOPATH)/bin:$PATH" >>$GITHUB_ENV - name: Install dependencies run: GO111MODULE=off go get golang.org/x/exp/cmd/apidiff - name: Checkout old code uses: actions/checkout@v4 with: ref: ${{ github.base_ref }} path: "old" - name: Checkout new code uses: actions/checkout@v4 with: path: "new" - name: APIDiff run: ./_tools/apidiff.sh -d ../old working-directory: "new" zapr-1.3.0/.github/workflows/lint.yaml000066400000000000000000000011631452114212200177110ustar00rootroot00000000000000name: Run lint on: [ push, pull_request ] jobs: lint: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Lint uses: golangci/golangci-lint-action@v3 with: # version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version version: latest # Optional: show only new issues if it's a pull request. The default value is `false`. # only-new-issues: true # golangci-lint command line arguments. args: -v --max-same-issues 10 zapr-1.3.0/.github/workflows/tests.yaml000066400000000000000000000010041452114212200200770ustar00rootroot00000000000000name: Run tests on: [ push, pull_request ] jobs: test: strategy: matrix: go-versions: [ 1.18.x, 1.19.x, 1.20.x ] platform: [ ubuntu-latest, macos-latest, windows-latest ] runs-on: ${{ matrix.platform }} steps: - name: Install Go uses: actions/setup-go@v4 with: go-version: ${{ matrix.go-version }} - name: Checkout code uses: actions/checkout@v4 - name: Build run: go build -v ./... - name: Test run: go test -v -race ./... zapr-1.3.0/.gitignore000066400000000000000000000000111452114212200144410ustar00rootroot00000000000000*~ *.swp zapr-1.3.0/.golangci.yaml000066400000000000000000000004121452114212200152030ustar00rootroot00000000000000issues: exclude-use-default: false linters: disable-all: true enable: - asciicheck - errcheck - forcetypeassert - gocritic - gofmt - goimports - gosimple - govet - ineffassign - misspell - revive - staticcheck - typecheck - unused zapr-1.3.0/LICENSE000066400000000000000000000261351452114212200134750ustar00rootroot00000000000000 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. zapr-1.3.0/README.md000066400000000000000000000053511452114212200137440ustar00rootroot00000000000000Zapr :zap: ========== A [logr](https://github.com/go-logr/logr) implementation using [Zap](https://github.com/uber-go/zap). Can also be used as [slog](https://pkg.go.dev/log/slog) handler. Usage ----- Via logr: ```go package main import ( "fmt" "go.uber.org/zap" "github.com/go-logr/logr" "github.com/go-logr/zapr" ) func main() { var log logr.Logger zapLog, err := zap.NewDevelopment() if err != nil { panic(fmt.Sprintf("who watches the watchmen (%v)?", err)) } log = zapr.NewLogger(zapLog) log.Info("Logr in action!", "the answer", 42) } ``` Via slog: ``` package main import ( "fmt" "log/slog" "github.com/go-logr/logr/slogr" "github.com/go-logr/zapr" "go.uber.org/zap" ) func main() { var log *slog.Logger zapLog, err := zap.NewDevelopment() if err != nil { panic(fmt.Sprintf("who watches the watchmen (%v)?", err)) } log = slog.New(slogr.NewSlogHandler(zapr.NewLogger(zapLog))) log.Info("Logr in action!", "the answer", 42) } ``` Increasing Verbosity -------------------- Zap uses semantically named levels for logging (`DebugLevel`, `InfoLevel`, `WarningLevel`, ...). Logr uses arbitrary numeric levels. By default logr's `V(0)` is zap's `InfoLevel` and `V(1)` is zap's `DebugLevel` (which is numerically -1). Zap does not have named levels that are more verbose than `DebugLevel`, but it's possible to fake it. As of zap v1.19.0 you can do something like the following in your setup code: ```go zc := zap.NewProductionConfig() zc.Level = zap.NewAtomicLevelAt(zapcore.Level(-2)) z, err := zc.Build() if err != nil { // ... } log := zapr.NewLogger(z) ``` Zap's levels get more verbose as the number gets smaller and more important and the number gets larger (`DebugLevel` is -1, `InfoLevel` is 0, `WarnLevel` is 1, and so on). The `-2` in the above snippet means that `log.V(2).Info()` calls will be active. `-3` would enable `log.V(3).Info()`, etc. Note that zap's levels are `int8` which means the most verbose level you can give it is -128. The zapr implementation will cap `V()` levels greater than 127 to 127, so setting the zap level to -128 really means "activate all logs". Implementation Details ---------------------- For the most part, concepts in Zap correspond directly with those in logr. Unlike Zap, all fields *must* be in the form of sugared fields -- it's illegal to pass a strongly-typed Zap field in a key position to any of the logging methods (`Log`, `Error`). The zapr `logr.LogSink` implementation also implements `logr.SlogHandler`. That enables `slogr.NewSlogHandler` to provide a `slog.Handler` which just passes parameters through to zapr. zapr handles special slog values (Group, LogValuer), regardless of which front-end API is used. zapr-1.3.0/_tools/000077500000000000000000000000001452114212200137605ustar00rootroot00000000000000zapr-1.3.0/_tools/apidiff.sh000077500000000000000000000062631452114212200157300ustar00rootroot00000000000000#!/usr/bin/env bash # Copyright 2020 The Kubernetes Authors. # Copyright 2021 The logr Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. set -o errexit set -o nounset set -o pipefail function usage { local script="$(basename $0)" echo >&2 "Usage: ${script} [-r | -d ] This script should be run at the root of a module. -r Compare the exported API of the local working copy with the exported API of the local repo at the specified branch or tag. -d Compare the exported API of the local working copy with the exported API of the specified directory, which should point to the root of a different version of the same module. Examples: ${script} -r master ${script} -r v1.10.0 ${script} -r release-1.10 ${script} -d /path/to/historical/version " exit 1 } ref="" dir="" while getopts r:d: o do case "$o" in r) ref="$OPTARG";; d) dir="$OPTARG";; [?]) usage;; esac done # If REF and DIR are empty, print usage and error if [[ -z "${ref}" && -z "${dir}" ]]; then usage; fi # If REF and DIR are both set, print usage and error if [[ -n "${ref}" && -n "${dir}" ]]; then usage; fi if ! which apidiff > /dev/null; then echo "Installing golang.org/x/exp/cmd/apidiff" pushd "${TMPDIR:-/tmp}" > /dev/null GO111MODULE=off go get golang.org/x/exp/cmd/apidiff popd > /dev/null fi output=$(mktemp -d -t "apidiff.output.XXXX") cleanup_output () { rm -fr "${output}"; } trap cleanup_output EXIT # If ref is set, clone . to temp dir at $ref, and set $dir to the temp dir clone="" base="${dir}" if [[ -n "${ref}" ]]; then base="${ref}" clone=$(mktemp -d -t "apidiff.clone.XXXX") cleanup_clone_and_output () { rm -fr "${clone}"; cleanup_output; } trap cleanup_clone_and_output EXIT git clone . -q --no-tags "${clone}" git -C "${clone}" co "${ref}" dir="${clone}" fi pushd "${dir}" >/dev/null echo "Inspecting API of ${base}" go list ./... > packages.txt for pkg in $(cat packages.txt); do mkdir -p "${output}/${pkg}" apidiff -w "${output}/${pkg}/apidiff.output" "${pkg}" done popd >/dev/null retval=0 echo "Comparing with ${base}" for pkg in $(go list ./...); do # New packages are ok if [ ! -f "${output}/${pkg}/apidiff.output" ]; then continue fi # Check for incompatible changes to previous packages incompatible=$(apidiff -incompatible "${output}/${pkg}/apidiff.output" "${pkg}") if [[ -n "${incompatible}" ]]; then echo >&2 "FAIL: ${pkg} contains incompatible changes: ${incompatible} " retval=1 fi done # Check for removed packages removed=$(comm -23 "${dir}/packages.txt" <(go list ./...)) if [[ -n "${removed}" ]]; then echo >&2 "FAIL: removed packages: ${removed} " retval=1 fi exit $retval zapr-1.3.0/example/000077500000000000000000000000001452114212200141145ustar00rootroot00000000000000zapr-1.3.0/example/main.go000066400000000000000000000036771452114212200154040ustar00rootroot00000000000000/* Copyright 2019 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // This example shows how to instantiate a zap logger and what output // looks like. package main import ( "github.com/go-logr/logr" "github.com/go-logr/zapr" "github.com/go-logr/zapr/internal/types" "go.uber.org/zap" ) type e struct { str string } func (e e) Error() string { return e.str } func helper(log logr.Logger, msg string) { helper2(log, msg) } func helper2(log logr.Logger, msg string) { log.WithCallDepth(2).Info(msg) } func main() { zc := zap.NewProductionConfig() zc.Level = zap.NewAtomicLevelAt(zap.DebugLevel) zc.DisableStacktrace = true z, _ := zc.Build() log := zapr.NewLogger(z) log = log.WithName("MyName") example(log.WithValues("module", "example")) } // example only depends on logr except when explicitly breaking the // abstraction. Even that part is written so that it works with non-zap // loggers. func example(log logr.Logger) { v := types.ObjectRef{Name: "myname", Namespace: "myns"} log.Info("marshal", "stringer", v.String(), "raw", v) log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1}) log.V(1).Info("you should see this") log.V(1).V(1).Info("you should NOT see this") log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14}) log.Error(e{"an error occurred"}, "goodbye", "code", -1) helper(log, "thru a helper") if zapLogger, ok := log.GetSink().(zapr.Underlier); ok { _ = zapLogger.GetUnderlying().Core().Sync() } } zapr-1.3.0/example_test.go000066400000000000000000000076131452114212200155110ustar00rootroot00000000000000/* Copyright 2021 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr_test import ( "errors" "time" "github.com/go-logr/zapr" "github.com/go-logr/zapr/internal/types" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var errSome = errors.New("some error") func encodeTime(_ time.Time, enc zapcore.PrimitiveArrayEncoder) { // Suppress actual time to keep output constant. enc.AppendString("TIMESTAMP") } func buildZapLogger() *zap.Logger { // zap gets configured to not panic on invalid log calls // and to produce simple, deterministic output on stdout. zc := zap.NewProductionConfig() zc.OutputPaths = []string{"stdout"} zc.ErrorOutputPaths = zc.OutputPaths zc.DisableStacktrace = true zc.DisableCaller = true zc.EncoderConfig.EncodeTime = encodeTime z, _ := zc.Build() return z } func ExampleNewLogger() { log := zapr.NewLogger(buildZapLogger()) log.Info("info message with default options") log.Error(errSome, "error message with default options") log.Info("support for zap fields as key/value replacement is disabled", zap.Int("answer", 42)) log.Info("invalid key", 42, "answer") log.Info("missing value", "answer") obj := types.ObjectRef{Name: "john", Namespace: "doe"} log.Info("marshaler", "stringer", obj.String(), "raw", obj) // Output: // {"level":"info","ts":"TIMESTAMP","msg":"info message with default options"} // {"level":"error","ts":"TIMESTAMP","msg":"error message with default options","error":"some error"} // {"level":"dpanic","ts":"TIMESTAMP","msg":"strongly-typed Zap Field passed to logr","zap field":{"Key":"answer","Type":11,"Integer":42,"String":"","Interface":null}} // {"level":"info","ts":"TIMESTAMP","msg":"support for zap fields as key/value replacement is disabled"} // {"level":"dpanic","ts":"TIMESTAMP","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":42} // {"level":"info","ts":"TIMESTAMP","msg":"invalid key"} // {"level":"dpanic","ts":"TIMESTAMP","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"answer"} // {"level":"info","ts":"TIMESTAMP","msg":"missing value"} // {"level":"info","ts":"TIMESTAMP","msg":"marshaler","stringer":"doe/john","raw":{"name":"john","namespace":"doe"}} } func ExampleLogInfoLevel() { log := zapr.NewLoggerWithOptions(buildZapLogger(), zapr.LogInfoLevel("v")) log.Info("info message with numeric verbosity level") log.Error(errSome, "error messages have no numeric verbosity level") // Output: // {"level":"info","ts":"TIMESTAMP","msg":"info message with numeric verbosity level","v":0} // {"level":"error","ts":"TIMESTAMP","msg":"error messages have no numeric verbosity level","error":"some error"} } func ExampleErrorKey() { log := zapr.NewLoggerWithOptions(buildZapLogger(), zapr.ErrorKey("err")) log.Error(errSome, "error message with non-default error key") // Output: // {"level":"error","ts":"TIMESTAMP","msg":"error message with non-default error key","err":"some error"} } func ExampleAllowZapFields() { log := zapr.NewLoggerWithOptions(buildZapLogger(), zapr.AllowZapFields(true)) log.Info("log zap field", zap.Int("answer", 42)) // Output: // {"level":"info","ts":"TIMESTAMP","msg":"log zap field","answer":42} } func ExampleDPanicOnBugs() { log := zapr.NewLoggerWithOptions(buildZapLogger(), zapr.DPanicOnBugs(false)) log.Info("warnings suppressed", zap.Int("answer", 42)) // Output: // {"level":"info","ts":"TIMESTAMP","msg":"warnings suppressed"} } zapr-1.3.0/go.mod000066400000000000000000000005171452114212200135720ustar00rootroot00000000000000module github.com/go-logr/zapr go 1.18 require ( github.com/go-logr/logr v1.3.0 github.com/stretchr/testify v1.8.4 go.uber.org/zap v1.26.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect go.uber.org/multierr v1.10.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) zapr-1.3.0/go.sum000066400000000000000000000026301452114212200136150ustar00rootroot00000000000000github.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/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY= github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= 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/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk= go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ= go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo= go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so= 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= zapr-1.3.0/internal/000077500000000000000000000000001452114212200142755ustar00rootroot00000000000000zapr-1.3.0/internal/types/000077500000000000000000000000001452114212200154415ustar00rootroot00000000000000zapr-1.3.0/internal/types/objectref.go000066400000000000000000000027661452114212200177460ustar00rootroot00000000000000/* Copyright 2021 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Package types holds a copy of the ObjectRef type from klog for // use in the example. package types import ( "fmt" "github.com/go-logr/logr" ) // ObjectRef references a Kubernetes object type ObjectRef struct { Name string `json:"name"` Namespace string `json:"namespace,omitempty"` } func (ref ObjectRef) String() string { if ref.Namespace != "" { return fmt.Sprintf("%s/%s", ref.Namespace, ref.Name) } return ref.Name } // MarshalLog ensures that loggers with structured output ignore the String method. // // We implement fmt.Stringer for non-structured logging, but we want the // raw struct when using structured logs. Some logr implementations call // String if it is present, so we want to convert this struct to something // that doesn't have that method. func (ref ObjectRef) MarshalLog() interface{} { // Methods do not survive type definitions. type forLog ObjectRef return forLog(ref) } var _ logr.Marshaler = ObjectRef{} zapr-1.3.0/slog_test.go000066400000000000000000000121201452114212200150070ustar00rootroot00000000000000//go:build go1.21 // +build go1.21 /* Copyright 2023 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr_test import ( "bytes" "context" "encoding/json" "log/slog" "strings" "testing" "testing/slogtest" "github.com/go-logr/logr/slogr" "github.com/go-logr/zapr" "github.com/stretchr/testify/require" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) func TestSlogHandler(t *testing.T) { var buffer bytes.Buffer encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{ MessageKey: slog.MessageKey, TimeKey: slog.TimeKey, LevelKey: slog.LevelKey, EncodeLevel: func(level zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) { encoder.AppendInt(int(level)) }, }) core := zapcore.NewCore(encoder, zapcore.AddSync(&buffer), zapcore.Level(0)) zl := zap.New(core) logger := zapr.NewLogger(zl) handler := slogr.NewSlogHandler(logger) err := slogtest.TestHandler(handler, func() []map[string]any { _ = zl.Sync() return parseOutput(t, buffer.Bytes()) }) t.Logf("Log output:\n%s\nAs JSON:\n%v\n", buffer.String(), parseOutput(t, buffer.Bytes())) // Correlating failures with individual test cases is hard with the current API. // See https://github.com/golang/go/issues/61758 if err != nil { if err, ok := err.(interface { Unwrap() []error }); ok { for _, err := range err.Unwrap() { if !containsOne(err.Error(), "a Handler should ignore a zero Record.Time", // zapr always writes a time field. "a Handler should not output groups for an empty Record", // Relies on WithGroup and that always opens a group. Text may change, see https://go.dev/cl/516155 ) { t.Errorf("Unexpected error: %v", err) } } return } // Shouldn't be reached, errors from errors.Join can be split up. t.Errorf("Unexpected errors:\n%v", err) } } func containsOne(hay string, needles ...string) bool { for _, needle := range needles { if strings.Contains(hay, needle) { return true } } return false } // TestSlogCases covers some gaps in the coverage we get from // slogtest.TestHandler (empty and invalud PC, see // https://github.com/golang/go/issues/62280) and verbosity handling in // combination with V(). func TestSlogCases(t *testing.T) { for name, tc := range map[string]struct { record slog.Record v int expected string }{ "empty": { expected: `{"msg":"", "level":"info", "v":0}`, }, "invalid-pc": { record: slog.Record{PC: 1}, expected: `{"msg":"", "level":"info", "v":0}`, }, "debug": { record: slog.Record{Level: slog.LevelDebug}, expected: `{"msg":"", "level":"Level(-4)", "v":4}`, }, "warn": { record: slog.Record{Level: slog.LevelWarn}, expected: `{"msg":"", "level":"warn", "v":0}`, }, "error": { record: slog.Record{Level: slog.LevelError}, expected: `{"msg":"", "level":"error"}`, }, "debug-v1": { v: 1, record: slog.Record{Level: slog.LevelDebug}, expected: `{"msg":"", "level":"Level(-5)", "v":5}`, }, "warn-v1": { v: 1, record: slog.Record{Level: slog.LevelWarn}, expected: `{"msg":"", "level":"info", "v":0}`, }, "error-v1": { v: 1, record: slog.Record{Level: slog.LevelError}, expected: `{"msg":"", "level":"error"}`, }, "debug-v4": { v: 4, record: slog.Record{Level: slog.LevelDebug}, expected: `{"msg":"", "level":"Level(-8)", "v":8}`, }, "warn-v4": { v: 4, record: slog.Record{Level: slog.LevelWarn}, expected: `{"msg":"", "level":"info", "v":0}`, }, "error-v4": { v: 4, record: slog.Record{Level: slog.LevelError}, expected: `{"msg":"", "level":"error"}`, }, } { t.Run(name, func(t *testing.T) { var buffer bytes.Buffer encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{ MessageKey: slog.MessageKey, LevelKey: slog.LevelKey, EncodeLevel: func(level zapcore.Level, encoder zapcore.PrimitiveArrayEncoder) { encoder.AppendString(level.String()) }, }) core := zapcore.NewCore(encoder, zapcore.AddSync(&buffer), zapcore.Level(-10)) zl := zap.New(core) logger := zapr.NewLoggerWithOptions(zl, zapr.LogInfoLevel("v")) handler := slogr.NewSlogHandler(logger.V(tc.v)) require.NoError(t, handler.Handle(context.Background(), tc.record)) _ = zl.Sync() require.JSONEq(t, tc.expected, buffer.String()) }) } } func parseOutput(t *testing.T, output []byte) []map[string]any { var ms []map[string]any for _, line := range bytes.Split(output, []byte{'\n'}) { if len(line) == 0 { continue } var m map[string]any if err := json.Unmarshal(line, &m); err != nil { t.Fatal(err) } ms = append(ms, m) } return ms } zapr-1.3.0/slogzapr.go000066400000000000000000000113511452114212200146520ustar00rootroot00000000000000//go:build go1.21 // +build go1.21 /* Copyright 2023 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr import ( "context" "log/slog" "runtime" "github.com/go-logr/logr/slogr" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) var _ slogr.SlogSink = &zapLogger{} func (zl *zapLogger) Handle(_ context.Context, record slog.Record) error { zapLevel := zap.InfoLevel intLevel := 0 isError := false switch { case record.Level >= slog.LevelError: zapLevel = zap.ErrorLevel isError = true case record.Level >= slog.LevelWarn: zapLevel = zap.WarnLevel case record.Level >= 0: // Already set above -> info. default: zapLevel = zapcore.Level(record.Level) intLevel = int(-zapLevel) } if checkedEntry := zl.l.Check(zapLevel, record.Message); checkedEntry != nil { checkedEntry.Time = record.Time checkedEntry.Caller = pcToCallerEntry(record.PC) var fieldsBuffer [2]zap.Field fields := fieldsBuffer[:0] if !isError && zl.numericLevelKey != "" { // Record verbosity for info entries. fields = append(fields, zap.Int(zl.numericLevelKey, intLevel)) } // Inline all attributes. fields = append(fields, zap.Inline(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error { record.Attrs(func(attr slog.Attr) bool { encodeSlog(enc, attr) return true }) return nil }))) checkedEntry.Write(fields...) } return nil } func encodeSlog(enc zapcore.ObjectEncoder, attr slog.Attr) { if attr.Equal(slog.Attr{}) { // Ignore empty attribute. return } // Check in order of expected frequency, most common ones first. // // Usage statistics for parameters from Kubernetes 152876a3e, // calculated with k/k/test/integration/logs/benchmark: // // kube-controller-manager -v10: // strings: 10043 (85%) // with API objects: 2 (0% of all arguments) // types and their number of usage: NodeStatus:2 // numbers: 792 (6%) // ObjectRef: 292 (2%) // others: 595 (5%) // // kube-scheduler -v10: // strings: 1325 (40%) // with API objects: 109 (3% of all arguments) // types and their number of usage: PersistentVolume:50 PersistentVolumeClaim:59 // numbers: 473 (14%) // ObjectRef: 1305 (39%) // others: 176 (5%) kind := attr.Value.Kind() switch kind { case slog.KindString: enc.AddString(attr.Key, attr.Value.String()) case slog.KindLogValuer: // This includes klog.KObj. encodeSlog(enc, slog.Attr{ Key: attr.Key, Value: attr.Value.Resolve(), }) case slog.KindInt64: enc.AddInt64(attr.Key, attr.Value.Int64()) case slog.KindUint64: enc.AddUint64(attr.Key, attr.Value.Uint64()) case slog.KindFloat64: enc.AddFloat64(attr.Key, attr.Value.Float64()) case slog.KindBool: enc.AddBool(attr.Key, attr.Value.Bool()) case slog.KindDuration: enc.AddDuration(attr.Key, attr.Value.Duration()) case slog.KindTime: enc.AddTime(attr.Key, attr.Value.Time()) case slog.KindGroup: attrs := attr.Value.Group() if attr.Key == "" { // Inline group. for _, attr := range attrs { encodeSlog(enc, attr) } return } if len(attrs) == 0 { // Ignore empty group. return } _ = enc.AddObject(attr.Key, marshalAttrs(attrs)) default: // We have to go through reflection in zap.Any to get support // for e.g. fmt.Stringer. zap.Any(attr.Key, attr.Value.Any()).AddTo(enc) } } type marshalAttrs []slog.Attr func (attrs marshalAttrs) MarshalLogObject(enc zapcore.ObjectEncoder) error { for _, attr := range attrs { encodeSlog(enc, attr) } return nil } var _ zapcore.ObjectMarshaler = marshalAttrs(nil) func pcToCallerEntry(pc uintptr) zapcore.EntryCaller { if pc == 0 { return zapcore.EntryCaller{} } // Same as https://cs.opensource.google/go/x/exp/+/642cacee:slog/record.go;drc=642cacee5cc05231f45555a333d07f1005ffc287;l=70 fs := runtime.CallersFrames([]uintptr{pc}) f, _ := fs.Next() if f.File == "" { return zapcore.EntryCaller{} } return zapcore.EntryCaller{ Defined: true, PC: pc, File: f.File, Line: f.Line, Function: f.Function, } } func (zl *zapLogger) WithAttrs(attrs []slog.Attr) slogr.SlogSink { newLogger := *zl newLogger.l = newLogger.l.With(zap.Inline(marshalAttrs(attrs))) return &newLogger } func (zl *zapLogger) WithGroup(name string) slogr.SlogSink { newLogger := *zl newLogger.l = newLogger.l.With(zap.Namespace(name)) return &newLogger } zapr-1.3.0/zapr.go000066400000000000000000000232171452114212200137710ustar00rootroot00000000000000/* Copyright 2019 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Copyright 2018 Solly Ross // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // Package zapr defines an implementation of the github.com/go-logr/logr // interfaces built on top of Zap (go.uber.org/zap). // // # Usage // // A new logr.Logger can be constructed from an existing zap.Logger using // the NewLogger function: // // log := zapr.NewLogger(someZapLogger) // // # Implementation Details // // For the most part, concepts in Zap correspond directly with those in // logr. // // Unlike Zap, all fields *must* be in the form of sugared fields -- // it's illegal to pass a strongly-typed Zap field in a key position // to any of the log methods. // // Levels in logr correspond to custom debug levels in Zap. Any given level // in logr is represents by its inverse in zap (`zapLevel = -1*logrLevel`). // For example V(2) is equivalent to log level -2 in Zap, while V(1) is // equivalent to Zap's DebugLevel. package zapr import ( "fmt" "github.com/go-logr/logr" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) // NB: right now, we always use the equivalent of sugared logging. // This is necessary, since logr doesn't define non-suggared types, // and using zap-specific non-suggared types would make uses tied // directly to Zap. // zapLogger is a logr.Logger that uses Zap to log. The level has already been // converted to a Zap level, which is to say that `logrLevel = -1*zapLevel`. type zapLogger struct { // NB: this looks very similar to zap.SugaredLogger, but // deals with our desire to have multiple verbosity levels. l *zap.Logger // numericLevelKey controls whether the numeric logr level is // added to each Info log message and with which key. numericLevelKey string // errorKey is the field name used for the error in // Logger.Error calls. errorKey string // allowZapFields enables logging of strongly-typed Zap // fields. It is off by default because it breaks // implementation agnosticism. allowZapFields bool // panicMessages enables log messages for invalid log calls // that explain why a call was invalid (for example, // non-string key). This is enabled by default. panicMessages bool } const ( // noLevel tells handleFields to not inject a numeric log level field. noLevel = -1 ) // handleFields converts a bunch of arbitrary key-value pairs into Zap fields. It takes // additional pre-converted Zap fields, for use with automatically attached fields, like // `error`. func (zl *zapLogger) handleFields(lvl int, args []interface{}, additional ...zap.Field) []zap.Field { injectNumericLevel := zl.numericLevelKey != "" && lvl != noLevel // a slightly modified version of zap.SugaredLogger.sweetenFields if len(args) == 0 { // fast-return if we have no suggared fields and no "v" field. if !injectNumericLevel { return additional } // Slightly slower fast path when we need to inject "v". return append(additional, zap.Int(zl.numericLevelKey, lvl)) } // unlike Zap, we can be pretty sure users aren't passing structured // fields (since logr has no concept of that), so guess that we need a // little less space. numFields := len(args)/2 + len(additional) if injectNumericLevel { numFields++ } fields := make([]zap.Field, 0, numFields) if injectNumericLevel { fields = append(fields, zap.Int(zl.numericLevelKey, lvl)) } for i := 0; i < len(args); { // Check just in case for strongly-typed Zap fields, // which might be illegal (since it breaks // implementation agnosticism). If disabled, we can // give a better error message. if field, ok := args[i].(zap.Field); ok { if zl.allowZapFields { fields = append(fields, field) i++ continue } if zl.panicMessages { zl.l.WithOptions(zap.AddCallerSkip(1)).DPanic("strongly-typed Zap Field passed to logr", zapIt("zap field", args[i])) } break } // make sure this isn't a mismatched key if i == len(args)-1 { if zl.panicMessages { zl.l.WithOptions(zap.AddCallerSkip(1)).DPanic("odd number of arguments passed as key-value pairs for logging", zapIt("ignored key", args[i])) } break } // process a key-value pair, // ensuring that the key is a string key, val := args[i], args[i+1] keyStr, isString := key.(string) if !isString { // if the key isn't a string, DPanic and stop logging if zl.panicMessages { zl.l.WithOptions(zap.AddCallerSkip(1)).DPanic("non-string key argument passed to logging, ignoring all later arguments", zapIt("invalid key", key)) } break } fields = append(fields, zapIt(keyStr, val)) i += 2 } return append(fields, additional...) } func invokeMarshaler(field string, m logr.Marshaler) (f string, ret interface{}) { defer func() { if r := recover(); r != nil { ret = fmt.Sprintf("PANIC=%s", r) f = field + "Error" } }() return field, m.MarshalLog() } func (zl *zapLogger) Init(ri logr.RuntimeInfo) { zl.l = zl.l.WithOptions(zap.AddCallerSkip(ri.CallDepth)) } // Zap levels are int8 - make sure we stay in bounds. logr itself should // ensure we never get negative values. func toZapLevel(lvl int) zapcore.Level { if lvl > 127 { lvl = 127 } // zap levels are inverted. return 0 - zapcore.Level(lvl) } func (zl zapLogger) Enabled(lvl int) bool { return zl.l.Core().Enabled(toZapLevel(lvl)) } func (zl *zapLogger) Info(lvl int, msg string, keysAndVals ...interface{}) { if checkedEntry := zl.l.Check(toZapLevel(lvl), msg); checkedEntry != nil { checkedEntry.Write(zl.handleFields(lvl, keysAndVals)...) } } func (zl *zapLogger) Error(err error, msg string, keysAndVals ...interface{}) { if checkedEntry := zl.l.Check(zap.ErrorLevel, msg); checkedEntry != nil { checkedEntry.Write(zl.handleFields(noLevel, keysAndVals, zap.NamedError(zl.errorKey, err))...) } } func (zl *zapLogger) WithValues(keysAndValues ...interface{}) logr.LogSink { newLogger := *zl newLogger.l = zl.l.With(zl.handleFields(noLevel, keysAndValues)...) return &newLogger } func (zl *zapLogger) WithName(name string) logr.LogSink { newLogger := *zl newLogger.l = zl.l.Named(name) return &newLogger } func (zl *zapLogger) WithCallDepth(depth int) logr.LogSink { newLogger := *zl newLogger.l = zl.l.WithOptions(zap.AddCallerSkip(depth)) return &newLogger } // Underlier exposes access to the underlying logging implementation. Since // callers only have a logr.Logger, they have to know which implementation is // in use, so this interface is less of an abstraction and more of way to test // type conversion. type Underlier interface { GetUnderlying() *zap.Logger } func (zl *zapLogger) GetUnderlying() *zap.Logger { return zl.l } // NewLogger creates a new logr.Logger using the given Zap Logger to log. func NewLogger(l *zap.Logger) logr.Logger { return NewLoggerWithOptions(l) } // NewLoggerWithOptions creates a new logr.Logger using the given Zap Logger to // log and applies additional options. func NewLoggerWithOptions(l *zap.Logger, opts ...Option) logr.Logger { // creates a new logger skipping one level of callstack log := l.WithOptions(zap.AddCallerSkip(1)) zl := &zapLogger{ l: log, } zl.errorKey = "error" zl.panicMessages = true for _, option := range opts { option(zl) } return logr.New(zl) } // Option is one additional parameter for NewLoggerWithOptions. type Option func(*zapLogger) // LogInfoLevel controls whether a numeric log level is added to // Info log message. The empty string disables this, a non-empty // string is the key for the additional field. Errors and // internal panic messages do not have a log level and thus // are always logged without this extra field. func LogInfoLevel(key string) Option { return func(zl *zapLogger) { zl.numericLevelKey = key } } // ErrorKey replaces the default "error" field name used for the error // in Logger.Error calls. func ErrorKey(key string) Option { return func(zl *zapLogger) { zl.errorKey = key } } // AllowZapFields controls whether strongly-typed Zap fields may // be passed instead of a key/value pair. This is disabled by // default because it breaks implementation agnosticism. func AllowZapFields(allowed bool) Option { return func(zl *zapLogger) { zl.allowZapFields = allowed } } // DPanicOnBugs controls whether extra log messages are emitted for // invalid log calls with zap's DPanic method. Depending on the // configuration of the zap logger, the program then panics after // emitting the log message which is useful in development because // such invalid log calls are bugs in the program. The log messages // explain why a call was invalid (for example, non-string // key). Emitting them is enabled by default. func DPanicOnBugs(enabled bool) Option { return func(zl *zapLogger) { zl.panicMessages = enabled } } var _ logr.LogSink = &zapLogger{} var _ logr.CallDepthLogSink = &zapLogger{} zapr-1.3.0/zapr_noslog.go000066400000000000000000000017071452114212200153520ustar00rootroot00000000000000//go:build !go1.21 // +build !go1.21 /* Copyright 2023 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr import ( "github.com/go-logr/logr" "go.uber.org/zap" ) func zapIt(field string, val interface{}) zap.Field { // Handle types that implement logr.Marshaler: log the replacement // object instead of the original one. if marshaler, ok := val.(logr.Marshaler); ok { field, val = invokeMarshaler(field, marshaler) } return zap.Any(field, val) } zapr-1.3.0/zapr_noslog_test.go000066400000000000000000000027631452114212200164140ustar00rootroot00000000000000//go:build !go1.21 && !go1.21 // +build !go1.21,!go1.21 /* Copyright 2023 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr_test import ( "fmt" "github.com/go-logr/logr" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) // These implementations only exist to allow the tests to compile. Test cases // that depend on slog support get skipped at runtime. func hasSlog() bool { return false } func slogInt(key string, value int) zap.Field { return zap.Int(key, value) } func slogString(key string, value string) zap.Field { return zap.String(key, value) } func slogGroup(key string, values ...zap.Field) zap.Field { return zap.Object(key, zapcore.ObjectMarshalerFunc(func(encoder zapcore.ObjectEncoder) error { for _, value := range values { value.AddTo(encoder) } return nil })) } func slogValue(value interface{}) string { return fmt.Sprintf("%v", value) } func slogValuer(value interface{}) interface{} { return value } func logWithSlog(_ logr.Logger, _ string, _, _ []interface{}) { } zapr-1.3.0/zapr_slog.go000066400000000000000000000025471452114212200150200ustar00rootroot00000000000000//go:build go1.21 // +build go1.21 /* Copyright 2023 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr import ( "log/slog" "github.com/go-logr/logr" "go.uber.org/zap" "go.uber.org/zap/zapcore" ) func zapIt(field string, val interface{}) zap.Field { switch valTyped := val.(type) { case logr.Marshaler: // Handle types that implement logr.Marshaler: log the replacement // object instead of the original one. field, val = invokeMarshaler(field, valTyped) case slog.LogValuer: // The same for slog.LogValuer. We let slog.Value handle // potential panics and recursion. val = slog.AnyValue(val).Resolve() } if slogValue, ok := val.(slog.Value); ok { return zap.Inline(zapcore.ObjectMarshalerFunc(func(enc zapcore.ObjectEncoder) error { encodeSlog(enc, slog.Attr{Key: field, Value: slogValue}) return nil })) } return zap.Any(field, val) } zapr-1.3.0/zapr_slog_test.go000066400000000000000000000034041452114212200160500ustar00rootroot00000000000000//go:build go1.21 // +build go1.21 /* Copyright 2023 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr_test import ( "log/slog" "github.com/go-logr/logr" "github.com/go-logr/logr/slogr" ) func hasSlog() bool { return true } func (m *marshaler) LogValue() slog.Value { if m == nil { // TODO: simulate crash once slog handles it. return slog.StringValue("msg=") } return slog.StringValue("msg=" + m.msg) } var _ slog.LogValuer = &marshaler{} func slogInt(key string, value int) slog.Attr { return slog.Int(key, value) } func slogString(key string, value string) slog.Attr { return slog.String(key, value) } func slogGroup(key string, values ...interface{}) slog.Attr { return slog.Group(key, values...) } func slogValue(value interface{}) slog.Value { return slog.AnyValue(value) } func slogValuer(value interface{}) slog.LogValuer { return valuer{value: value} } type valuer struct { value interface{} } func (v valuer) LogValue() slog.Value { return slog.AnyValue(v.value) } var _ slog.LogValuer = valuer{} func logWithSlog(l logr.Logger, msg string, withKeysValues, keysValues []interface{}) { logger := slog.New(slogr.NewSlogHandler(l)) if withKeysValues != nil { logger = logger.With(withKeysValues...) } logger.Info(msg, keysValues...) } zapr-1.3.0/zapr_test.go000066400000000000000000000445171452114212200150360ustar00rootroot00000000000000/* Copyright 2020 The Kubernetes Authors. Copyright 2021 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr_test import ( "bufio" "bytes" "fmt" "regexp" "strings" "testing" "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" "go.uber.org/zap/zapcore" "github.com/go-logr/logr" "github.com/go-logr/zapr" ) const fixedTime = 123.456789 func fixedTimeEncoder(_ time.Time, enc zapcore.PrimitiveArrayEncoder) { enc.AppendFloat64(fixedTime) } // discard is a replacement for io.Discard, needed for Go 1.14. type discard struct{} func (d discard) Write(_ []byte) (int, error) { return 0, nil } type marshaler struct { msg string } func (m *marshaler) String() string { return m.msg } func (m *marshaler) MarshalLog() interface{} { return "msg=" + m.msg } var _ fmt.Stringer = &marshaler{} var _ logr.Marshaler = &marshaler{} type stringer struct { msg string } func (s *stringer) String() string { return s.msg } var _ fmt.Stringer = &stringer{} type stringerPanic struct { } func (s *stringerPanic) String() string { panic("fake panic") } var _ fmt.Stringer = &stringerPanic{} func newZapLogger(lvl zapcore.Level, w zapcore.WriteSyncer) *zap.Logger { if w == nil { w = zapcore.AddSync(discard{}) } encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{ MessageKey: "msg", CallerKey: "caller", TimeKey: "ts", EncodeTime: fixedTimeEncoder, EncodeDuration: zapcore.StringDurationEncoder, EncodeCaller: zapcore.ShortCallerEncoder, }) core := zapcore.NewCore(encoder, zapcore.AddSync(w), lvl) l := zap.New(core, zap.WithCaller(true)) return l } // TestInfo tests the JSON info format. func TestInfo(t *testing.T) { type testCase struct { msg string format string // If empty, only formatting with slog as API is supported. formatSlog string // If empty, formatting with slog as API yields the same result as logr. names []string withKeysValues []interface{} keysValues []interface{} wrapper func(logr.Logger, string, ...interface{}) needSlog bool } var testDataInfo = []testCase{ { msg: "simple", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"simple","v":0} `, }, { msg: "WithCallDepth", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"WithCallDepth","v":0} `, wrapper: myInfo, }, { msg: "incremental WithCallDepth", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"incremental WithCallDepth","v":0} `, wrapper: myInfoInc, }, { msg: "one name", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"one name","v":0} `, names: []string{"me"}, }, { msg: "many names", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"many names","v":0} `, names: []string{"hello", "world"}, }, { msg: "key-value pairs", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"key-value pairs","v":0,"ns":"default","podnum":2} `, keysValues: []interface{}{"ns", "default", "podnum", 2}, }, { msg: "WithValues", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"WithValues","ns":"default","podnum":2,"v":0} `, withKeysValues: []interface{}{"ns", "default", "podnum", 2}, }, { msg: "empty WithValues", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"empty WithValues","v":0,"ns":"default","podnum":2} `, withKeysValues: []interface{}{}, keysValues: []interface{}{"ns", "default", "podnum", 2}, }, { msg: "mixed", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"mixed","ns":"default","v":0,"podnum":2} `, withKeysValues: []interface{}{"ns", "default"}, keysValues: []interface{}{"podnum", 2}, }, { msg: "invalid WithValues", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":200} {"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"invalid WithValues","ns":"default","podnum":2,"v":0} `, formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"invalid WithValues","ns":"default","podnum":2,"!BADKEY":200,"replica":"Running","!BADKEY":10,"v":0} `, withKeysValues: []interface{}{"ns", "default", "podnum", 2, 200, "replica", "Running", 10}, }, { msg: "strongly typed Zap field", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"strongly-typed Zap Field passed to logr","zap field":{"Key":"zap-field-attempt","Type":11,"Integer":3,"String":"","Interface":null}} {"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"strongly typed Zap field","v":0,"ns":"default","podnum":2,"zap-field-attempt":3,"Running":10} `, formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"strongly typed Zap field","v":0,"ns":"default","podnum":2,"!BADKEY":{"Key":"zap-field-attempt","Type":11,"Integer":3,"String":"","Interface":null},"Running":10} `, keysValues: []interface{}{"ns", "default", "podnum", 2, zap.Int("zap-field-attempt", 3), "Running", 10}, }, { msg: "non-string key argument", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"non-string key argument passed to logging, ignoring all later arguments","invalid key":200} {"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"non-string key argument","v":0,"ns":"default","podnum":2} `, formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"non-string key argument","v":0,"ns":"default","podnum":2,"!BADKEY":200,"replica":"Running","!BADKEY":10} `, keysValues: []interface{}{"ns", "default", "podnum", 2, 200, "replica", "Running", 10}, }, { msg: "missing value", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"odd number of arguments passed as key-value pairs for logging","ignored key":"no-value"} {"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"missing value","v":0,"ns":"default","podnum":2} `, formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"missing value","v":0,"ns":"default","podnum":2,"!BADKEY":"no-value"} `, keysValues: []interface{}{"ns", "default", "podnum", 2, "no-value"}, }, { msg: "duration value argument", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"duration value argument","v":0,"duration":"5s"} `, keysValues: []interface{}{"duration", time.Duration(5 * time.Second)}, }, { msg: "valid marshaler", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"valid marshaler","v":0,"obj":"msg=hello"} `, keysValues: []interface{}{"obj", &marshaler{msg: "hello"}}, }, { msg: "nil marshaler", // Handled by our code: it just formats the error. format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"nil marshaler","v":0,"objError":"PANIC=runtime error: invalid memory address or nil pointer dereference"} `, formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"nil marshaler","v":0,"obj":"msg="} `, keysValues: []interface{}{"obj", (*marshaler)(nil)}, }, { msg: "nil stringer", // Handled by zap: it detects a nil pointer. format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"nil stringer","v":0,"obj":""} `, keysValues: []interface{}{"obj", (*stringer)(nil)}, }, { msg: "panic stringer", // Handled by zap: it prints the panic, but using a different key and format than funcr. format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"panic stringer","v":0,"objError":"PANIC=fake panic"} `, keysValues: []interface{}{"obj", &stringerPanic{}}, }, { msg: "slog values", format: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"slog values","v":0,"valuer":"some string","str":"another string","int64":-1,"uint64":2,"float64":3.124,"bool":true,"duration":"1s","timestamp":123.456789,"struct":{"SomeValue":42}} `, keysValues: []interface{}{"valuer", slogValuer("some string"), "str", slogValue("another string"), "int64", slogValue(int64(-1)), "uint64", slogValue(uint64(2)), "float64", slogValue(float64(3.124)), "bool", slogValue(true), "duration", slogValue(time.Second), "timestamp", slogValue(time.Time{} /* replaced by custom formatter */), "struct", slogValue(struct{ SomeValue int }{SomeValue: 42}), }, needSlog: true, }, { msg: "group with empty key", formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"group with empty key","v":0,"int":1,"string":"hello"} `, keysValues: []interface{}{slogGroup("", slogInt("int", 1), slogString("string", "hello"))}, needSlog: true, }, { msg: "empty group", formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"empty group","v":0} `, keysValues: []interface{}{slogGroup("obj")}, needSlog: true, }, { msg: "group with key", formatSlog: `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"group with key","v":0,"obj":{"int":1,"string":"hello"}} `, keysValues: []interface{}{slogGroup("obj", slogInt("int", 1), slogString("string", "hello"))}, needSlog: true, }, } test := func(t *testing.T, logNumeric *string, enablePanics *bool, allowZapFields *bool, useSlog bool, data testCase) { if (data.needSlog || useSlog) && !hasSlog() { t.Skip("slog is not supported") } if allowZapFields != nil && *allowZapFields && useSlog { t.Skip("zap fields not supported by slog") } if (enablePanics == nil || *enablePanics) && useSlog { t.Skip("printing additional log messages not supported by slog") } if !useSlog && data.format == "" { t.Skip("test case only supported for slog") } var buffer bytes.Buffer writer := bufio.NewWriter(&buffer) var sampleInfoLogger logr.Logger zl := newZapLogger(zapcore.Level(-100), zapcore.AddSync(writer)) if logNumeric == nil && enablePanics == nil && allowZapFields == nil { // No options. sampleInfoLogger = zapr.NewLogger(zl) } else { opts := []zapr.Option{} if logNumeric != nil { opts = append(opts, zapr.LogInfoLevel(*logNumeric)) } if enablePanics != nil { opts = append(opts, zapr.DPanicOnBugs(*enablePanics)) } if allowZapFields != nil { opts = append(opts, zapr.AllowZapFields(*allowZapFields)) } sampleInfoLogger = zapr.NewLoggerWithOptions(zl, opts...) } if useSlog { if len(data.names) > 0 { t.Skip("WithName not supported for slog") // logger = logger.WithName(name) } if data.wrapper != nil { t.Skip("slog does not support WithCallDepth") } logWithSlog(sampleInfoLogger, data.msg, data.withKeysValues, data.keysValues) } else { if data.withKeysValues != nil { sampleInfoLogger = sampleInfoLogger.WithValues(data.withKeysValues...) } for _, name := range data.names { sampleInfoLogger = sampleInfoLogger.WithName(name) } if data.wrapper != nil { data.wrapper(sampleInfoLogger, data.msg, data.keysValues...) } else { sampleInfoLogger.Info(data.msg, data.keysValues...) } } if err := writer.Flush(); err != nil { t.Fatalf("unexpected error from Flush: %v", err) } logStr := buffer.String() logStrLines := strings.Split(logStr, "\n") var dataFormatLines []string noPanics := enablePanics != nil && !*enablePanics withZapFields := allowZapFields != nil && *allowZapFields format := data.format if data.formatSlog != "" && useSlog { format = data.formatSlog } for _, line := range strings.Split(format, "\n") { // Potentially filter out all or some panic // message. We can recognize them based on the // expected special keys. if strings.Contains(line, "invalid key") || strings.Contains(line, "ignored key") { if noPanics || useSlog { continue } } else if strings.Contains(line, "zap field") { if noPanics || withZapFields || useSlog { continue } } haveZapField := strings.Index(line, `"zap-field`) if haveZapField != -1 && !withZapFields && !strings.Contains(line, "zap field") && !useSlog { // When Zap fields are not allowed, output gets truncated at the first Zap field. line = line[0:haveZapField-1] + "}" } dataFormatLines = append(dataFormatLines, line) } if !assert.Equal(t, len(logStrLines), len(dataFormatLines)) { t.Errorf("Info has wrong format: no. of lines in log is incorrect \n expected: %s\n got: %s", dataFormatLines, logStrLines) return } for i := range logStrLines { if len(logStrLines[i]) == 0 && len(dataFormatLines[i]) == 0 { continue } var ts float64 var lineNo int format := dataFormatLines[i] actual := logStrLines[i] // TODO: as soon as all supported Go versions have log/slog, // the code from slogzapr_test.go can be moved into zapr_test.go // and this Replace can get removed. actual = strings.ReplaceAll(actual, "zapr_slog_test.go", "zapr_test.go") if logNumeric == nil || *logNumeric == "" { format = regexp.MustCompile(`,"v":-?\d`).ReplaceAllString(format, "") } n, err := fmt.Sscanf(actual, format, &ts, &lineNo) if n != 2 || err != nil { t.Errorf("log format error: %d elements, error %s:\n%s", n, err, actual) } expected := fmt.Sprintf(format, fixedTime, lineNo) require.JSONEq(t, expected, actual) } } noV := "" v := "v" for name, logNumeric := range map[string]*string{"default": nil, "disabled": &noV, "v": &v} { t.Run(fmt.Sprintf("numeric level %v", name), func(t *testing.T) { yes := true no := false nilYesNo := map[string]*bool{"default": nil, "yes": &yes, "no": &no} for name, panicMessages := range nilYesNo { t.Run(fmt.Sprintf("panic messages %s", name), func(t *testing.T) { for name, allowZapFields := range nilYesNo { t.Run(fmt.Sprintf("allow zap fields %s", name), func(t *testing.T) { for _, data := range testDataInfo { t.Run(data.msg, func(t *testing.T) { for name, useSlog := range map[string]bool{"with-logr": false, "with-slog": true} { t.Run(name, func(t *testing.T) { test(t, logNumeric, panicMessages, allowZapFields, useSlog, data) }) } }) } }) } }) } }) } } // TestEnabled tests whether log messages are enabled. func TestEnabled(t *testing.T) { for i := 0; i < 11; i++ { t.Run(fmt.Sprintf("logger level %d", i), func(t *testing.T) { var sampleInfoLogger = zapr.NewLogger(newZapLogger(zapcore.Level(0-i), nil)) // Very high levels are theoretically possible and need special // handling because zap uses int8. for j := 0; j <= 128; j++ { shouldBeEnabled := i >= j t.Run(fmt.Sprintf("message level %d", j), func(t *testing.T) { isEnabled := sampleInfoLogger.V(j).Enabled() if !isEnabled && shouldBeEnabled { t.Errorf("V(%d).Info should be enabled", j) } else if isEnabled && !shouldBeEnabled { t.Errorf("V(%d).Info should not be enabled", j) } log := sampleInfoLogger for k := 0; k < j; k++ { log = log.V(1) } isEnabled = log.Enabled() if !isEnabled && shouldBeEnabled { t.Errorf("repeated V(1).Info should be enabled") } else if isEnabled && !shouldBeEnabled { t.Errorf("repeated V(1).Info should not be enabled") } }) } }) } } // TestV tests support for numeric log level logging. func TestLogNumeric(t *testing.T) { for logNumeric, formatted := range map[string]string{"": "", "v": `"v":%d,`, "verbose": `"verbose":%d,`} { t.Run(fmt.Sprintf("numeric verbosity field %q", logNumeric), func(t *testing.T) { for i := 0; i < 2; i++ { t.Run(fmt.Sprintf("message verbosity %d", i), func(t *testing.T) { var buffer bytes.Buffer writer := bufio.NewWriter(&buffer) var sampleInfoLogger logr.Logger zl := newZapLogger(zapcore.Level(-100), zapcore.AddSync(writer)) if logNumeric != "" { sampleInfoLogger = zapr.NewLoggerWithOptions(zl, zapr.LogInfoLevel(logNumeric)) } else { sampleInfoLogger = zapr.NewLogger(zl) } sampleInfoLogger.V(i).Info("test", "ns", "default", "podnum", 2, "time", time.Microsecond) if err := writer.Flush(); err != nil { t.Fatalf("unexpected error from Flush: %v", err) } logStr := buffer.String() var v, lineNo int expectedFormat := `{"ts":123.456789,"caller":"zapr/zapr_test.go:%d","msg":"test",` + formatted + `"ns":"default","podnum":2,"time":"1µs"} ` expected := "" if logNumeric != "" { n, err := fmt.Sscanf(logStr, expectedFormat, &lineNo, &v) if n != 2 || err != nil { t.Errorf("log format error: %d elements, error %s:\n%s", n, err, logStr) } if v != i { t.Errorf("V(%d).Info...) returned v=%d. expected v=%d", i, v, i) } expected = fmt.Sprintf(expectedFormat, lineNo, v) } else { n, err := fmt.Sscanf(logStr, expectedFormat, &lineNo) if n != 1 || err != nil { t.Errorf("log format error: %d elements, error %s:\n%s", n, err, logStr) } expected = fmt.Sprintf(expectedFormat, lineNo) } require.JSONEq(t, logStr, expected) }) } }) } } // TestError tests Logger.Error. func TestError(t *testing.T) { for _, logError := range []string{"err", "error"} { t.Run(fmt.Sprintf("error field name %s", logError), func(t *testing.T) { var buffer bytes.Buffer writer := bufio.NewWriter(&buffer) opts := []zapr.Option{zapr.LogInfoLevel("v")} if logError != "error" { opts = append(opts, zapr.ErrorKey(logError)) } // Errors always get logged, regardless of log levels. var sampleInfoLogger = zapr.NewLoggerWithOptions(newZapLogger(zapcore.Level(-5), zapcore.AddSync(writer)), opts...) sampleInfoLogger.V(10).Error(fmt.Errorf("invalid namespace:%s", "default"), "wrong namespace", "ns", "default", "podnum", 2, "time", time.Microsecond) if err := writer.Flush(); err != nil { t.Fatalf("unexpected error from Flush: %v", err) } logStr := buffer.String() var ts float64 var lineNo int expectedFormat := `{"ts":%f,"caller":"zapr/zapr_test.go:%d","msg":"wrong namespace","ns":"default","podnum":2,"time":"1µs","` + logError + `":"invalid namespace:default"}` n, err := fmt.Sscanf(logStr, expectedFormat, &ts, &lineNo) if n != 2 || err != nil { t.Errorf("log format error: %d elements, error %s:\n%s", n, err, logStr) } expected := fmt.Sprintf(expectedFormat, ts, lineNo) require.JSONEq(t, expected, logStr) }) } } zapr-1.3.0/zapr_wrapper_test.go000066400000000000000000000022001452114212200165550ustar00rootroot00000000000000/* Copyright 2021 The logr Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package zapr_test import ( "github.com/go-logr/logr" ) func myInfo(logger logr.Logger, msg string, keysAndValues ...interface{}) { myInfo2(logger, msg, keysAndValues...) } func myInfo2(logger logr.Logger, msg string, keysAndValues ...interface{}) { logger.WithCallDepth(2).Info(msg, keysAndValues...) } func myInfoInc(logger logr.Logger, msg string, keysAndValues ...interface{}) { myInfoInc2(logger.WithCallDepth(1), msg, keysAndValues...) } func myInfoInc2(logger logr.Logger, msg string, keysAndValues ...interface{}) { logger.WithCallDepth(1).Info(msg, keysAndValues...) }