1 /* 2 Copyright 2021 The logr Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package stdr_test 18 19 import ( 20 "errors" 21 "log" 22 "os" 23 24 "github.com/go-logr/stdr" 25 ) 26 27 var errSome = errors.New("some error") 28 29 func newStdLogger(flags int) stdr.StdLogger { 30 return log.New(os.Stdout, "", flags) 31 } 32 33 func ExampleNew() { 34 log := stdr.New(newStdLogger(log.Lshortfile)) 35 log.Info("info message with default options") 36 log.Error(errSome, "error message with default options") 37 log.Info("invalid key", 42, "answer") 38 log.Info("missing value", "answer") 39 // Output: 40 // example_test.go:35: "level"=0 "msg"="info message with default options" 41 // example_test.go:36: "msg"="error message with default options" "error"="some error" 42 // example_test.go:37: "level"=0 "msg"="invalid key" "<non-string-key: 42>"="answer" 43 // example_test.go:38: "level"=0 "msg"="missing value" "answer"="<no-value>" 44 } 45 46 func ExampleNew_withName() { 47 log := stdr.New(newStdLogger(0)) 48 log.WithName("hello").WithName("world").Info("thanks for the fish") 49 // Output: 50 // hello/world: "level"=0 "msg"="thanks for the fish" 51 } 52 53 func ExampleNewWithOptions() { 54 log := stdr.NewWithOptions(newStdLogger(0), stdr.Options{LogCaller: stdr.All}) 55 log.Info("with LogCaller=All") 56 // Output: 57 // "caller"={"file":"example_test.go","line":55} "level"=0 "msg"="with LogCaller=All" 58 } 59