github.com/cayleygraph/cayley@v0.7.7/clog/clog.go (about)

     1  // Copyright 2016 The Cayley Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package clog provides a logging interface for cayley packages.
    16  package clog
    17  
    18  import "log"
    19  
    20  // Logger is the clog logging interface.
    21  type Logger interface {
    22  	Infof(format string, args ...interface{})
    23  	Warningf(format string, args ...interface{})
    24  	Errorf(format string, args ...interface{})
    25  	Fatalf(format string, args ...interface{})
    26  	V(int) bool
    27  	SetV(level int)
    28  }
    29  
    30  var logger Logger = &stdlog{
    31  	verbosity: 0,
    32  }
    33  
    34  // SetLogger set the clog logging implementation.
    35  func SetLogger(l Logger) { logger = l }
    36  
    37  var verbosity int
    38  
    39  // V returns whether the current clog verbosity is above the specified level.
    40  func V(level int) bool {
    41  	if logger == nil {
    42  		return false
    43  	}
    44  	return logger.V(level)
    45  }
    46  
    47  // SetV sets the clog verbosity level.
    48  func SetV(level int) {
    49  	if logger != nil {
    50  		logger.SetV(level)
    51  	}
    52  }
    53  
    54  // Infof logs information level messages.
    55  func Infof(format string, args ...interface{}) {
    56  	if logger != nil {
    57  		logger.Infof(format, args...)
    58  	}
    59  }
    60  
    61  // Warningf logs warning level messages.
    62  func Warningf(format string, args ...interface{}) {
    63  	if logger != nil {
    64  		logger.Warningf(format, args...)
    65  	}
    66  }
    67  
    68  // Errorf logs error level messages.
    69  func Errorf(format string, args ...interface{}) {
    70  	if logger != nil {
    71  		logger.Errorf(format, args...)
    72  	}
    73  }
    74  
    75  // Fatalf logs fatal messages and terminates the program.
    76  func Fatalf(format string, args ...interface{}) {
    77  	if logger != nil {
    78  		logger.Fatalf(format, args...)
    79  	}
    80  }
    81  
    82  // stdlog wraps the standard library logger.
    83  type stdlog struct {
    84  	verbosity int
    85  }
    86  
    87  func (stdlog) Infof(format string, args ...interface{})    { log.Printf(format, args...) }
    88  func (stdlog) Warningf(format string, args ...interface{}) { log.Printf("WARN: "+format, args...) }
    89  func (stdlog) Errorf(format string, args ...interface{})   { log.Printf("ERROR: "+format, args...) }
    90  func (stdlog) Fatalf(format string, args ...interface{})   { log.Fatalf("FATAL: "+format, args...) }
    91  func (s stdlog) V(level int) bool                          { return s.verbosity >= level }
    92  func (s *stdlog) SetV(level int)                           { s.verbosity = level }