github.com/gogf/gf/v2@v2.7.4/os/glog/glog.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package glog implements powerful and easy-to-use leveled logging functionality.
     8  package glog
     9  
    10  import (
    11  	"context"
    12  
    13  	"github.com/gogf/gf/v2/internal/command"
    14  	"github.com/gogf/gf/v2/os/grpool"
    15  	"github.com/gogf/gf/v2/util/gconv"
    16  )
    17  
    18  // ILogger is the API interface for logger.
    19  type ILogger interface {
    20  	Print(ctx context.Context, v ...interface{})
    21  	Printf(ctx context.Context, format string, v ...interface{})
    22  	Debug(ctx context.Context, v ...interface{})
    23  	Debugf(ctx context.Context, format string, v ...interface{})
    24  	Info(ctx context.Context, v ...interface{})
    25  	Infof(ctx context.Context, format string, v ...interface{})
    26  	Notice(ctx context.Context, v ...interface{})
    27  	Noticef(ctx context.Context, format string, v ...interface{})
    28  	Warning(ctx context.Context, v ...interface{})
    29  	Warningf(ctx context.Context, format string, v ...interface{})
    30  	Error(ctx context.Context, v ...interface{})
    31  	Errorf(ctx context.Context, format string, v ...interface{})
    32  	Critical(ctx context.Context, v ...interface{})
    33  	Criticalf(ctx context.Context, format string, v ...interface{})
    34  	Panic(ctx context.Context, v ...interface{})
    35  	Panicf(ctx context.Context, format string, v ...interface{})
    36  	Fatal(ctx context.Context, v ...interface{})
    37  	Fatalf(ctx context.Context, format string, v ...interface{})
    38  }
    39  
    40  const (
    41  	commandEnvKeyForDebug = "gf.glog.debug"
    42  )
    43  
    44  var (
    45  	// Ensure Logger implements ILogger.
    46  	_ ILogger = &Logger{}
    47  
    48  	// Default logger object, for package method usage.
    49  	defaultLogger = New()
    50  
    51  	// Goroutine pool for async logging output.
    52  	// It uses only one asynchronous worker to ensure log sequence.
    53  	asyncPool = grpool.New(1)
    54  
    55  	// defaultDebug enables debug level or not in default,
    56  	// which can be configured using command option or system environment.
    57  	defaultDebug = true
    58  )
    59  
    60  func init() {
    61  	defaultDebug = gconv.Bool(command.GetOptWithEnv(commandEnvKeyForDebug, "true"))
    62  	SetDebug(defaultDebug)
    63  }
    64  
    65  // DefaultLogger returns the default logger.
    66  func DefaultLogger() *Logger {
    67  	return defaultLogger
    68  }
    69  
    70  // SetDefaultLogger sets the default logger for package glog.
    71  // Note that there might be concurrent safety issue if calls this function
    72  // in different goroutines.
    73  func SetDefaultLogger(l *Logger) {
    74  	defaultLogger = l
    75  }