github.com/ethersphere/bee/v2@v2.2.0/pkg/log/example_test.go (about) 1 // Copyright 2022 The Swarm Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package log_test 6 7 import ( 8 "errors" 9 "fmt" 10 "os" 11 12 "github.com/ethersphere/bee/v2/pkg/log" 13 ) 14 15 func Example() { 16 log.ModifyDefaults( 17 log.WithSink(os.Stdout), 18 log.WithVerbosity(log.VerbosityAll), 19 ) 20 21 logger := log.NewLogger("example").Build() 22 logger.Error(nil, "error should work", "k", 1, "k", 2) 23 logger.Warning("warning should work", "address", 12345) 24 logger.Info("info should work", "k", 3, "k", 4) 25 logger.Debug("debug should work", "error", errors.New("failed")) 26 fmt.Println() 27 28 loggerV1 := logger.V(1).Build() 29 loggerV1.Error(nil, "v1 error should work") 30 loggerV1.Warning("v1 warning should work") 31 loggerV1.Info("v1 info should work") 32 loggerV1.Debug("v1 debug should work") 33 fmt.Println() 34 35 _ = log.SetVerbosity(loggerV1, log.VerbosityWarning) 36 loggerV1.Error(nil, "v1 error should work") 37 loggerV1.Warning("v1 warning should work") 38 loggerV1.Info("v1 info shouldn't work") 39 loggerV1.Debug("v1 debug shouldn't work") 40 fmt.Println() 41 42 loggerV1WithName := loggerV1.WithName("example_name").Build() 43 // The verbosity was inherited from loggerV1 so we have to change it. 44 _ = log.SetVerbosity(loggerV1WithName, log.VerbosityAll) 45 loggerV1WithName.Error(nil, "v1 with name error should work") 46 loggerV1WithName.Warning("v1 with name warning should work") 47 loggerV1WithName.Info("v1 with name info should work") 48 loggerV1WithName.Debug("v1 with name debug should work") 49 50 // Output: 51 // "level"="error" "logger"="example" "msg"="error should work" "k"=1 "k"=2 52 // "level"="warning" "logger"="example" "msg"="warning should work" "address"=12345 53 // "level"="info" "logger"="example" "msg"="info should work" "k"=3 "k"=4 54 // "level"="debug" "logger"="example" "msg"="debug should work" "error"="failed" 55 // 56 // "level"="error" "logger"="example" "msg"="v1 error should work" 57 // "level"="warning" "logger"="example" "msg"="v1 warning should work" 58 // "level"="info" "logger"="example" "msg"="v1 info should work" 59 // "level"="debug" "logger"="example" "v"=1 "msg"="v1 debug should work" 60 // 61 // "level"="error" "logger"="example" "msg"="v1 error should work" 62 // "level"="warning" "logger"="example" "msg"="v1 warning should work" 63 // 64 // "level"="error" "logger"="example/example_name" "msg"="v1 with name error should work" 65 // "level"="warning" "logger"="example/example_name" "msg"="v1 with name warning should work" 66 // "level"="info" "logger"="example/example_name" "msg"="v1 with name info should work" 67 // "level"="debug" "logger"="example/example_name" "v"=1 "msg"="v1 with name debug should work" 68 }