github.com/XiaoMi/Gaea@v1.2.5/log/logger.go (about)

     1  // Copyright 2019 The Gaea 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 log
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/XiaoMi/Gaea/log/xlog"
    21  )
    22  
    23  var logger Logger
    24  
    25  // Logger is the log interface
    26  type Logger interface {
    27  	SetLevel(name, level string) error
    28  
    29  	Debug(format string, a ...interface{}) (err error)
    30  	Trace(format string, a ...interface{}) (err error)
    31  	Notice(format string, a ...interface{}) (err error)
    32  	Warn(format string, a ...interface{}) (err error)
    33  	Fatal(format string, a ...interface{}) (err error)
    34  
    35  	Debugx(logID, format string, a ...interface{}) (err error)
    36  	Tracex(logID, format string, a ...interface{}) (err error)
    37  	Noticex(logID, format string, a ...interface{}) (err error)
    38  	Warnx(logID, format string, a ...interface{}) (err error)
    39  	Fatalx(logID, format string, a ...interface{}) (err error)
    40  
    41  	Close()
    42  }
    43  
    44  func init() {
    45  	cfg := make(map[string]string)
    46  	cfg["level"] = "debug"
    47  	lg, err := xlog.CreateLogManager("console", cfg)
    48  	if err != nil {
    49  		panic(fmt.Errorf("init global logger error: %v", err))
    50  	}
    51  	logger = lg
    52  }
    53  
    54  // SetGlobalLogger set global logger, if global logger already exists, close it and set to new.
    55  func SetGlobalLogger(lg Logger) {
    56  	if logger != nil {
    57  		logger.Close()
    58  	}
    59  	logger = lg
    60  }
    61  
    62  // Debug log debug message.
    63  func Debug(format string, a ...interface{}) (err error) {
    64  	return logger.Debug(format, a...)
    65  }
    66  
    67  // Debugx log debug message with logID.
    68  func Debugx(logID, format string, a ...interface{}) (err error) {
    69  	return logger.Debugx(logID, format, a...)
    70  }
    71  
    72  // Trace log trace message.
    73  func Trace(format string, a ...interface{}) (err error) {
    74  	return logger.Trace(format, a...)
    75  }
    76  
    77  // Tracex log trace message with logID.
    78  func Tracex(logID, format string, a ...interface{}) (err error) {
    79  	return logger.Tracex(logID, format, a...)
    80  }
    81  
    82  // Notice log notice message.
    83  func Notice(format string, a ...interface{}) (err error) {
    84  	return logger.Notice(format, a...)
    85  }
    86  
    87  // Noticex log notice message with logID.
    88  func Noticex(logID, format string, a ...interface{}) (err error) {
    89  	return logger.Noticex(logID, format, a...)
    90  }
    91  
    92  // Warn log warn message.
    93  func Warn(format string, a ...interface{}) (err error) {
    94  	return logger.Warn(format, a...)
    95  }
    96  
    97  // Warnx log warn message with logID.
    98  func Warnx(logID, format string, a ...interface{}) (err error) {
    99  	return logger.Warnx(logID, format, a...)
   100  }
   101  
   102  // Fatal log fatal message.
   103  func Fatal(format string, a ...interface{}) (err error) {
   104  	return logger.Fatal(format, a...)
   105  }
   106  
   107  // Fatalx log fatal message with logID.
   108  func Fatalx(logID, format string, a ...interface{}) (err error) {
   109  	return logger.Fatalx(logID, format, a...)
   110  }
   111  
   112  // Close close the global logger
   113  func Close() {
   114  	logger.Close()
   115  }