github.com/gogf/gf/v2@v2.7.4/os/glog/glog_logger_api.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
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"os"
    13  )
    14  
    15  // Print prints `v` with newline using fmt.Sprintln.
    16  // The parameter `v` can be multiple variables.
    17  func (l *Logger) Print(ctx context.Context, v ...interface{}) {
    18  	l.printStd(ctx, LEVEL_NONE, v...)
    19  }
    20  
    21  // Printf prints `v` with format `format` using fmt.Sprintf.
    22  // The parameter `v` can be multiple variables.
    23  func (l *Logger) Printf(ctx context.Context, format string, v ...interface{}) {
    24  	l.printStd(ctx, LEVEL_NONE, l.format(format, v...))
    25  }
    26  
    27  // Fatal prints the logging content with [FATA] header and newline, then exit the current process.
    28  func (l *Logger) Fatal(ctx context.Context, v ...interface{}) {
    29  	l.printErr(ctx, LEVEL_FATA, v...)
    30  	os.Exit(1)
    31  }
    32  
    33  // Fatalf prints the logging content with [FATA] header, custom format and newline, then exit the current process.
    34  func (l *Logger) Fatalf(ctx context.Context, format string, v ...interface{}) {
    35  	l.printErr(ctx, LEVEL_FATA, l.format(format, v...))
    36  	os.Exit(1)
    37  }
    38  
    39  // Panic prints the logging content with [PANI] header and newline, then panics.
    40  func (l *Logger) Panic(ctx context.Context, v ...interface{}) {
    41  	l.printErr(ctx, LEVEL_PANI, v...)
    42  	panic(fmt.Sprint(v...))
    43  }
    44  
    45  // Panicf prints the logging content with [PANI] header, custom format and newline, then panics.
    46  func (l *Logger) Panicf(ctx context.Context, format string, v ...interface{}) {
    47  	l.printErr(ctx, LEVEL_PANI, l.format(format, v...))
    48  	panic(l.format(format, v...))
    49  }
    50  
    51  // Info prints the logging content with [INFO] header and newline.
    52  func (l *Logger) Info(ctx context.Context, v ...interface{}) {
    53  	if l.checkLevel(LEVEL_INFO) {
    54  		l.printStd(ctx, LEVEL_INFO, v...)
    55  	}
    56  }
    57  
    58  // Infof prints the logging content with [INFO] header, custom format and newline.
    59  func (l *Logger) Infof(ctx context.Context, format string, v ...interface{}) {
    60  	if l.checkLevel(LEVEL_INFO) {
    61  		l.printStd(ctx, LEVEL_INFO, l.format(format, v...))
    62  	}
    63  }
    64  
    65  // Debug prints the logging content with [DEBU] header and newline.
    66  func (l *Logger) Debug(ctx context.Context, v ...interface{}) {
    67  	if l.checkLevel(LEVEL_DEBU) {
    68  		l.printStd(ctx, LEVEL_DEBU, v...)
    69  	}
    70  }
    71  
    72  // Debugf prints the logging content with [DEBU] header, custom format and newline.
    73  func (l *Logger) Debugf(ctx context.Context, format string, v ...interface{}) {
    74  	if l.checkLevel(LEVEL_DEBU) {
    75  		l.printStd(ctx, LEVEL_DEBU, l.format(format, v...))
    76  	}
    77  }
    78  
    79  // Notice prints the logging content with [NOTI] header and newline.
    80  // It also prints caller stack info if stack feature is enabled.
    81  func (l *Logger) Notice(ctx context.Context, v ...interface{}) {
    82  	if l.checkLevel(LEVEL_NOTI) {
    83  		l.printStd(ctx, LEVEL_NOTI, v...)
    84  	}
    85  }
    86  
    87  // Noticef prints the logging content with [NOTI] header, custom format and newline.
    88  // It also prints caller stack info if stack feature is enabled.
    89  func (l *Logger) Noticef(ctx context.Context, format string, v ...interface{}) {
    90  	if l.checkLevel(LEVEL_NOTI) {
    91  		l.printStd(ctx, LEVEL_NOTI, l.format(format, v...))
    92  	}
    93  }
    94  
    95  // Warning prints the logging content with [WARN] header and newline.
    96  // It also prints caller stack info if stack feature is enabled.
    97  func (l *Logger) Warning(ctx context.Context, v ...interface{}) {
    98  	if l.checkLevel(LEVEL_WARN) {
    99  		l.printStd(ctx, LEVEL_WARN, v...)
   100  	}
   101  }
   102  
   103  // Warningf prints the logging content with [WARN] header, custom format and newline.
   104  // It also prints caller stack info if stack feature is enabled.
   105  func (l *Logger) Warningf(ctx context.Context, format string, v ...interface{}) {
   106  	if l.checkLevel(LEVEL_WARN) {
   107  		l.printStd(ctx, LEVEL_WARN, l.format(format, v...))
   108  	}
   109  }
   110  
   111  // Error prints the logging content with [ERRO] header and newline.
   112  // It also prints caller stack info if stack feature is enabled.
   113  func (l *Logger) Error(ctx context.Context, v ...interface{}) {
   114  	if l.checkLevel(LEVEL_ERRO) {
   115  		l.printErr(ctx, LEVEL_ERRO, v...)
   116  	}
   117  }
   118  
   119  // Errorf prints the logging content with [ERRO] header, custom format and newline.
   120  // It also prints caller stack info if stack feature is enabled.
   121  func (l *Logger) Errorf(ctx context.Context, format string, v ...interface{}) {
   122  	if l.checkLevel(LEVEL_ERRO) {
   123  		l.printErr(ctx, LEVEL_ERRO, l.format(format, v...))
   124  	}
   125  }
   126  
   127  // Critical prints the logging content with [CRIT] header and newline.
   128  // It also prints caller stack info if stack feature is enabled.
   129  func (l *Logger) Critical(ctx context.Context, v ...interface{}) {
   130  	if l.checkLevel(LEVEL_CRIT) {
   131  		l.printErr(ctx, LEVEL_CRIT, v...)
   132  	}
   133  }
   134  
   135  // Criticalf prints the logging content with [CRIT] header, custom format and newline.
   136  // It also prints caller stack info if stack feature is enabled.
   137  func (l *Logger) Criticalf(ctx context.Context, format string, v ...interface{}) {
   138  	if l.checkLevel(LEVEL_CRIT) {
   139  		l.printErr(ctx, LEVEL_CRIT, l.format(format, v...))
   140  	}
   141  }
   142  
   143  // checkLevel checks whether the given `level` could be output.
   144  func (l *Logger) checkLevel(level int) bool {
   145  	// nil logger, print nothing
   146  	if l == nil {
   147  		return false
   148  	}
   149  	return l.config.Level&level > 0
   150  }