Documentation
¶
Overview ¶
Package log is a log package used by TKE team.
Index ¶
- Constants
- Variables
- func Cache(msg string, fields ...Field)
- func CacheDebug(msg string, fields ...Field)
- func CacheError(msg string, fields ...Field)
- func CacheWarn(msg string, fields ...Field)
- func CheckIntLevel(level int32) bool
- func Debug(msg string, fields ...Field)
- func DebugContext(ctx context.Context, msg string, fields ...Field)
- func Debugf(format string, v ...interface{})
- func DebugfContext(ctx context.Context, format string, v ...interface{})
- func Debugw(msg string, keysAndValues ...interface{})
- func Error(msg string, fields ...Field)
- func ErrorContext(ctx context.Context, msg string, fields ...Field)
- func Errorf(format string, v ...interface{})
- func ErrorfContext(ctx context.Context, format string, v ...interface{})
- func Errorw(msg string, keysAndValues ...interface{})
- func ExtractRequestID(ctx context.Context) string
- func ExtractSpanID(ctx context.Context) string
- func ExtractTraceID(ctx context.Context) string
- func Fatal(msg string, fields ...Field)
- func Fatalf(format string, v ...interface{})
- func Fatalw(msg string, keysAndValues ...interface{})
- func Flush()
- func GRPC(msg string, fields ...Field)
- func GRPCDebug(msg string, fields ...Field)
- func GRPCError(msg string, fields ...Field)
- func GRPCWarn(msg string, fields ...Field)
- func HTTP(msg string, fields ...Field)
- func HTTPDebug(msg string, fields ...Field)
- func HTTPError(msg string, fields ...Field)
- func HTTPWarn(msg string, fields ...Field)
- func Info(msg string, fields ...Field)
- func InfoContext(ctx context.Context, msg string, fields ...Field)
- func Infof(format string, v ...interface{})
- func InfofContext(ctx context.Context, format string, v ...interface{})
- func Infow(msg string, keysAndValues ...interface{})
- func Init(opts *Options)
- func L(ctx context.Context) *zapLogger
- func MQ(msg string, fields ...Field)
- func MQDebug(msg string, fields ...Field)
- func MQError(msg string, fields ...Field)
- func MQWarn(msg string, fields ...Field)
- func Mongo(msg string, fields ...Field)
- func MongoDebug(msg string, fields ...Field)
- func MongoError(msg string, fields ...Field)
- func MongoWarn(msg string, fields ...Field)
- func New(opts *Options) *zapLogger
- func NewWithRotation(opts *Options) *zap.Logger
- func Panic(msg string, fields ...Field)
- func Panicf(format string, v ...interface{})
- func Panicw(msg string, keysAndValues ...interface{})
- func RPC(msg string, fields ...Field)
- func RPCDebug(msg string, fields ...Field)
- func RPCError(msg string, fields ...Field)
- func RPCWarn(msg string, fields ...Field)
- func Redis(msg string, fields ...Field)
- func RedisDebug(msg string, fields ...Field)
- func RedisError(msg string, fields ...Field)
- func RedisWarn(msg string, fields ...Field)
- func RequestID(ctx context.Context) zap.Field
- func SQL(msg string, fields ...Field)
- func SQLDebug(msg string, fields ...Field)
- func SQLError(msg string, fields ...Field)
- func SQLWarn(msg string, fields ...Field)
- func SpanID(ctx context.Context) zap.Field
- func StdErrLogger() *log.Logger
- func StdInfoLogger() *log.Logger
- func SugaredLogger() *zap.SugaredLogger
- func TraceFields(ctx context.Context) []zap.Field
- func TraceID(ctx context.Context) zap.Field
- func Warn(msg string, fields ...Field)
- func WarnContext(ctx context.Context, msg string, fields ...Field)
- func Warnf(format string, v ...interface{})
- func WarnfContext(ctx context.Context, format string, v ...interface{})
- func Warnw(msg string, keysAndValues ...interface{})
- func WithContext(ctx context.Context) context.Context
- func WithRequestID(ctx context.Context, requestID string) context.Context
- func WithSpanID(ctx context.Context, spanID string) context.Context
- func WithTraceContext(ctx context.Context, traceID, spanID, requestID string) context.Context
- func WithTraceID(ctx context.Context, traceID string) context.Context
- func ZapLogger() *zap.Logger
- type Field
- type InfoLogger
- type Level
- type Logger
- type Options
- type TimeRotationWriter
- type TraceContextKey
Constants ¶
const ( KeyRequestID string = "requestID" KeyUsername string = "username" KeyWatcherName string = "watcher" )
Defines common log fields.
Variables ¶
var ( // DebugLevel logs are typically voluminous, and are usually disabled in // production. DebugLevel = zapcore.DebugLevel // InfoLevel is the default logging priority. InfoLevel = zapcore.InfoLevel // WarnLevel logs are more important than Info, but don't need individual // human review. WarnLevel = zapcore.WarnLevel // ErrorLevel logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-level logs. ErrorLevel = zapcore.ErrorLevel // PanicLevel logs a message, then panics. PanicLevel = zapcore.PanicLevel // FatalLevel logs a message, then calls os.Exit(1). FatalLevel = zapcore.FatalLevel )
var ( Any = zap.Any Array = zap.Array Object = zap.Object Binary = zap.Binary Bool = zap.Bool Bools = zap.Bools ByteString = zap.ByteString ByteStrings = zap.ByteStrings Complex64 = zap.Complex64 Complex64s = zap.Complex64s Complex128 = zap.Complex128 Complex128s = zap.Complex128s Duration = zap.Duration Durations = zap.Durations Err = zap.Error Errors = zap.Errors Float32 = zap.Float32 Float32s = zap.Float32s Float64 = zap.Float64 Float64s = zap.Float64s Int = zap.Int Ints = zap.Ints Int8 = zap.Int8 Int8s = zap.Int8s Int16 = zap.Int16 Int16s = zap.Int16s Int32 = zap.Int32 Int32s = zap.Int32s Int64 = zap.Int64 Int64s = zap.Int64s Namespace = zap.Namespace Reflect = zap.Reflect Stack = zap.Stack String = zap.String Stringer = zap.Stringer Strings = zap.Strings Time = zap.Time Times = zap.Times Uint = zap.Uint Uints = zap.Uints Uint8 = zap.Uint8 Uint8s = zap.Uint8s Uint16 = zap.Uint16 Uint16s = zap.Uint16s Uint32 = zap.Uint32 Uint32s = zap.Uint32s Uint64 = zap.Uint64 Uint64s = zap.Uint64s Uintptr = zap.Uintptr Uintptrs = zap.Uintptrs )
Alias for zap type functions.
Functions ¶
func CacheDebug ¶ added in v0.3.0
CacheDebug 记录缓存操作的调试日志
func CacheError ¶ added in v0.3.0
CacheError 记录缓存操作的错误日志
func CheckIntLevel ¶
CheckIntLevel used for other log wrapper such as klog which return if logging a message at the specified level is enabled.
func DebugContext ¶ added in v0.2.7
DebugContext 记录带追踪信息的 debug 日志
func DebugfContext ¶ added in v0.2.7
DebugfContext 记录带追踪信息的格式化 debug 日志
func Debugw ¶
func Debugw(msg string, keysAndValues ...interface{})
Debugw method output debug level log.
func ErrorContext ¶ added in v0.2.7
ErrorContext 记录带追踪信息的 error 日志
func ErrorfContext ¶ added in v0.2.7
ErrorfContext 记录带追踪信息的格式化 error 日志
func Errorw ¶
func Errorw(msg string, keysAndValues ...interface{})
Errorw method output error level log.
func ExtractRequestID ¶ added in v0.2.7
ExtractRequestID 从 context 中提取 request ID
func ExtractSpanID ¶ added in v0.2.7
ExtractSpanID 从 context 中提取 span ID
func ExtractTraceID ¶ added in v0.2.7
ExtractTraceID 从 context 中提取 trace ID 支持 OpenTelemetry 和自定义 trace ID
func Fatalw ¶
func Fatalw(msg string, keysAndValues ...interface{})
Fatalw method output Fatalw level log.
func Flush ¶
func Flush()
Flush calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.
func InfoContext ¶ added in v0.2.7
InfoContext 记录带追踪信息的 info 日志
func InfofContext ¶ added in v0.2.7
InfofContext 记录带追踪信息的格式化 info 日志
func Infow ¶
func Infow(msg string, keysAndValues ...interface{})
Infow method output info level log.
func MongoDebug ¶ added in v0.4.0
MongoDebug 记录 MongoDB 操作的调试日志
func MongoError ¶ added in v0.4.0
MongoError 记录 MongoDB 操作的错误日志
func New ¶
func New(opts *Options) *zapLogger
New create logger by opts which can custmoized by command arguments.
func NewWithRotation ¶
NewWithRotation 创建支持日志轮转的 logger
func Panicf ¶
func Panicf(format string, v ...interface{})
Panicf method output panic level log and shutdown application.
func Panicw ¶
func Panicw(msg string, keysAndValues ...interface{})
Panicw method output panic level log.
func RedisDebug ¶ added in v0.3.0
RedisDebug 记录 Redis 操作的调试日志
func RedisError ¶ added in v0.3.0
RedisError 记录 Redis 操作的错误日志
func StdErrLogger ¶
StdErrLogger returns logger of standard library which writes to supplied zap logger at error level.
func StdInfoLogger ¶
StdInfoLogger returns logger of standard library which writes to supplied zap logger at info level.
func SugaredLogger ¶
func SugaredLogger() *zap.SugaredLogger
SugaredLogger returns global sugared logger.
func TraceFields ¶ added in v0.2.7
TraceFields 返回所有追踪相关字段
func WarnContext ¶ added in v0.2.7
WarnContext 记录带追踪信息的 warn 日志
func WarnfContext ¶ added in v0.2.7
WarnfContext 记录带追踪信息的格式化 warn 日志
func Warnw ¶
func Warnw(msg string, keysAndValues ...interface{})
Warnw method output warning level log.
func WithContext ¶
WithContext returns a copy of context in which the log value is set.
func WithRequestID ¶ added in v0.2.7
WithRequestID 将 request ID 添加到 context
func WithSpanID ¶ added in v0.2.7
WithSpanID 将 span ID 添加到 context
func WithTraceContext ¶ added in v0.2.7
WithTraceContext 将完整的追踪上下文添加到 context
func WithTraceID ¶ added in v0.2.7
WithTraceID 将 trace ID 添加到 context
Types ¶
type InfoLogger ¶
type InfoLogger interface {
// Info logs a non-error message with the given key/value pairs as context.
//
// The msg argument should be used to add some constant description to
// the log line. The key/value pairs can then be used to add additional
// variable information. The key/value pairs should alternate string
// keys and arbitrary values.
Info(msg string, fields ...Field)
Infof(format string, v ...interface{})
Infow(msg string, keysAndValues ...interface{})
// Enabled tests whether this InfoLogger is enabled. For example,
// commandline flags might be used to set the logging verbosity and disable
// some info logs.
Enabled() bool
}
InfoLogger represents the ability to log non-error messages, at a particular verbosity.
type Logger ¶
type Logger interface {
// All Loggers implement InfoLogger. Calling InfoLogger methods directly on
// a Logger value is equivalent to calling them on a V(0) InfoLogger. For
// example, logger.Info() produces the same result as logger.V(0).Info.
InfoLogger
Debug(msg string, fields ...Field)
Debugf(format string, v ...interface{})
Debugw(msg string, keysAndValues ...interface{})
Warn(msg string, fields ...Field)
Warnf(format string, v ...interface{})
Warnw(msg string, keysAndValues ...interface{})
Error(msg string, fields ...Field)
Errorf(format string, v ...interface{})
Errorw(msg string, keysAndValues ...interface{})
Panic(msg string, fields ...Field)
Panicf(format string, v ...interface{})
Panicw(msg string, keysAndValues ...interface{})
Fatal(msg string, fields ...Field)
Fatalf(format string, v ...interface{})
Fatalw(msg string, keysAndValues ...interface{})
// V returns an InfoLogger value for a specific verbosity level. A higher
// verbosity level means a log message is less important. It's illegal to
// pass a log level less than zero.
V(level Level) InfoLogger
Write(p []byte) (n int, err error)
// WithValues adds some key-value pairs of context to a logger.
// See Info for documentation on how key/value pairs work.
WithValues(keysAndValues ...interface{}) Logger
// WithName adds a new element to the logger's name.
// Successive calls with WithName continue to append
// suffixes to the logger's name. It's strongly recommended
// that name segments contain only letters, digits, and hyphens
// (see the package documentation for more information).
WithName(name string) Logger
// WithContext returns a copy of context in which the log value is set.
WithContext(ctx context.Context) context.Context
// Flush calls the underlying Core's Sync method, flushing any buffered
// log entries. Applications should take care to call Sync before exiting.
Flush()
}
Logger represents the ability to log messages, both errors and not.
func FromContext ¶
FromContext returns the value of the log key on the ctx.
func WithName ¶
WithName adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.
func WithValues ¶
func WithValues(keysAndValues ...interface{}) Logger
WithValues creates a child logger and adds adds Zap fields to it.
type Options ¶
type Options struct {
OutputPaths []string `json:"output-paths" mapstructure:"output-paths"`
ErrorOutputPaths []string `json:"error-output-paths" mapstructure:"error-output-paths"`
Level string `json:"level" mapstructure:"level"`
Format string `json:"format" mapstructure:"format"`
DisableCaller bool `json:"disable-caller" mapstructure:"disable-caller"`
DisableStacktrace bool `json:"disable-stacktrace" mapstructure:"disable-stacktrace"`
EnableColor bool `json:"enable-color" mapstructure:"enable-color"`
Development bool `json:"development" mapstructure:"development"`
Name string `json:"name" mapstructure:"name"`
// 日志轮转配置
MaxSize int `json:"max-size" mapstructure:"max-size"` // 单个日志文件最大大小(MB)
MaxAge int `json:"max-age" mapstructure:"max-age"` // 保留旧日志文件的最大天数
MaxBackups int `json:"max-backups" mapstructure:"max-backups"` // 保留旧日志文件的最大个数
Compress bool `json:"compress" mapstructure:"compress"` // 是否压缩旧日志文件
// 按时间轮转配置
EnableTimeRotation bool `json:"enable-time-rotation" mapstructure:"enable-time-rotation"` // 是否启用按时间轮转
TimeRotationFormat string `json:"time-rotation-format" mapstructure:"time-rotation-format"` // 时间轮转格式,如 "2006-01-02" 表示按天
// 日志分级输出配置
// 为不同日志级别配置独立的输出路径
// 例如: {"all": []string{"/var/log/app.log"}, "error": []string{"/var/log/error.log"}}
LevelOutputPaths map[string][]string `json:"level-output-paths" mapstructure:"level-output-paths"`
// 是否启用分级输出(如果为 true,则使用 LevelOutputPaths;否则使用 OutputPaths)
EnableLevelOutput bool `json:"enable-level-output" mapstructure:"enable-level-output"`
// 分级输出模式:
// "exact" - 只输出精确匹配的日志级别(例如:info 文件只记录 INFO 级别)
// "above" - 输出该级别及以上的日志(例如:info 文件记录 INFO、WARN、ERROR)
// "duplicate" - 支持重复输出,"all" 记录所有日志,其他级别额外记录(推荐)
// 例如:{"all": ["app.log"], "error": ["error.log"]}
// app.log 记录所有日志,error.log 只额外记录 error
LevelOutputMode string `json:"level-output-mode" mapstructure:"level-output-mode"`
}
Options contains configuration items related to log.
func NewOptions ¶
func NewOptions() *Options
NewOptions creates an Options object with default parameters.
type TimeRotationWriter ¶ added in v0.2.7
type TimeRotationWriter struct {
// contains filtered or unexported fields
}
TimeRotationWriter 按时间轮转的日志写入器
func NewTimeRotationWriter ¶ added in v0.2.7
func NewTimeRotationWriter(filename, timeFormat string, maxAge int, compress bool) *TimeRotationWriter
NewTimeRotationWriter 创建一个按时间轮转的写入器
func (*TimeRotationWriter) Close ¶ added in v0.2.7
func (w *TimeRotationWriter) Close() error
Close 关闭当前文件
func (*TimeRotationWriter) Sync ¶ added in v0.2.7
func (w *TimeRotationWriter) Sync() error
Sync 实现 zapcore.WriteSyncer 接口
type TraceContextKey ¶ added in v0.2.7
type TraceContextKey string
TraceContextKey 用于在 context 中存储自定义 trace ID
const ( // TraceIDKey trace ID 的 context key TraceIDKey TraceContextKey = "trace_id" // SpanIDKey span ID 的 context key SpanIDKey TraceContextKey = "span_id" // RequestIDKey request ID 的 context key RequestIDKey TraceContextKey = "request_id" )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package distribution implements a logger which compatible to logrus/std log/prometheus.
|
Package distribution implements a logger which compatible to logrus/std log/prometheus. |
|
advanced
command
Package main 展示日志模块的高级功能组合使用
|
Package main 展示日志模块的高级功能组合使用 |
|
colorful
command
Package main 展示带颜色和方括号的日志输出
|
Package main 展示带颜色和方括号的日志输出 |
|
compare
command
Package main 对比展示带颜色和不带颜色的日志输出
|
Package main 对比展示带颜色和不带颜色的日志输出 |
|
comparison
command
对比三种日志分级输出模式
|
对比三种日志分级输出模式 |
|
context
command
|
|
|
dailyrotation
command
Package main 展示按天轮转日志功能
|
Package main 展示按天轮转日志功能 |
|
duplicatemode
command
演示 duplicate 模式:app.log 记录所有日志,error.log 额外记录错误
|
演示 duplicate 模式:app.log 记录所有日志,error.log 额外记录错误 |
|
exactlevel
command
Package main 展示精确级别输出模式
|
Package main 展示精确级别输出模式 |
|
hourlyrotation
command
Package main 展示按小时轮转日志功能
|
Package main 展示按小时轮转日志功能 |
|
leveloutput
command
Package main 展示日志分级输出功能
|
Package main 展示日志分级输出功能 |
|
microservices
command
Package main 展示微服务间的链路追踪
|
Package main 展示微服务间的链路追踪 |
|
nocolor
command
Package main 展示不带颜色但有方括号的日志输出
|
Package main 展示不带颜色但有方括号的日志输出 |
|
production
command
演示实际生产环境的日志配置:app.log 记录所有,error.log 额外记录错误,按天轮转
|
演示实际生产环境的日志配置:app.log 记录所有,error.log 额外记录错误,按天轮转 |
|
simple
command
|
|
|
tracing
command
Package main 展示链路追踪集成功能
|
Package main 展示链路追踪集成功能 |
|
typed-logs
command
演示类型化日志的使用
|
演示类型化日志的使用 |
|
vlevel
command
|
|
|
Package klog init klog logger.
|
Package klog init klog logger. |
|
Package logrus adds a hook to the logrus logger hooks.
|
Package logrus adds a hook to the logrus logger hooks. |
|
Package otel 提供 OpenTelemetry 集成
|
Package otel 提供 OpenTelemetry 集成 |