github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/internal/logutil/logutil.go (about)

     1  // Copyright 2022 zGraph 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 logutil
    16  
    17  import (
    18  	"fmt"
    19  	"log"
    20  )
    21  
    22  var g Logger = &defaultLogger{}
    23  
    24  type Logger interface {
    25  	Infof(msg string, args ...interface{})
    26  	Errorf(msg string, args ...interface{})
    27  	Fatalf(msg string, args ...interface{})
    28  }
    29  
    30  // SetLogger replaces the default logger
    31  func SetLogger(l Logger) {
    32  	g = l
    33  }
    34  
    35  type defaultLogger struct{}
    36  
    37  func (defaultLogger) Infof(msg string, args ...interface{}) {
    38  	_ = log.Output(2, fmt.Sprintf(msg, args...))
    39  }
    40  
    41  func (defaultLogger) Errorf(msg string, args ...interface{}) {
    42  	_ = log.Output(2, fmt.Sprintf(msg, args...))
    43  }
    44  
    45  func (defaultLogger) Fatalf(msg string, args ...interface{}) {
    46  	_ = log.Output(2, fmt.Sprintf(msg, args...))
    47  }
    48  
    49  func Infof(msg string, args ...interface{}) {
    50  	g.Infof(msg, args...)
    51  }
    52  
    53  func Errorf(msg string, args ...interface{}) {
    54  	g.Errorf(msg, args...)
    55  }
    56  
    57  func Fatalf(msg string, args ...interface{}) {
    58  	g.Fatalf(msg, args...)
    59  }