log

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Ldate         = 1 << iota     // the date in the local time zone: 2009/01/23
	Ltime                         // the time in the local time zone: 01:23:23
	Lmicroseconds                 // microsecond resolution: 01:23:23.123123.  assumes Ltime.
	Llongfile                     // full file name and line number: /a/b/c/d.go:23
	Lshortfile                    // final file name element and line number: d.go:23. overrides Llongfile
	LUTC                          // if Ldate or Ltime is set, use UTC rather than the local time zone
	LstdFlags     = Ldate | Ltime // initial values for the standard logger
)

以下是一些常用的日志标志和日志级别的常量。

View Source
const (
	DEBUG = iota // 调试级别,用于记录详细的调试信息。
	INFO         // 信息级别,用于记录一般的信息消息。
	WARN         // 警告级别,用于记录可能的问题或警告消息。
	ERROR        // 错误级别,用于记录错误和异常消息。
	NOLOG        // 无日志级别,表示不记录任何日志消息。
)

Variables

View Source
var (
	LEVEL_NAME map[int]string // 日志级别的名称映射
	LOG_LEVEL  int            // 当前日志级别
	OPEN_STACK bool           // 是否开启堆栈信息记录
)
View Source
var (
	// DefaultTimestamp is a Valuer that returns the current wallclock time,
	// respecting time zones, when bound.
	// DefaultTimestamp 是一个 Valuer,当绑定时返回当前墙钟时间,尊重时区。
	DefaultTimestamp = TimestampFormat(time.Now, time.RFC3339Nano)

	// DefaultTimestampUTC is a Valuer that returns the current time in UTC
	// when bound.
	// DefaultTimestampUTC 是一个 Valuer,在绑定时返回当前 UTC 时间。
	DefaultTimestampUTC = TimestampFormat(
		func() time.Time { return time.Now().UTC() },
		time.RFC3339Nano,
	)

	// DefaultCaller is a Valuer that returns the file and line where the Log
	// method was invoked. It can only be used with log.With.
	// DefaultCaller 是一个 Valuer,返回调用 Log 方法的文件和行号。它只能与 log.With 一起使用。
	DefaultCaller = Caller(3)
)
View Source
var ErrInvalidKey = errors.New("invalid key")

ErrInvalidKey is returned by Marshal functions and Encoder methods if, after dropping invalid runes, a key is empty. ErrInvalidKey 是Marshal函数和Encoder方法的返回值,如果在删除无效的字符后,键为空,则返回。

View Source
var ErrNilKey = errors.New("nil key")

ErrNilKey is returned by Marshal functions and Encoder methods if a key is a nil interface or pointer value. ErrNilKey 是Marshal函数和Encoder方法的返回值,如果键是nil接口或指针值,则返回。

View Source
var ErrUnsupportedKeyType = errors.New("unsupported key type")

ErrUnsupportedKeyType is returned by Encoder methods if a key has an unsupported type. ErrUnsupportedKeyType 是Encoder方法的返回值,如果键具有不支持的类型,则返回。

View Source
var ErrUnsupportedValueType = errors.New("unsupported value type")

ErrUnsupportedValueType is returned by Encoder methods if a value has an unsupported type. ErrUnsupportedValueType 是Encoder方法的返回值,如果值具有不支持的类型,则返回。

Functions

func Debug

func Debug(v ...interface{})

Debug 记录 DEBUG 级别的日志消息,使用 KVLog 方法。

func Debugf

func Debugf(format string, v ...interface{})

Debugf 记录 DEBUG 级别的格式化日志消息。

func Error

func Error(v ...interface{})

Error 记录 ERROR 级别的日志消息,使用 KVLog 方法。

func Errorf

func Errorf(format string, v ...interface{})

Errorf 记录 ERROR 级别的格式化日志消息。

func Info

func Info(v ...interface{})

Info 记录 INFO 级别的日志消息,使用 KVLog 方法。

func Infof

func Infof(format string, v ...interface{})

Infof 记录 INFO 级别的格式化日志消息。

func Log

func Log(v ...interface{})

Log 记录 DEBUG 级别的日志消息,使用 VLog 方法。

func Logf

func Logf(format string, v ...interface{})

Logf 记录 DEBUG 级别的格式化日志消息。

func MarshalKeyvals

func MarshalKeyvals(keyvals ...interface{}) ([]byte, error)

MarshalKeyvals returns the logfmt encoding of keyvals, a variadic sequence of alternating keys and values. MarshalKeyvals 将keyvals的logfmt编码返回,keyvals是一系列交替的键和值。

func NewSyncWriter

func NewSyncWriter(w io.Writer) io.Writer

NewSyncWriter returns a new writer that is safe for concurrent use by multiple goroutines. Writes to the returned writer are passed on to w. If another write is already in progress, the calling goroutine blocks until the writer is available.

If w implements the following interface, so does the returned writer.

interface {
    Fd() uintptr
}

NewSyncWriter 返回一个新的 writer,可安全地由多个 goroutine 并发使用。 写入到返回的 writer 会传递到 w。如果另一个写入操作已经在进行中, 则调用 goroutine 将阻塞,直到 writer 可用。

如果 w 实现以下接口,则返回的 writer 也会实现它:

interface {
    Fd() uintptr
}

func OpenStack

func OpenStack()

OpenStack 开启堆栈信息记录。

func SetFlags

func SetFlags(flag int)

SetFlags 设置默认日志记录器的标志。

func SetLevel

func SetLevel(level int)

SetLevel 设置当前日志级别。

func Warn

func Warn(v ...interface{})

Warn 记录 WARN 级别的日志消息,使用 KVLog 方法。

func Warnf

func Warnf(format string, v ...interface{})

Warnf 记录 WARN 级别的格式化日志消息。

Types

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

An Encoder writes logfmt data to an output stream. Encoder 用于将logfmt数据写入输出流的结构。

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w. NewEncoder 返回一个新的编码器,用于将数据写入w。

func (*Encoder) EncodeKeyval

func (enc *Encoder) EncodeKeyval(key, value interface{}, needQuoted bool) error

EncodeKeyval writes the logfmt encoding of key and value to the stream. A single space is written before the second and subsequent keys in a record. Nothing is written if a non-nil error is returned. EncodeKeyval 将键和值的logfmt编码写入流。在记录中的第二个及后续键之前写入单个空格。 如果返回非nil错误,则不会写入任何内容。

func (*Encoder) EncodeKeyvals

func (enc *Encoder) EncodeKeyvals(keyvals ...interface{}) error

EncodeKeyvals writes the logfmt encoding of keyvals to the stream. Keyvals is a variadic sequence of alternating keys and values. Keys of unsupported type are skipped along with their corresponding value. Values of unsupported type or that cause a MarshalerError are replaced by their error but do not cause EncodeKeyvals to return an error. If a non-nil error is returned some key/value pairs may not have be written. EncodeKeyvals 将keyvals的logfmt编码写入流。Keyvals是一系列交替的键和值。 不支持的键类型及其对应的值将被跳过。不支持的类型或导致MarshalerError的值将被替换为它们的错误值, 但不会导致EncodeKeyvals返回错误。如果返回非nil错误,可能会有一些键/值对未写入。

func (*Encoder) EncodeKeyvalsWithQuoted

func (enc *Encoder) EncodeKeyvalsWithQuoted(keyvals ...interface{}) error

EncodeKeyvalsWithQuoted 与EncodeKeyvals类似,但引用需要引用的值。

func (*Encoder) EndRecord

func (enc *Encoder) EndRecord() error

EndRecord writes a newline character to the stream and resets the encoder to the beginning of a new record.

func (*Encoder) Reset

func (enc *Encoder) Reset()

Reset resets the encoder to the beginning of a new record. EndRecord 向流写入换行字符并重置编码器以开始新记录。

type InfluxdbLogger

type InfluxdbLogger struct {
	// contains filtered or unexported fields
}

InfluxdbLogger 是一个实现了 Logger 接口的类型,用于将日志编码为 InfluxDB Line Protocol 格式。

func (*InfluxdbLogger) KVLog

func (l *InfluxdbLogger) KVLog(keyvals ...interface{}) error

KVLog 将键值对编码为 InfluxDB Line Protocol 格式写入 w。

func (InfluxdbLogger) SetDepth

func (l InfluxdbLogger) SetDepth(depth int) Logger

SetDepth 设置调用深度。 SetDepth 用于设置当前 InfluxdbLogger 实例的调用深度。 调用深度表示在日志记录中标识调用位置的深度。例如,深度为 2 表示在日志记录中将显示调用 `SetDepth` 方法的调用者的位置。 这对于定位日志记录的来源非常有用,可以将其用于标识哪个函数或方法生成了日志记录。 通过设置不同的调用深度,您可以控制从哪个位置生成日志记录,以适应不同的应用程序需求。 注意:深度设置得太浅可能会显示不相关的调用位置,深度设置得太深可能会隐藏有用的信息,因此需要谨慎选择深度值。 返回一个新的 InfluxdbLogger 实例,以确保调用深度的更改不会影响到原始实例。

func (*InfluxdbLogger) SetFlags

func (l *InfluxdbLogger) SetFlags(flag int)

SetFlags 设置标志位。 SetFlags 用于设置当前 InfluxdbLogger 实例的标志属性。 标志属性控制日志记录的格式和输出方式。可以使用位掩码来组合多个标志属性。 可用的标志属性包括:

  • Ldate: 在日志中包含日期,格式为 "2009/01/23"。
  • Ltime: 在日志中包含时间,格式为 "01:23:23"。
  • Lmicroseconds: 在日志中包含微秒级时间戳,格式为 "01:23:23.123123"(需要 Ltime 属性)。
  • Llongfile: 在日志中包含完整文件名和行号,格式为 "/a/b/c/d.go:23"。
  • Lshortfile: 在日志中包含文件名和行号,格式为 "d.go:23"(会覆盖 Llongfile)。
  • LUTC: 如果设置了 Ldate 或 Ltime 标志,将使用UTC时间而不是本地时间。
  • LstdFlags: 默认的标志属性,包括 Ldate 和 Ltime。

设置标志属性后,新的日志记录将按照指定的格式输出。

func (*InfluxdbLogger) VLog

func (l *InfluxdbLogger) VLog(v ...interface{}) error

VLog 将日志按照 InfluxDB Line Protocol 格式写入 w。 VLog 方法用于记录具有可变参数的日志消息。它将一组任意类型的参数进行格式化,并将结果添加到日志记录中。 参数 v 是一个可变参数列表,您可以传递任意数量的参数给它,这些参数将在日志消息中被格式化和记录。 日志消息的格式为:measurement key1=value1 key2=value2 ... msg="formatted message" caller="file:line" timestamp 其中,measurement 是 InfluxDB 中的测量名称,key1、key2、... 是日志消息的键值对,msg 包含格式化后的消息, caller 显示调用日志记录方法的文件和行号,timestamp 显示时间戳。 该方法会将格式化后的日志消息写入到 InfluxDB 日志记录器的输出流中,并返回可能出现的错误。

func (InfluxdbLogger) WithHeader

func (l InfluxdbLogger) WithHeader(keyvals ...interface{}) Logger

WithHeader 返回带有新标签的 Logger。 WithHeader 为当前的 InfluxdbLogger 实例添加自定义标头信息。 该方法接受一系列 keyvals 参数,这些参数是成对出现的键值对,用于自定义标头。 例如:WithHeader("key1", "value1", "key2", "value2")。 每个键值对将被编码成字符串,并附加到已存在的标头信息之后。 返回一个新的 Logger 实例,该实例包含了添加的标头信息,原始 Logger 实例不受影响。

type Logger

type Logger interface {
	// VLog 用于记录日志消息,可以接收可变数量的 keyvals 参数,并返回一个 error 表示记录是否成功。
	VLog(keyvals ...interface{}) error

	// KVLog 类似于 VLog,用于记录日志消息,但与 VLog 不同的是,它不需要明确指定消息级别,而是根据实际情况自动选择。
	KVLog(keyvals ...interface{}) error

	// WithHeader 返回一个新的 Logger,带有指定的头部信息(键值对)。这允许你为日志消息添加额外的元数据。
	WithHeader(keyvals ...interface{}) Logger

	// SetDepth 返回一个新的 Logger,设置日志消息的深度。深度表示从调用 SetDepth 方法的位置开始计算的堆栈深度,
	// 用于确定日志消息的源代码位置。
	SetDepth(depth int) Logger

	// SetFlags 设置日志记录器的标志,以控制日志消息的格式和内容。
	SetFlags(flag int)
}

Logger 是一个日志记录器的接口,定义了日志记录的方法和配置。

func NewInfluxdbLogger

func NewInfluxdbLogger(w io.Writer, measurement string, kv ...string) Logger

NewInfluxdbLogger 返回一个将 keyvals 编码为 InfluxDB Line Protocol 格式并写入 w 的 Logger。 NewInfluxdbLogger 是一个函数,用于创建一个新的 InfluxDB 日志记录器。 它接受一个 io.Writer 接口用于写入日志,一个字符串作为测量名(measurement),以及一个可变长的键值对字符串数组。

func NewOriginLogger

func NewOriginLogger(w io.Writer) Logger

NewOriginLogger 创建一个新的 OriginLogger 实例,它将日志消息写入给定的 io.Writer。

func WithHeader

func WithHeader(keyvals ...interface{}) Logger

WithHeader 返回一个具有指定头部信息的新日志记录器。

type MarshalerError

type MarshalerError struct {
	// Type 表示不支持的数据类型的反射类型信息。
	Type reflect.Type
	// Err 表示底层错误,通常用于描述为什么不支持该数据类型。
	Err error
}

MarshalerError represents an error encountered while marshaling a value. MarshalerError 表示在编组值时遇到的错误。

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

错误实现

type OriginLogger

type OriginLogger struct {
	// contains filtered or unexported fields
}

OriginLogger 结构表示一个基本的日志记录器,它实现了 Logger 接口。

func (*OriginLogger) KVLog

func (this *OriginLogger) KVLog(v ...interface{}) error

KVLog 实现了 Logger 接口的 KVLog 方法,与 VLog 方法类似,用于记录日志消息。

func (OriginLogger) SetDepth

func (this OriginLogger) SetDepth(depth int) Logger

SetDepth 设置日志消息的调用深度,以便正确识别日志消息的来源位置。

func (*OriginLogger) SetFlags

func (this *OriginLogger) SetFlags(flag int)

SetFlags 设置日志库的标志。

func (*OriginLogger) VLog

func (this *OriginLogger) VLog(v ...interface{}) error

VLog 实现了 Logger 接口的 VLog 方法,用于记录日志消息。

func (OriginLogger) WithHeader

func (this OriginLogger) WithHeader(keyvals ...interface{}) Logger

WithHeader 添加一个带有指定头部信息的头部到日志消息。

type SwapLogger

type SwapLogger struct {
	// contains filtered or unexported fields
}

SwapLogger wraps another logger that may be safely replaced while other goroutines use the SwapLogger concurrently. The zero value for a SwapLogger will discard all log events without error.

SwapLogger serves well as a package global logger that can be changed by importers. SwapLogger 包装了另一个 logger,可以在其他 goroutine 使用 SwapLogger 时安全替换。 SwapLogger 的零值将丢弃所有的日志事件而不返回错误。

SwapLogger 适用于作为一个包全局的 logger,可以被导入者更改。

func (*SwapLogger) Log

func (l *SwapLogger) Log(keyvals ...interface{}) error

Log implements the Logger interface by forwarding keyvals to the currently wrapped logger. It does not log anything if the wrapped logger is nil. Log 通过将 keyvals 转发到当前包装的 logger 来实现 Logger 接口。 如果包装的 logger 为 nil,则不记录任何内容。

func (*SwapLogger) Swap

func (l *SwapLogger) Swap(logger Logger)

Swap replaces the currently wrapped logger with logger. Swap may be called concurrently with calls to Log from other goroutines. Swap 用 logger 替换当前包装的 logger。Swap 可以与其他 goroutine 中的 Log 调用并发调用。

type Valuer

type Valuer func() interface{}

A Valuer generates a log value. When passed to With or WithPrefix in a value element (odd indexes), it represents a dynamic value which is re- evaluated with each log event. Valuer 表示生成日志值的函数类型。当作为值元素(奇数索引)传递给 With 或 WithPrefix 时, 它代表一个动态值,每次日志事件都会重新评估。

func Caller

func Caller(depth int) Valuer

Caller returns a Valuer that returns a file and line from a specified depth in the callstack. Users will probably want to use DefaultCaller. Caller 返回一个 Valuer,它返回指定深度的调用堆栈中的文件和行号。用户可能希望使用 DefaultCaller。

func Timestamp

func Timestamp(t func() time.Time) Valuer

Timestamp returns a timestamp Valuer. It invokes the t function to get the time; unless you are doing something tricky, pass time.Now.

Most users will want to use DefaultTimestamp or DefaultTimestampUTC, which are TimestampFormats that use the RFC3339Nano format. Timestamp 返回一个时间戳 Valuer。它调用 t 函数以获取时间;除非您在做一些复杂的事情,否则传递 time.Now。

大多数用户将希望使用 DefaultTimestamp 或 DefaultTimestampUTC,它们是使用 RFC3339Nano 格式的 TimestampFormats。

func TimestampFormat

func TimestampFormat(t func() time.Time, layout string) Valuer

TimestampFormat returns a timestamp Valuer with a custom time format. It invokes the t function to get the time to format; unless you are doing something tricky, pass time.Now. The layout string is passed to Time.Format.

Most users will want to use DefaultTimestamp or DefaultTimestampUTC, which are TimestampFormats that use the RFC3339Nano format. TimestampFormat 返回具有自定义时间格式的时间戳 Valuer。它调用 t 函数以获取要格式化的时间;除非您在做一些复杂的事情,否则传递 time.Now。布局字符串传递给 Time.Format。

大多数用户将希望使用 DefaultTimestamp 或 DefaultTimestampUTC,它们是使用 RFC3339Nano 格式的 TimestampFormats。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL