logex

package module
v1.2.15 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2020 License: MIT Imports: 7 Imported by: 5

README

logex

Go GitHub tag (latest SemVer) Sourcegraph GoDoc go.dev Go Report Card Coverage Status FOSSA Status

an enhanced for logrus. logex append the context call info to the log.

Since v1.2.0, logex allows switching the logging backend transparently.

image-20200927083958978

Features

  • Pre-setup logging backends with clickeable caller info: logrus or zap
  • Generic logging interface to cover the various logging backends via: log.Logger, build.New(config)

Usage

Build logger transparently

We provide the ability to switch logging backends transparently now.

A sample config file looks like:

app:

  # runmode: devel  # devel, prod

  logger:
    # The field 'level' will be reset to "debug" while the app is started up within a debugger
    # available values are:
    #   "disable"/"off", "panic", "fatal", "error", "warn", "info", "debug", "trace"
    level:  info
    format: text                  # text, json, logfmt, ...
    backend: sugar                # zap, sugar(sugared-zap) or logrus
    target: file                  # console, file
    directory: /var/log/$APPNAME

Load it to Config structure:

import "github.com/hedzr/log"
var config *log.LoggerConfig = log.NewLoggerConfig()
// ...

And build the backend:

import "github.com/hedzr/logex/build"
logger := build.New(config)
logger.Debugf("int value = %v", intVal)
Or build a logger backend directly
import "github.com/hedzr/logex/logx/logrus"
logrus.New(level string, traceMode, debugMode bool, opts ...Opt)

import "github.com/hedzr/logex/logx/zap"
zap.New(level string, traceMode, debugMode bool, opts ...Opt)

import "github.com/hedzr/logex/logx/zap/sugar"
sugar.New(level string, traceMode, debugMode bool, opts ...Opt)

Legacy tools
Enable logrus
import "github.com/hedzr/logex"

func init(){
    logex.Enable()
    // Or:
    logex.EnableWith(logrus.DebugLevel)
}
Ignore the extra caller frames

If you are writing logging func wrappers, you might ignore the extra caller frames for those wrappers:

func wrong(err error, fmt string, args interface{}) {
    logrus.WithError(err).WithFields(logrus.Fields{
        logex.SKIP: 1,  // ignore wrong() frame
    }).Errorf(fmt, args)
}

func wrongInner(err error, fields logrus.Fields, fmt string, args interface{}) {
    logrus.WithError(err).WithFields(fields).Errorf(fmt, args)
}

func wrongwrong(err error, fmt string, args interface{}) {
    wrongInner(err, logrus.Fields{
        logex.SKIP: 2,  // ignore wrongwrong() and wrongInner() frame
    }, fmt, args...)
}

For go test

make logrus works in go test

The codes is copied from:

https://github.com/sirupsen/logrus/issues/834

And in a test function, you could code now:

   func TestFoo(t *testing.T) {
     defer logex.CaptureLog(t).Release()
     // …
   }

ACK

LICENSE

MIT

FOSSA Status

Documentation

Overview

Package logex is a logging helper library which append the context call info to logging output for locating

Index

Constants

View Source
const (
	// AppName const
	AppName = "logex"
	// Version const
	Version = "1.2.15"
	// VersionInt const
	VersionInt = 0x01020e
)
View Source
const SKIP = formatter.SKIP

Variables

This section is empty.

Functions

func Enable

func Enable()

func EnableWith added in v1.0.3

func EnableWith(lvl log.Level, opts ...Option)

func GetDebugMode added in v1.2.0

func GetDebugMode() bool

GetDebugMode return the debug boolean flag generally

func GetLevel

func GetLevel() log.Level

func GetTraceMode added in v1.2.0

func GetTraceMode() bool

GetTraceMode return the trace boolean flag generally

func InDebugging added in v1.2.0

func InDebugging() bool

InDebugging check if the delve debugger presents

func SetDebugMode added in v1.2.0

func SetDebugMode(b bool)

SetDebugMode set the debug boolean flag generally

func SetTraceMode added in v1.2.0

func SetTraceMode(b bool)

SetTraceMode set the trace boolean flag generally

func SetupLoggingFormat added in v1.1.9

func SetupLoggingFormat(format string, logexSkipFrames int)

Types

type LogCapturer added in v1.0.1

type LogCapturer interface {
	Release()
}

LogCapturer reroutes testing.T log output

func CaptureLog added in v1.0.1

func CaptureLog(t *testing.T) LogCapturer

CaptureLog redirects logrus output to testing.Log

type Option added in v1.1.9

type Option func()

Directories

Path Synopsis
logx
zap

Jump to

Keyboard shortcuts

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