|
| 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 glogr_test |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "flag" |
| 22 | + "os" |
| 23 | + "sync" |
| 24 | + |
| 25 | + "github.com/go-logr/glogr" |
| 26 | +) |
| 27 | + |
| 28 | +var glogInit = sync.Once{} |
| 29 | + |
| 30 | +func initGlog() { |
| 31 | + glogInit.Do(func() { |
| 32 | + _ = flag.Set("v", "1") |
| 33 | + _ = flag.Set("logtostderr", "true") |
| 34 | + flag.Parse() |
| 35 | + }) |
| 36 | + os.Stderr = os.Stdout |
| 37 | +} |
| 38 | + |
| 39 | +var errSome = errors.New("some error") |
| 40 | + |
| 41 | +func ExampleNew() { |
| 42 | + initGlog() |
| 43 | + log := glogr.New() |
| 44 | + log.Info("info message with default options") |
| 45 | + log.Error(errSome, "error message with default options") |
| 46 | + log.Info("invalid key", 42, "answer") |
| 47 | + log.Info("missing value", "answer") |
| 48 | + // I1015 08:59:26.952954 2385059 example_test.go:44] "level"=0 "msg"="info message with default options" |
| 49 | + // E1015 08:59:26.953000 2385059 example_test.go:45] "msg"="error message with default options" "error"="some error" |
| 50 | + // I1015 08:59:26.953005 2385059 example_test.go:46] "level"=0 "msg"="invalid key" "<non-string-key: 42>"="answer" |
| 51 | + // I1015 08:59:26.953013 2385059 example_test.go:47] "level"=0 "msg"="missing value" "answer"="<no-value>" |
| 52 | +} |
| 53 | + |
| 54 | +func ExampleNew_withName() { |
| 55 | + initGlog() |
| 56 | + log := glogr.New() |
| 57 | + log.WithName("hello").WithName("world").Info("thanks for the fish") |
| 58 | + // I1015 08:59:26.953089 2385059 example_test.go:54] hello/world: "level"=0 "msg"="thanks for the fish" |
| 59 | +} |
| 60 | + |
| 61 | +func ExampleNewWithOptions() { |
| 62 | + initGlog() |
| 63 | + log := glogr.NewWithOptions(glogr.Options{LogCaller: glogr.Error}) |
| 64 | + log.Info("Info log with LogCaller=Error") |
| 65 | + log.Error(nil, "Error log with LogCaller=All") |
| 66 | + // I1015 09:10:36.754010 2392854 example_test.go:64] "level"=0 "msg"="Info log with LogCaller=Error" |
| 67 | + // E1015 09:10:36.754057 2392854 example_test.go:65] "caller"={"file":"example_test.go","line":65} "msg"="Error log with LogCaller=All" "error"=null |
| 68 | +} |
0 commit comments