log

package module
v8.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2023 License: Apache-2.0 Imports: 6 Imported by: 14

README

Log

Publish application log to adapter with ASNYC mode. Support kafka, file, terminal.

import "github.com/fuyibing/log/v8"
func init(){
    // Follow configurations are optional. All of follows can be
    // configured in `config/log.yaml`. Filled with default if not
    // configured.
    log.Config.Set(
        conf.SetTimeFormat("2006-01-02 15:04:05.999999"),
        conf.SetLevel(conf.Error),
        conf.SetPrefix("Prefix"),

        conf.SetServiceAddr("172.16.0.110"),
        conf.SetServicePort(8080),
        conf.SetServiceEnvironment("production"),
        conf.SetServiceName("myapp"),
        conf.SetServiceVersion("1.2.3"),
    )

    // If adapter changed by code, You must call log.Client.Reset()
    // to apply it.
    log.Config.Set(conf.SetAdapter(adapters.AdapterKafka))
    log.Client.Reset()
}

func main(){
    // Wait for a while
    // until all logs publish completed.
    //
    // If the Close method `log.Client.Close()` is not set, Some logs
    // end of the application may be lost.
    defer log.Client.Close()

    // ... ...
    
    log.Debug("debug info")
    log.Infof("info message: adapter=%s, level=%v", conf.Adapter, conf.Level)
    
    // ... ...
}

ASync Supports

  • Term - Print log content on console.
  • File - Write log to local file.
  • Kafka - Publish log to kafka.
  • SLS - Aliyun SLS service.

Configurations

Load config file config/log.yaml when package initialized. Use default if not specified.

adapter: term
level: debug
time-format: "2006-01-02 15:04:05.999999"
kafka:
  topic: "Log"
  addresses: 
    - "172.16.0.100:9092"
    - "172.16.0.101:9092"
    - "172.16.0.102:9092"
file:
term:
  color: false

Formatter

System
Terminal
log.Client.GetAdapterRegistry().SetFormatter(formatters.NewTermFormatter())
File
log.Client.GetAdapterRegistry().SetFormatter(formatters.NewFileFormatter())
JSON
log.Client.GetAdapterRegistry().SetFormatter(formatters.NewJsonFormatter())
Custom
type MyFormatter struct{}

func (o *MyFormatter) Body(line *base.Line) []byte {
    // ...
}

func (o *MyFormatter) String(line *base.Line) string {
    // ...
}

func init(){
    log.Client.GetAdapterRegistry().SetFormatter(&MyFormatter{})
}

Example

YAML.
# path: config/log.yaml
adapter: term
level: debug
time-format: 15:04:05.9999
async-disabled: true
term:
  color: true
Run example
cd examples/config-file
go run main.go
Code
func main(){
    defer log.Client.Close()

	log.Debug("debug")
	log.Infof("text format, id=%d, type=%s", 100, "demo")
	log.Map{"id": 100, "type": "demo"}.Infof("with map config")

	c1 := log.NewContext()
	log.Debugfc(c1, "debug")
	log.Infofc(c1, "text format, id=%d, type=%s", 100, "demo")
	log.Map{"id": 100, "type": "demo"}.Infofc(c1, "with map config")

	c2 := log.NewChild(c1)
	log.Infofc(c2, "child of map 1")
	log.Infofc(c2, "child of map 2")
}
Output
[19:16:46.4406][DEBUG][P=65221] debug
[19:16:46.4407][INFO][P=65221] text format, id=100, type=demo
[19:16:46.4407][INFO][P=65221] {"id":100,"type":"demo"} with map config
[19:19:34.3841][DEBUG][P=65221][T=693f34e13932458ba956400d2e921bdc][TS=898c4a1d][TP=][TV=0.0] debug
[19:19:34.3841][INFO][P=65221][T=693f34e13932458ba956400d2e921bdc][TS=898c4a1d][TP=][TV=0.1] text format, id=100, type=demo
[19:19:34.3841][INFO][P=65221][T=693f34e13932458ba956400d2e921bdc][TS=898c4a1d][TP=][TV=0.2] {"id":100,"type":"demo"} with map config
[19:19:34.3841][INFO][P=65221][T=693f34e13932458ba956400d2e921bdc][TS=a2534f36][TP=898c4a1d][TV=0.2.0] child of map 1
[19:19:34.3841][INFO][P=65221][T=693f34e13932458ba956400d2e921bdc][TS=a2534f36][TP=898c4a1d][TV=0.2.1] child of map 2

Documentation

Overview

Package log publish with async mode, allow kafka, file, terminal configuration.

// Import V8 Engine.
import "github.com/fuyibing/log/v8"

Initialize.

func init(){
    // Follow configurations are optional. All of follows can be
    // configured in `config/log.yaml`. Filled with default if not
    // configured.
    log.Config.Set(
        conf.SetTimeFormat("2006-01-02 15:04:05.999999"),
        conf.SetLevel(conf.Error),
        conf.SetPrefix("Prefix"),

        conf.SetServiceAddr("172.16.0.110"),
        conf.SetServicePort(8080),
        conf.SetServiceEnvironment("production"),
        conf.SetServiceName("myapp"),
        conf.SetServiceVersion("1.2.3"),
    )

    // If adapter changed by code, You must call log.Client.Reset()
    // to apply it.
    log.Config.Set(conf.SetAdapter(adapters.AdapterKafka))
    log.Client.Reset()
}

// Main process.

func main(){
    // Wait for a while
    // until all logs publish completed.
    //
    // If the Close method `log.Client.Close()` is not set, Some logs
    // end of the application may be lost.
    defer log.Client.Close()

    ...
    log.Debug("debug info")
    log.Infof("info message: adapter=%s, level=%v", conf.Adapter, conf.Level)
    ...
}

Index

Constants

This section is empty.

Variables

View Source
var (
	Config conf.Configuration
	Client core.Client
)

Functions

func AddTrace added in v8.0.7

func AddTrace(ctx context.Context, request *http.Request)

AddTrace read current trace then add to request.

func Debug

func Debug(text string)

func Debugf

func Debugf(text string, args ...interface{})

func Debugfc

func Debugfc(ctx context.Context, text string, args ...interface{})

func Error

func Error(text string)

func Errorf

func Errorf(text string, args ...interface{})

func Errorfc

func Errorfc(ctx context.Context, text string, args ...interface{})

func Fatal

func Fatal(text string)

func Fatalf

func Fatalf(text string, args ...interface{})

func Fatalfc

func Fatalfc(ctx context.Context, text string, args ...interface{})

func Info

func Info(text string)

func Infof

func Infof(text string, args ...interface{})

func Infofc

func Infofc(ctx context.Context, text string, args ...interface{})

func NewChild

func NewChild(ctx context.Context) context.Context

NewChild return new trace context, child of parent.

func NewChildInfo

func NewChildInfo(ctx context.Context, text string, args ...interface{}) context.Context

NewChildInfo set info level log before return new trace context, child of parent.

func NewContext

func NewContext() context.Context

NewContext return new root trace context.

func NewContextInfo

func NewContextInfo(text string, args ...interface{}) context.Context

NewContextInfo set info level log before return new root context.

func NewRequest

func NewRequest(request *http.Request) context.Context

NewRequest create context based on http request. Return root context if parent trace not specified.

func NewRequestInfo

func NewRequestInfo(request *http.Request, text string, args ...interface{}) context.Context

NewRequestInfo set info level log before return. Create context based on http request. Return root context if parent trace not specified.

func Panic

func Panic(text string)

func Panicf

func Panicf(text string, args ...interface{})

func Panicfc

func Panicfc(ctx context.Context, text string, args ...interface{})

func Warn

func Warn(text string)

func Warnf

func Warnf(text string, args ...interface{})

func Warnfc

func Warnfc(ctx context.Context, text string, args ...interface{})

Types

type Map added in v8.0.8

type Map map[string]interface{}

Map configure struct fields into log.

func (Map) Debugf added in v8.0.8

func (p Map) Debugf(text string, args ...interface{})

func (Map) Debugfc added in v8.0.8

func (p Map) Debugfc(ctx context.Context, text string, args ...interface{})

func (Map) Errorf added in v8.0.8

func (p Map) Errorf(text string, args ...interface{})

func (Map) Errorfc added in v8.0.8

func (p Map) Errorfc(ctx context.Context, text string, args ...interface{})

func (Map) Fatalf added in v8.0.8

func (p Map) Fatalf(text string, args ...interface{})

func (Map) Fatalfc added in v8.0.8

func (p Map) Fatalfc(ctx context.Context, text string, args ...interface{})

func (Map) Infof added in v8.0.8

func (p Map) Infof(text string, args ...interface{})

func (Map) Infofc added in v8.0.8

func (p Map) Infofc(ctx context.Context, text string, args ...interface{})

func (Map) Warnf added in v8.0.8

func (p Map) Warnf(text string, args ...interface{})

func (Map) Warnfc added in v8.0.8

func (p Map) Warnfc(ctx context.Context, text string, args ...interface{})

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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