mirror of
https://github.com/bolucat/Archive.git
synced 2026-04-22 16:07:49 +08:00
189 lines
4.9 KiB
Go
189 lines
4.9 KiB
Go
package log
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
// std is the name of the standard logger in stdlib `log`
|
|
std = New()
|
|
)
|
|
|
|
func StandardLogger() *Logger {
|
|
return std
|
|
}
|
|
|
|
// SetOutput sets the standard logger output.
|
|
func SetOutput(out io.Writer) {
|
|
std.SetOutput(out)
|
|
}
|
|
|
|
// SetFormatter sets the standard logger formatter.
|
|
func SetFormatter(formatter Formatter) {
|
|
std.SetFormatter(formatter)
|
|
}
|
|
|
|
// SetCallback sets the standard logger callback function.
|
|
func SetCallback(callback Callback) {
|
|
std.SetCallback(callback)
|
|
}
|
|
|
|
// SetReportCaller sets whether the standard logger will include the calling
|
|
// method as a field.
|
|
func SetReportCaller(include bool) {
|
|
std.SetReportCaller(include)
|
|
}
|
|
|
|
// SetLevel sets the standard logger level.
|
|
func SetLevel(level string) (ok bool) {
|
|
level = strings.ToUpper(level)
|
|
switch level {
|
|
case "FATAL":
|
|
std.SetLevel(FatalLevel)
|
|
return true
|
|
case "ERROR":
|
|
std.SetLevel(ErrorLevel)
|
|
return true
|
|
case "WARN", "WARNING":
|
|
std.SetLevel(WarnLevel)
|
|
return true
|
|
case "INFO":
|
|
std.SetLevel(InfoLevel)
|
|
return true
|
|
case "DEBUG":
|
|
std.SetLevel(DebugLevel)
|
|
return true
|
|
case "TRACE":
|
|
std.SetLevel(TraceLevel)
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
// GetLevel returns the standard logger level.
|
|
func GetLevel() Level {
|
|
return std.GetLevel()
|
|
}
|
|
|
|
// IsLevelEnabled checks if the log level of the standard logger is greater than the level param
|
|
func IsLevelEnabled(level Level) bool {
|
|
return std.IsLevelEnabled(level)
|
|
}
|
|
|
|
// WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.
|
|
func WithError(err error) *Entry {
|
|
return std.WithField(ErrorKey, err)
|
|
}
|
|
|
|
// WithContext creates an entry from the standard logger and adds a context to it.
|
|
func WithContext(ctx context.Context) *Entry {
|
|
return std.WithContext(ctx)
|
|
}
|
|
|
|
// WithField creates an entry from the standard logger and adds a field to
|
|
// it. If you want multiple fields, use `WithFields`.
|
|
//
|
|
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
|
// or Panic on the Entry it returns.
|
|
func WithField(key string, value interface{}) *Entry {
|
|
return std.WithField(key, value)
|
|
}
|
|
|
|
// WithFields creates an entry from the standard logger and adds multiple
|
|
// fields to it. This is simply a helper for `WithField`, invoking it
|
|
// once for each field.
|
|
//
|
|
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
|
// or Panic on the Entry it returns.
|
|
func WithFields(fields Fields) *Entry {
|
|
return std.WithFields(fields)
|
|
}
|
|
|
|
// WithTime creates an entry from the standard logger and overrides the time of
|
|
// logs generated with it.
|
|
//
|
|
// Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal
|
|
// or Panic on the Entry it returns.
|
|
func WithTime(t time.Time) *Entry {
|
|
return std.WithTime(t)
|
|
}
|
|
|
|
// Tracef logs a message at level Trace on the standard logger.
|
|
func Tracef(format string, args ...interface{}) {
|
|
std.Tracef(format, args...)
|
|
}
|
|
|
|
// Debugf logs a message at level Debug on the standard logger.
|
|
func Debugf(format string, args ...interface{}) {
|
|
std.Debugf(format, args...)
|
|
}
|
|
|
|
// Printf logs a message at level Info on the standard logger.
|
|
func Printf(format string, args ...interface{}) {
|
|
std.Printf(format, args...)
|
|
}
|
|
|
|
// Infof logs a message at level Info on the standard logger.
|
|
func Infof(format string, args ...interface{}) {
|
|
std.Infof(format, args...)
|
|
}
|
|
|
|
// Warnf logs a message at level Warn on the standard logger.
|
|
func Warnf(format string, args ...interface{}) {
|
|
std.Warnf(format, args...)
|
|
}
|
|
|
|
// Warningf logs a message at level Warn on the standard logger.
|
|
func Warningf(format string, args ...interface{}) {
|
|
std.Warningf(format, args...)
|
|
}
|
|
|
|
// Errorf logs a message at level Error on the standard logger.
|
|
func Errorf(format string, args ...interface{}) {
|
|
std.Errorf(format, args...)
|
|
}
|
|
|
|
// Panicf logs a message at level Panic on the standard logger.
|
|
func Panicf(format string, args ...interface{}) {
|
|
std.Panicf(format, args...)
|
|
}
|
|
|
|
// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
|
|
func Fatalf(format string, args ...interface{}) {
|
|
std.Fatalf(format, args...)
|
|
}
|
|
|
|
// Print logs a message at level Info on the standard logger.
|
|
func Print(args ...interface{}) {
|
|
std.Print(args...)
|
|
}
|
|
|
|
// Panic logs a message at level Panic on the standard logger.
|
|
func Panic(args ...interface{}) {
|
|
std.Panic(args...)
|
|
}
|
|
|
|
// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
|
|
func Fatal(args ...interface{}) {
|
|
std.Fatal(args...)
|
|
}
|
|
|
|
// Println logs a message at level Info on the standard logger.
|
|
func Println(args ...interface{}) {
|
|
std.Println(args...)
|
|
}
|
|
|
|
// Panicln logs a message at level Panic on the standard logger.
|
|
func Panicln(args ...interface{}) {
|
|
std.Panicln(args...)
|
|
}
|
|
|
|
// Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
|
|
func Fatalln(args ...interface{}) {
|
|
std.Fatalln(args...)
|
|
}
|