logrus

package module
v0.0.0-...-1db7dc6 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

Go2sky with logrus (v1.8.1)

Installation

go get -u github.com/SkyAPM/go2sky-plugins/logrus

Usage

package main

import (
	"context"
	"github.com/sirupsen/logrus"
	logrusplugin "github.com/SkyAPM/go2sky-plugins/logrus"
)

func main() {
	// init format with custom trace context key
	// SW_CTX format: [$serviceName,$instanceName,$traceId,$traceSegmentId,$spanId]
	logrus.SetFormatter(logrusplugin.Wrap(&logrus.JSONFormatter{}, "SW_CTX"}))

	// init tracer

	// log with context
	ctx := context.Background()
	logrus.WithContext(ctx).Info("test1")
}

See more.

Documentation

Overview

Package logrus is a plugin that can be transmit trace context to the log framework.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WrapFormat

type WrapFormat struct {
	Base logrus.Formatter
	// contains filtered or unexported fields
}

WrapFormat is wrap format to transmit trace context when logging

Example
context := context.Background()

logrus.SetOutput(os.Stdout)

// init tracer
_, err := go2sky.NewTracer("example")
if err != nil {
	logrus.Fatalf("create tracer error %v \n", err)
}

// json format
logrus.SetFormatter(Wrap(&logrus.JSONFormatter{
	DisableTimestamp: true,
}, "SW_CTX"))
logrus.WithContext(context).Info("test1")

// test format
logrus.SetFormatter(Wrap(&logrus.TextFormatter{
	DisableTimestamp: true,
}, "SW_CTX"))
logrus.WithContext(context).Info("test2")
Output:

{"SW_CTX":"[,,N/A,N/A,-1]","level":"info","msg":"test1"}
level=info msg=test2 SW_CTX="[,,N/A,N/A,-1]"

func Wrap

func Wrap(base logrus.Formatter, contextKey string) *WrapFormat

Wrap original format

func (*WrapFormat) Format

func (format *WrapFormat) Format(entry *logrus.Entry) ([]byte, error)

Format logging with trace context

Jump to

Keyboard shortcuts

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