Documentation ¶
Overview ¶
基于 klog 和 zap 封装的 Logger 及其接口
Index ¶
- func CtxDebugf(ctx context.Context, format string, v ...any)
- func CtxErrorf(ctx context.Context, format string, v ...any)
- func CtxFatalf(ctx context.Context, format string, v ...any)
- func CtxInfof(ctx context.Context, format string, v ...any)
- func CtxNoticef(ctx context.Context, format string, v ...any)
- func CtxTracef(ctx context.Context, format string, v ...any)
- func CtxWarnf(ctx context.Context, format string, v ...any)
- func Debug(v ...any)
- func Debugf(format string, v ...any)
- func Error(v ...any)
- func Errorf(format string, v ...any)
- func Fatal(v ...any)
- func Fatalf(format string, v ...any)
- func Info(v ...any)
- func Infof(format string, v ...any)
- func InitLog() *zap.Logger
- func Notice(v ...any)
- func Noticef(format string, v ...any)
- func SetLevel(lv klog.Level)
- func SetOutput(w io.Writer)
- func Trace(v ...any)
- func Tracef(format string, v ...any)
- func Warn(v ...any)
- func Warnf(format string, v ...any)
- type SugaredLogger
- func (s *SugaredLogger) DPanic(args ...interface{})
- func (s *SugaredLogger) DPanicf(template string, args ...interface{})
- func (s *SugaredLogger) DPanicw(msg string, keysAndValues ...interface{})
- func (s *SugaredLogger) Debug(args ...interface{})
- func (s *SugaredLogger) Debugf(template string, args ...interface{})
- func (s *SugaredLogger) Debugw(msg string, keysAndValues ...interface{})
- func (s *SugaredLogger) Error(args ...interface{})
- func (s *SugaredLogger) Errorf(template string, args ...interface{})
- func (s *SugaredLogger) Errorw(msg string, keysAndValues ...interface{})
- func (s *SugaredLogger) Fatal(args ...interface{})
- func (s *SugaredLogger) Fatalf(template string, args ...interface{})
- func (s *SugaredLogger) Fatalw(msg string, keysAndValues ...interface{})
- func (s *SugaredLogger) Info(args ...interface{})
- func (s *SugaredLogger) Infof(template string, args ...interface{})
- func (s *SugaredLogger) Infow(msg string, keysAndValues ...interface{})
- func (s *SugaredLogger) Named(name string) *SugaredLogger
- func (s *SugaredLogger) Notice(args ...interface{})
- func (s *SugaredLogger) Noticef(template string, args ...interface{})
- func (s *SugaredLogger) Panic(args ...interface{})
- func (s *SugaredLogger) Panicf(template string, args ...interface{})
- func (s *SugaredLogger) Panicw(msg string, keysAndValues ...interface{})
- func (s *SugaredLogger) Sync() error
- func (s *SugaredLogger) Trace(args ...interface{})
- func (s *SugaredLogger) Tracef(template string, args ...interface{})
- func (s *SugaredLogger) Warn(args ...interface{})
- func (s *SugaredLogger) Warnf(template string, args ...interface{})
- func (s *SugaredLogger) Warnw(msg string, keysAndValues ...interface{})
- func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger
- type ZapLogger
- func (ll *ZapLogger) CtxDebugf(ctx context.Context, format string, v ...any)
- func (ll *ZapLogger) CtxErrorf(ctx context.Context, format string, v ...any)
- func (ll *ZapLogger) CtxFatalf(ctx context.Context, format string, v ...any)
- func (ll *ZapLogger) CtxInfof(ctx context.Context, format string, v ...any)
- func (ll *ZapLogger) CtxNoticef(ctx context.Context, format string, v ...any)
- func (ll *ZapLogger) CtxTracef(ctx context.Context, format string, v ...any)
- func (ll *ZapLogger) CtxWarnf(ctx context.Context, format string, v ...any)
- func (ll *ZapLogger) SetLevel(lv klog.Level)
- func (ll *ZapLogger) SetOutput(w io.Writer)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CtxNoticef ¶
CtxNoticef calls the default logger's CtxNoticef method.
func Fatal ¶
func Fatal(v ...any)
Fatal calls the default logger's Fatal method and then os.Exit(1).
func SetLevel ¶
SetLevel sets the level of logs below which logs will not be output. The default log level is LevelTrace. Note that this method is not concurrent-safe.
Types ¶
type SugaredLogger ¶
A SugaredLogger wraps the Base Logger functionality in a slower, but less verbose, API. Any Logger can be converted to a SugaredLogger with its Sugar method.
Unlike the Logger, the SugaredLogger doesn't insist on structured logging. For each log level, it exposes three methods: one for loosely-typed structured logging, one for println-style formatting, and one for printf-style formatting. For example, SugaredLoggers can produce InfoLevel output with Infow ("info with" structured context), Info, or Infof.
func (*SugaredLogger) DPanic ¶
func (s *SugaredLogger) DPanic(args ...interface{})
DPanic uses fmt.Sprint to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)
func (*SugaredLogger) DPanicf ¶
func (s *SugaredLogger) DPanicf(template string, args ...interface{})
DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)
func (*SugaredLogger) DPanicw ¶
func (s *SugaredLogger) DPanicw(msg string, keysAndValues ...interface{})
DPanicw logs a message with some additional context. In development, the logger then panics. (See DPanicLevel for details.) The variadic key-value pairs are treated as they are in With.
func (*SugaredLogger) Debug ¶
func (s *SugaredLogger) Debug(args ...interface{})
Debug uses fmt.Sprint to construct and log a message.
func (*SugaredLogger) Debugf ¶
func (s *SugaredLogger) Debugf(template string, args ...interface{})
Debugf uses fmt.Sprintf to log a templated message.
func (*SugaredLogger) Debugw ¶
func (s *SugaredLogger) Debugw(msg string, keysAndValues ...interface{})
Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
When debug-level logging is disabled, this is much faster than
s.With(keysAndValues).Debug(msg)
func (*SugaredLogger) Error ¶
func (s *SugaredLogger) Error(args ...interface{})
Error uses fmt.Sprint to construct and log a message.
func (*SugaredLogger) Errorf ¶
func (s *SugaredLogger) Errorf(template string, args ...interface{})
Errorf uses fmt.Sprintf to log a templated message.
func (*SugaredLogger) Errorw ¶
func (s *SugaredLogger) Errorw(msg string, keysAndValues ...interface{})
Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*SugaredLogger) Fatal ¶
func (s *SugaredLogger) Fatal(args ...interface{})
Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.
func (*SugaredLogger) Fatalf ¶
func (s *SugaredLogger) Fatalf(template string, args ...interface{})
Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func (*SugaredLogger) Fatalw ¶
func (s *SugaredLogger) Fatalw(msg string, keysAndValues ...interface{})
Fatalw logs a message with some additional context, then calls os.Exit. The variadic key-value pairs are treated as they are in With.
func (*SugaredLogger) Info ¶
func (s *SugaredLogger) Info(args ...interface{})
Info uses fmt.Sprint to construct and log a message.
func (*SugaredLogger) Infof ¶
func (s *SugaredLogger) Infof(template string, args ...interface{})
Infof uses fmt.Sprintf to log a templated message.
func (*SugaredLogger) Infow ¶
func (s *SugaredLogger) Infow(msg string, keysAndValues ...interface{})
Infow logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*SugaredLogger) Named ¶
func (s *SugaredLogger) Named(name string) *SugaredLogger
func (*SugaredLogger) Notice ¶
func (s *SugaredLogger) Notice(args ...interface{})
func (*SugaredLogger) Noticef ¶
func (s *SugaredLogger) Noticef(template string, args ...interface{})
func (*SugaredLogger) Panic ¶
func (s *SugaredLogger) Panic(args ...interface{})
Panic uses fmt.Sprint to construct and log a message, then panics.
func (*SugaredLogger) Panicf ¶
func (s *SugaredLogger) Panicf(template string, args ...interface{})
Panicf uses fmt.Sprintf to log a templated message, then panics.
func (*SugaredLogger) Panicw ¶
func (s *SugaredLogger) Panicw(msg string, keysAndValues ...interface{})
Panicw logs a message with some additional context, then panics. The variadic key-value pairs are treated as they are in With.
func (*SugaredLogger) Sync ¶
func (s *SugaredLogger) Sync() error
Sync flushes any buffered log entries.
func (*SugaredLogger) Trace ¶
func (s *SugaredLogger) Trace(args ...interface{})
func (*SugaredLogger) Tracef ¶
func (s *SugaredLogger) Tracef(template string, args ...interface{})
func (*SugaredLogger) Warn ¶
func (s *SugaredLogger) Warn(args ...interface{})
Warn uses fmt.Sprint to construct and log a message.
func (*SugaredLogger) Warnf ¶
func (s *SugaredLogger) Warnf(template string, args ...interface{})
Warnf uses fmt.Sprintf to log a templated message.
func (*SugaredLogger) Warnw ¶
func (s *SugaredLogger) Warnw(msg string, keysAndValues ...interface{})
Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are in With.
func (*SugaredLogger) With ¶
func (s *SugaredLogger) With(args ...interface{}) *SugaredLogger
With adds a variadic number of fields to the logging context. It accepts a mix of strongly-typed Field objects and loosely-typed key-value pairs. When processing pairs, the first element of the pair is used as the field key and the second as the field value.
For example,
sugaredLogger.With( "hello", "world", "failure", errors.New("oh no"), Stack(), "count", 42, "user", User{Name: "alice"}, )
is the equivalent of
unsugared.With( String("hello", "world"), String("failure", "oh no"), Stack(), Int("count", 42), Object("user", User{Name: "alice"}), )
Note that the keys in key-value pairs should be strings. In development, passing a non-string key panics. In production, the logger is more forgiving: a separate error is logged, but the key-value pair is skipped and execution continues. Passing an orphaned key triggers similar behavior: panics in development and errors in production.