log

package module
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: Apache-2.0 Imports: 13 Imported by: 4

README

Log

适配 OpenTracing 和 OpenTelemetry 的日志工具.

import "gitee.com/go-wares/log"

func main() {
// 等待完成.
// 主协程退出前, 等待日志和调用链全部上报完成.
defer log.Close()

...
}

1. 配置

本工具包首次加载时, 在项目的工作目录中查找 config/log.yamlconfig/log.yml../config/log.yaml../config/log.yml 文件, 然后解析到 go-wares/log.Config 实例.

2. 日志

日志系统支持下列适配, 同时只能配置其中一项.

  • term - sync 在终端上打印日志.
  • file - async 日志写入到本地文件中.
  • kafka - async 发布日志到 Kafka 中间件.
  • elasticsearch - async 发布日志到 Elasticsearch 中间件.
2.1 简易用法
log.Info("info message")
log.Infof("info message with: %s", "formatter")

// 兼容 OpenTracing / OpenTelemetry.
ctx := log.NewContext()
log.Infofc(ctx, "info message with: %s", "formatter and context")
2.2 自定义字段
log.Field{"key": "value"}.Info("info message with fields")
2.3 调整格式化
formatter := &MyFormatter{}
log.Manager.GetLoggerAdapter().SetFormatter(formatter)

3. 调用链

Trace reporter support follow adapters.

  • jaeger - publish span to jaeger
  • zipkin - publish span to zipkin
3.1 简易用法
span := log.NewSpan("example")
defer span.End()

span.Info("info message")

Documentation

Overview

Package log logging library.

import "gitee.com/go-wares/log"

func main() {
    // Call close when main function end to report log and span to
    // reporter. Log or span will be discarded if not called.
    defer log.Close()

    // You can call like follows at any code point.
    log.Info("info message")
    log.Infof("info message with: %d", 1)

    // With fields for logging.
    log.Field{
        "captcha": "9981",
        "mobile": "13966013721",
    }.Info("info message")
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close()

Close logging and span reporter. This should be called in main function before exited.

  func main(){
	    defer log.Close()
     ...
  }

func Config

func Config() *conf.Config

Config returns the configuration of log.

conf := log.Config()
conf := log.Manager.Config()
Example
cfg := Config()
fmt.Printf("config: %+v\n", cfg)

// level: DEBUG
Output:

func ContextHeaders

func ContextHeaders(ctx context.Context) map[string]string

ContextHeaders creates the headers map from a context.

Example
ctx := NewContext()
headers := ContextHeaders(ctx)
for key, value := range headers {
	println("header: key =", key, " & value =", value)
}

// header: key = X-B3-TraceId  & value = 90e1e3641c9a3ffcb8b9fce8b71463d5
// header: key = X-B3-SpanId  & value = fdcedf10be021b59
// header: key = X-B3-Version  & value = 0
Output:

func Debug

func Debug(text string)

Debug send debug level log.

Example
Debug("debug message")
Output:

func Debugf

func Debugf(format string, args ...any)

Debugf send debug level log with standard format.

Example
Debugf("debug message with: %s", "formatter")
Output:

func Debugfc

func Debugfc(ctx context.Context, format string, args ...any)

Debugfc send debug level log with context and standard format.

Example
ctx := NewContext()
Debugfc(ctx, "debug message with: %s", "formatter and context")
Output:

func Error

func Error(text string)

Error send error level log.

Example
Error("error message")
Output:

func Errorf

func Errorf(format string, args ...any)

Errorf send error level log with standard format.

Example
Errorf("error message with: %s", "formatter")
Output:

func Errorfc

func Errorfc(ctx context.Context, format string, args ...any)

Errorfc send error level log with context and standard format.

Example
ctx := NewContext()
Errorfc(ctx, "error message with: %s", "formatter and context")
Output:

func Fatal

func Fatal(text string)

Fatal send fatal level log.

Example
Fatal("fatal message")
Output:

func Fatalf

func Fatalf(format string, args ...any)

Fatalf send fatal level log with standard format.

Example
Fatalf("fatal message with: %s", "formatter")
Output:

func Fatalfc

func Fatalfc(ctx context.Context, format string, args ...any)

Fatalfc send fatal level log with context and standard format.

Example
ctx := NewContext()
Fatalfc(ctx, "fatal message with: %s", "formatter and context")
Output:

func Info

func Info(text string)

Info send info level log.

Example
Info("info message")
Output:

func Infof

func Infof(format string, args ...any)

Infof send info level log with standard format.

Example
Infof("info message with: %s", "formatter")
Output:

func Infofc

func Infofc(ctx context.Context, format string, args ...any)

Infofc send info level log with context and standard format.

Example
ctx := NewContext()
Infofc(ctx, "info message with: %s", "formatter and context")
Output:

func NewContext

func NewContext(ctx ...context.Context) context.Context

NewContext creates a new context with a tracing component.

Example
ctx := NewContext()
fmt.Printf("context: %v\n", ctx)
Output:

func Panic

func Panic(text string)

Panic send fatal level log.

Example
Panic("panic message")
Output:

func Panicf

func Panicf(format string, args ...any)

Panicf send fatal level log with standard format.

Example
Panicf("panic message with: %s", "formatter")
Output:

func Panicfc

func Panicfc(ctx context.Context, format string, args ...any)

Panicfc send fatal level log with context and standard format.

Example
ctx := NewContext()
Panicfc(ctx, "panic message with: %s", "formatter and context")
Output:

func Warn

func Warn(text string)

Warn send warn level log.

Example
Warn("warn message")
Output:

func Warnf

func Warnf(format string, args ...any)

Warnf send warn level log with standard format.

Example
Warnf("warn message with: %s", "formatter")
Output:

func Warnfc

func Warnfc(ctx context.Context, format string, args ...any)

Warnfc send warn level log with context and standard format.

Example
ctx := NewContext()
Warnfc(ctx, "warn message with: %s", "formatter and context")
Output:

Types

type Field

type Field map[string]any

Field is a map of key-value pairs.

  • log.Field{"key": "value"}.Info()

func (Field) Debug

func (o Field) Debug(text string)

Debug send debug level log.

Example
Field{
	"Key": "value",
}.Debug("debug message")
Output:

func (Field) Debugf

func (o Field) Debugf(format string, args ...any)

Debugf send debug level log with standard format.

func (Field) Debugfc

func (o Field) Debugfc(ctx context.Context, format string, args ...any)

Debugfc send debug level log with context and standard format.

func (Field) Error

func (o Field) Error(text string)

Error send error level log.

Example
Field{
	"Key": "value",
}.Error("error message")
Output:

func (Field) Errorf

func (o Field) Errorf(format string, args ...any)

Errorf send error level log with standard format.

func (Field) Errorfc

func (o Field) Errorfc(ctx context.Context, format string, args ...any)

Errorfc send .

func (Field) Fatal

func (o Field) Fatal(text string)

Fatal send fatal level log.

Example
Field{
	"Key": "value",
}.Fatal("fatal message")
Output:

func (Field) Fatalf

func (o Field) Fatalf(format string, args ...any)

Fatalf send fatal level log with standard format.

func (Field) Fatalfc

func (o Field) Fatalfc(ctx context.Context, format string, args ...any)

Fatalfc send fatal level log with context and standard format.

func (Field) Info

func (o Field) Info(text string)

Info send info level log.

Example
Field{
	"Key": "value",
}.Info("info message")
Output:

func (Field) Infof

func (o Field) Infof(format string, args ...any)

Infof send info level log with standard format.

func (Field) Infofc

func (o Field) Infofc(ctx context.Context, format string, args ...any)

Infofc send info level log with context and standard format.

func (Field) Warn

func (o Field) Warn(text string)

Warn send warn level log.

Example
Field{
	"Key": "value",
}.Warn("warn message")
Output:

func (Field) Warnf

func (o Field) Warnf(format string, args ...any)

Warnf send warn level log with standard format.

func (Field) Warnfc

func (o Field) Warnfc(ctx context.Context, format string, args ...any)

Warnfc send warn level log with context and standard format.

type Management

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

Management is a component for managing logging and trace.

var (
	// Manager
	// is a component for managing logging and trace.
	Manager *Management
)

func (*Management) Config

func (o *Management) Config() *conf.Config

Config returns the configuration of the log management.

Example
// Config is initialized from config file.
// Path: config/log.yaml
cfg := Manager.Config()

// You can override configured values.
cfg.SetAdapter("term")
cfg.SetLevel("debug")

println("debug status: ", cfg.DebugOn())
Output:

func (*Management) ConfigLoggerAdapter

func (o *Management) ConfigLoggerAdapter(adapter base.Adapter)

ConfigLoggerAdapter configure the logger adapter.

func (*Management) ConfigTracerReporter

func (o *Management) ConfigTracerReporter(tracer base.Tracer)

ConfigTracerReporter configure the tracer reporter.

func (*Management) GetLoggerAdapter

func (o *Management) GetLoggerAdapter() base.Logger

GetLoggerAdapter returns the latest logger adapter.

Example
// Define your formatter.
formatter := &term.Formatter{}

// Override default formatter.
adapter := Manager.GetLoggerAdapter()
adapter.SetFormatter(formatter)
Output:

func (*Management) GetTracerReporter

func (o *Management) GetTracerReporter() base.Reporter

GetTracerReporter returns the latest tracer reporter.

Example
// Define your formatter.
formatter := &jaeger.Formatter{}

// Override default formatter.
reporter := Manager.GetTracerReporter()
reporter.SetFormatter(formatter)
Output:

func (*Management) Log

func (o *Management) Log(ctx context.Context, field map[string]any, level base.Level, format string, args ...any)

Log report the log with level and contents.

func (*Management) LogItem

func (o *Management) LogItem(item *base.Item)

LogItem report the log with item.

func (*Management) Report

func (o *Management) Report(span base.Span)

Report reports the span of a trace.

func (*Management) StopAndWait

func (o *Management) StopAndWait()

StopAndWait stops the log management and waits for the log to be reported.

type Span

type Span = base.Span

Span is an interface for call chain span.

func NewSpan

func NewSpan(format string, args ...any) Span

NewSpan creates a new span component.

Example
span := NewSpan("test")
defer span.End()
span.Info("info log on a span")
Output:

func NewSpanWithContext

func NewSpanWithContext(ctx context.Context, format string, args ...any) Span

NewSpanWithContext creates a new span component.

Example
span := NewSpanWithContext(nil, "example")
defer span.End()

span.SetAttr("key", "value")

span.Info("info log on a span")
Output:

func NewSpanWithHeader added in v0.1.11

func NewSpanWithHeader(header map[string]any, format string, args ...any) Span

func NewSpanWithRequest

func NewSpanWithRequest(req *http.Request) Span

NewSpanWithRequest creates a new span component with given request of a http request.

Example
var request *http.Request

span := NewSpanWithRequest(request)
defer span.End()
span.Info("info log on a span")
Output:

Directories

Path Synopsis
Package conf is a component that defines the log configuration.
Package conf is a component that defines the log configuration.
plugins
reporters/zipkin/model
Package model contains the Zipkin V2 model which is used by the Zipkin Go tracer implementation.
Package model contains the Zipkin V2 model which is used by the Zipkin Go tracer implementation.

Jump to

Keyboard shortcuts

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