xlog

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: MIT Imports: 8 Imported by: 40

README

xlog

xlog is a simple and easy-to-use logger with zap logger as the bottom abstraction

介绍

xlog是一个对zap logger的简单的封装, 意在简化配置, 增强可控, 简洁易用。

example

package example_test

import (
	"testing"
	"time"

	"github.com/pubgo/dix"
	"github.com/pubgo/xerror"
	"github.com/pubgo/xlog"
	"github.com/pubgo/xlog/internal"
	"github.com/pubgo/xlog/xlog_config"
)

var log = xlog.GetLog()

func init() {
	dix.Go(func(log1 xlog.XLog) {
		log = log1.
			Named("service").With(xlog.String("key", "service")).
			Named("hello").With(xlog.String("key", "hello")).
			Named("world").With(xlog.String("key", "world"))
	})
}

func TestExample(t *testing.T) {
	log.Debug("hello",
		xlog.Any("hss", "ss"),
	)

	dix.Go(initCfgFromJsonDebug(time.Now().Format("2006-01-02 15:04:05")))

	log.Info("hello",
		xlog.Any("hss", "ss"),
	)
	//fmt.Println(dix.Graph())
}

func initCfgFromJsonDebug(name string) internal.XLog {
	cfg := `{
        "level": "debug",
        "development": true,
        "disableCaller": false,
        "disableStacktrace": false,
        "sampling": null,
        "encoding": "console",
        "encoderConfig": {
                "messageKey": "M",
                "levelKey": "L",
                "timeKey": "T",
                "nameKey": "N",
                "callerKey": "C",
                "stacktraceKey": "S",
                "lineEnding": "\n",
                "levelEncoder": "capitalColor",
                "timeEncoder": "iso8601",
                "durationEncoder": "string",
                "callerEncoder": "default",
                "nameEncoder": ""
        },
        "outputPaths": [
                "stderr"
        ],
        "errorOutputPaths": [
                "stderr"
        ],
        "initialFields": null
}`

	zl, err := xlog_config.NewZapLoggerFromJson([]byte(cfg), xlog_config.WithEncoding("console"))
	xerror.Exit(err)
	return xlog.New(zl.WithOptions(xlog.AddCallerSkip(1)))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DPanic

func DPanic(msg string, fields ...internal.Field)

func DPanicF

func DPanicF(format string, a ...interface{})

func Debug

func Debug(msg string, fields ...internal.Field)

func DebugF

func DebugF(format string, a ...interface{})

func Error

func Error(msg string, fields ...internal.Field)

func ErrorF

func ErrorF(format string, a ...interface{})

func Fatal

func Fatal(msg string, fields ...internal.Field)

func FatalF

func FatalF(format string, a ...interface{})

func Info

func Info(msg string, fields ...internal.Field)

func InfoF

func InfoF(format string, a ...interface{})

func Named

func Named(s string, opts ...zap.Option) internal.XLog

func Panic

func Panic(msg string, fields ...internal.Field)

func PanicF

func PanicF(format string, a ...interface{})

func SetLog added in v0.0.6

func SetLog(zl XLog) error

func Sync

func Sync(ll ...XLog) (err error)

func Warn

func Warn(msg string, fields ...internal.Field)

func WarnF

func WarnF(format string, a ...interface{})

func With

func With(fields ...zap.Field) internal.XLog

Types

type Field

type Field = zap.Field

func Any

func Any(key string, value interface{}) Field

Any takes a key and an arbitrary value and chooses the best way to represent them as a field, falling back to a reflection-based approach only if necessary.

Since byte/uint8 and rune/int32 are aliases, Any can't differentiate between them. To minimize surprises, []byte values are treated as binary blobs, byte values are treated as uint8, and runes are always treated as integers.

func Binary

func Binary(key string, val []byte) Field

Binary constructs a field that carries an opaque binary blob.

Binary data is serialized in an encoding-appropriate format. For example, zap's JSON encoder base64-encodes binary blobs. To log UTF-8 encoded text, use ByteString.

func Bool

func Bool(key string, val bool) Field

Bool constructs a field that carries a bool.

func ByteString

func ByteString(key string, val []byte) Field

ByteString constructs a field that carries UTF-8 encoded text as a []byte. To log opaque binary blobs (which aren't necessarily valid UTF-8), use Binary.

func Complex128

func Complex128(key string, val complex128) Field

Complex128 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex128 to interface{}).

func Complex64

func Complex64(key string, val complex64) Field

Complex64 constructs a field that carries a complex number. Unlike most numeric fields, this costs an allocation (to convert the complex64 to interface{}).

func Duration

func Duration(key string, val time.Duration) Field

Duration constructs a field with the given key and value. The encoder controls how the duration is serialized.

func Float32

func Float32(key string, val float32) Field

Float32 constructs a field that carries a float32. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Float64

func Float64(key string, val float64) Field

Float64 constructs a field that carries a float64. The way the floating-point value is represented is encoder-dependent, so marshaling is necessarily lazy.

func Fmt

func Fmt(format string, a ...interface{}) Field

func Int

func Int(key string, val int) Field

Int constructs a field with the given key and value.

func Int16

func Int16(key string, val int16) Field

Int16 constructs a field with the given key and value.

func Int32

func Int32(key string, val int32) Field

Int32 constructs a field with the given key and value.

func Int64

func Int64(key string, val int64) Field

Int64 constructs a field with the given key and value.

func Int8

func Int8(key string, val int8) Field

Int8 constructs a field with the given key and value.

func Object

func Object(key string, val zapcore.ObjectMarshaler) Field

Object constructs a field with the given key and ObjectMarshaler. It provides a flexible, but still type-safe and efficient, way to add map- or struct-like user-defined types to the logging context. The struct's MarshalLogObject method is called lazily.

func Reflect

func Reflect(key string, val interface{}) Field

Reflect constructs a field with the given key and an arbitrary object. It uses an encoding-appropriate, reflection-based function to lazily serialize nearly any object into the logging context, but it's relatively slow and allocation-heavy. Outside tests, Any is always a better choice.

If encoding fails (e.g., trying to serialize a map[int]string to JSON), Reflect includes the error message in the final log output.

func Skip

func Skip() Field

Skip constructs a no-op field, which is often useful when handling invalid inputs in other Field constructors.

func Stack

func Stack(key string) Field

Stack constructs a field that stores a stacktrace of the current goroutine under provided key. Keep in mind that taking a stacktrace is eager and expensive (relatively speaking); this function both makes an allocation and takes about two microseconds.

func String

func String(key string, val string) Field

String constructs a field with the given key and value.

func Stringer

func Stringer(key string, val fmt.Stringer) Field

Stringer constructs a field with the given key and the output of the value's String method. The Stringer's String method is called lazily.

func Time

func Time(key string, val time.Time) Field

Time constructs a Field with the given key and value. The encoder controls how the time is serialized.

func Uint

func Uint(key string, val uint) Field

Uint constructs a field with the given key and value.

func Uint16

func Uint16(key string, val uint16) Field

Uint16 constructs a field with the given key and value.

func Uint32

func Uint32(key string, val uint32) Field

Uint32 constructs a field with the given key and value.

func Uint64

func Uint64(key string, val uint64) Field

Uint64 constructs a field with the given key and value.

func Uint8

func Uint8(key string, val uint8) Field

Uint8 constructs a field with the given key and value.

func Uintptr

func Uintptr(key string, val uintptr) Field

Uintptr constructs a field with the given key and value.

type Option added in v0.0.6

type Option = zap.Option

func AddCaller added in v0.0.6

func AddCaller() Option

func AddCallerSkip added in v0.0.6

func AddCallerSkip(skip int) Option

func AddStacktrace added in v0.0.6

func AddStacktrace(lvl zapcore.LevelEnabler) Option

func Development added in v0.0.6

func Development() Option

func ErrorOutput added in v0.0.6

func ErrorOutput(w zapcore.WriteSyncer) Option

func Fields added in v0.0.6

func Fields(fs ...Field) Option

func Hooks added in v0.0.6

func Hooks(hooks ...func(zapcore.Entry) error) Option

func IncreaseLevel added in v0.0.6

func IncreaseLevel(lvl zapcore.LevelEnabler) Option

func WithCaller added in v0.0.6

func WithCaller(enabled bool) Option

type XLog

type XLog = internal.XLog

func GetDevLog

func GetDevLog() XLog

func New added in v0.0.6

func New(zl *zap.Logger) XLog

Directories

Path Synopsis
log
writer/rotate/file-rotatelogs
package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
package rotatelogs is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.

Jump to

Keyboard shortcuts

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