...

Source file src/github.com/go-logr/stdr/example/main.go

Documentation: github.com/go-logr/stdr/example

     1  /*
     2  Copyright 2019 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 main
    18  
    19  import (
    20  	stdlog "log"
    21  	"os"
    22  
    23  	"github.com/go-logr/logr"
    24  	"github.com/go-logr/stdr"
    25  )
    26  
    27  type e struct {
    28  	str string
    29  }
    30  
    31  func (e e) Error() string {
    32  	return e.str
    33  }
    34  
    35  func helper(log logr.Logger, msg string) {
    36  	helper2(log, msg)
    37  }
    38  
    39  func helper2(log logr.Logger, msg string) {
    40  	log.WithCallDepth(2).Info(msg)
    41  }
    42  
    43  func main() {
    44  	stdr.SetVerbosity(1)
    45  	log := stdr.NewWithOptions(stdlog.New(os.Stderr, "", stdlog.LstdFlags), stdr.Options{LogCaller: stdr.All})
    46  	log = log.WithName("MyName")
    47  	example(log.WithValues("module", "example"))
    48  }
    49  
    50  // If this were in another package, all it would depend on in logr, not stdr.
    51  func example(log logr.Logger) {
    52  	log.Info("hello", "val1", 1, "val2", map[string]int{"k": 1})
    53  	log.V(1).Info("you should see this")
    54  	log.V(1).V(1).Info("you should NOT see this")
    55  	log.Error(nil, "uh oh", "trouble", true, "reasons", []float64{0.1, 0.11, 3.14})
    56  	log.Error(e{"an error occurred"}, "goodbye", "code", -1)
    57  	helper(log, "thru a helper")
    58  }
    59  

View as plain text