golog

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2021 License: Apache-2.0 Imports: 10 Imported by: 3

README

Log the world very easy

GitHub forks GitHub stars GitHub last commit GitHub issues License

Thanks To Uber ZapLog! Log to console or file very easy and fast!

中文说明

How to use

Simple:

go get -v github.com/hunterhug/golog

Demo

default logger is InfoLevel, and has long func caller.

Demo1
package main

import . "github.com/hunterhug/golog"

func main() {
	// use default log
	Info("now is Info", 2, " good")
	Debug("now is Debug", 2, " good")
	Warn("now is Warn", 2, " good")
	Error("now is Error", 2, " good")
	Infof("now is Infof: %d,%s", 2, "good")
	Debugf("now is Debugf: %d,%s", 2, "good")
	Warnf("now is Warnf: %d,%s", 2, "good")
	Errorf("now is Errorf: %d,%s", 2, "good")
	Sync()

	// config log
	SetLevel(DebugLevel).SetCallerShort(true).SetOutputJson(true).InitLogger()

	Info("now is Info", 2, " good")
	Debug("now is Debug", 2, " good")
	Warn("now is Warn", 2, " good")
	Error("now is Error", 2, " good")
	Infof("now is Infof: %d,%s", 2, "good")
	Debugf("now is Debugf: %d,%s", 2, "good")
	Warnf("now is Warnf: %d,%s", 2, "good")
	Errorf("now is Errorf: %d,%s", 2, "good")
	Sync()

}

Output:

2021-08-27T11:16:10.455+0800    INFO    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:7    main.main       now is Info2 good
2021-08-27T11:16:10.455+0800    WARN    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:9    main.main       now is Warn2 good
2021-08-27T11:16:10.455+0800    ERROR   /Users/pika/Documents/code/github/golog/demo/demo1/main.go:10   main.main       now is Error2 good
2021-08-27T11:16:10.455+0800    INFO    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:11   main.main       now is Infof: 2,good
2021-08-27T11:16:10.455+0800    WARN    /Users/pika/Documents/code/github/golog/demo/demo1/main.go:13   main.main       now is Warnf: 2,good
2021-08-27T11:16:10.455+0800    ERROR   /Users/pika/Documents/code/github/golog/demo/demo1/main.go:14   main.main       now is Errorf: 2,good
{"l":"info","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:19","func":"main.main","msg":"now is Info2 good"}
{"l":"debug","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:20","func":"main.main","msg":"now is Debug2 good"}
{"l":"warn","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:21","func":"main.main","msg":"now is Warn2 good"}
{"l":"error","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:22","func":"main.main","msg":"now is Error2 good"}
{"l":"info","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:23","func":"main.main","msg":"now is Infof: 2,good"}
{"l":"debug","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:24","func":"main.main","msg":"now is Debugf: 2,good"}
{"l":"warn","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:25","func":"main.main","msg":"now is Warnf: 2,good"}
{"l":"error","t":"2021-08-27T11:16:10.455+0800","caller":"demo1/main.go:26","func":"main.main","msg":"now is Errorf: 2,good"}
Demo2

you can config log to file and auto rotate.

package main

import (
	"context"
	"fmt"
	. "github.com/hunterhug/golog"
	"time"
)

func main() {
	SetName("log_demo")
	SetLevel(InfoLevel)

	SetCallerShort(true).SetOutputJson(true)

	AddFieldFunc(func(ctx context.Context, m map[string]interface{}) {
		m["diy_filed"] = ctx.Value("diy")
	})

	SetOutputFile("./log", "demo").SetFileRotate(30*24*time.Hour, 24*time.Hour)
	SetIsOutputStdout(true)
	InitLogger()

	Info("now is Info", 2, " good")
	Debug("now is Debug", 2, " good")
	Warn("now is Warn", 2, " good")
	Error("now is Error", 2, " good")
	Infof("now is Infof: %d,%s", 2, "good")
	Debugf("now is Debugf: %d,%s", 2, "good")
	Warnf("now is Warnf: %d,%s", 2, "good")
	Errorf("now is Errorf: %d,%s", 2, "good")

	ctx := context.WithValue(context.Background(), "diy", []interface{}{"ahhahahahahh"})
	InfoContext(ctx, "InfoContext")
	InfoContext(ctx, "InfoContext, %s:InfoContext, %d", "ss", 333)
	InfoWithFields(map[string]interface{}{"k1": "sss"}, "InfoWithFields:%s,%d", "sss", 33333)
	InfoWithFields(map[string]interface{}{"k1": "sss"}, "InfoWithFields")

	err := Sync()
	if err != nil {
		fmt.Println(err.Error())
	}
}

Output is in the file dir ·/log, due to SetIsOutputStdout is true it also output to console.

Usage

very easy to understand.

type LoggerInterface interface {
	SetOutputFile(logPath, fileName string)
	SetFileRotate(fileMaxAge, fileRotation time.Duration)
	SetLevel(level Level)
	SetCallerShort(short bool)
	SetName(name string)
	SetIsOutputStdout(isOutputStdout bool)
	SetCallerSkip(skip int)
	SetOutputJson(json bool)

	GetOutputFile() (logPath, fileName string)
	GetFileRotate() (fileMaxAge, fileRotation time.Duration)
	GetLevel() (level Level)
	GetCallerShort() (short bool)
	GetName() (name string)
	GetIsOutputStdout() (isOutputStdout bool)
	GetCallerSkip() (skip int)
	GetOutputJson() bool

	InitLogger()
	Sync() error

	Panicf(template string, args ...interface{})
	Fatalf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Debugf(template string, args ...interface{})

	Panic(args ...interface{})
	Fatal(args ...interface{})
	Error(args ...interface{})
	Warn(args ...interface{})
	Info(args ...interface{})
	Debug(args ...interface{})

	PanicWithFields(fields map[string]interface{}, template string, args ...interface{})
	FatalWithFields(fields map[string]interface{}, template string, args ...interface{})
	ErrorWithFields(fields map[string]interface{}, template string, args ...interface{})
	WarnWithFields(fields map[string]interface{}, template string, args ...interface{})
	InfoWithFields(fields map[string]interface{}, template string, args ...interface{})
	DebugWithFields(fields map[string]interface{}, template string, args ...interface{})

	PanicContext(ctx context.Context, template string, args ...interface{})
	FatalContext(ctx context.Context, template string, args ...interface{})
	ErrorContext(ctx context.Context, template string, args ...interface{})
	WarnContext(ctx context.Context, template string, args ...interface{})
	InfoContext(ctx context.Context, template string, args ...interface{})
	DebugContext(ctx context.Context, template string, args ...interface{})

	PanicContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	FatalContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	ErrorContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	WarnContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	InfoContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	DebugContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

	AddFieldFunc(func(context.Context, map[string]interface{}))
}

License

Copyright [2021-2021] [github.com/hunterhug]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

View Source
const (
	DebugLevel = zapcore.DebugLevel
	InfoLevel  = zapcore.InfoLevel
	WarnLevel  = zapcore.WarnLevel
	ErrorLevel = zapcore.ErrorLevel
)

Variables

This section is empty.

Functions

func AddFieldFunc

func AddFieldFunc(f func(context.Context, map[string]interface{}))

func Debug

func Debug(args ...interface{})

func DebugContext

func DebugContext(ctx context.Context, template string, args ...interface{})

func DebugContextWithFields

func DebugContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

func DebugWithFields

func DebugWithFields(fields map[string]interface{}, template string, args ...interface{})

func Debugf

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

func Error

func Error(args ...interface{})

func ErrorContext

func ErrorContext(ctx context.Context, template string, args ...interface{})

func ErrorContextWithFields

func ErrorContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

func ErrorWithFields

func ErrorWithFields(fields map[string]interface{}, template string, args ...interface{})

func Errorf

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

func Fatal

func Fatal(args ...interface{})

func FatalContext

func FatalContext(ctx context.Context, template string, args ...interface{})

func FatalContextWithFields

func FatalContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

func FatalWithFields

func FatalWithFields(fields map[string]interface{}, template string, args ...interface{})

func Fatalf

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

func GetCallerShort

func GetCallerShort() (short bool)

func GetCallerSkip

func GetCallerSkip() (skip int)

func GetFileRotate

func GetFileRotate() (fileMaxAge, fileRotation time.Duration)

func GetIsOutputStdout

func GetIsOutputStdout() (isOutputStdout bool)

func GetName

func GetName() (name string)

func GetOutputFile

func GetOutputFile() (logPath, fileName string)

func GetOutputJson

func GetOutputJson() (json bool)

func GetZapLogger added in v1.0.2

func GetZapLogger() *zap.Logger

func GetZapSugaredLogger added in v1.0.2

func GetZapSugaredLogger() *zap.SugaredLogger

func Info

func Info(args ...interface{})

func InfoContext

func InfoContext(ctx context.Context, template string, args ...interface{})

func InfoContextWithFields

func InfoContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

func InfoWithFields

func InfoWithFields(fields map[string]interface{}, template string, args ...interface{})

func Infof

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

func InitLogger

func InitLogger()

func Panic

func Panic(args ...interface{})

func PanicContext

func PanicContext(ctx context.Context, template string, args ...interface{})

func PanicContextWithFields

func PanicContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

func PanicWithFields

func PanicWithFields(fields map[string]interface{}, template string, args ...interface{})

func Panicf

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

func Sync

func Sync() error

func Warn

func Warn(args ...interface{})

func WarnContext

func WarnContext(ctx context.Context, template string, args ...interface{})

func WarnContextWithFields

func WarnContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

func WarnWithFields

func WarnWithFields(fields map[string]interface{}, template string, args ...interface{})

func Warnf

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

Types

type Level

type Level = zapcore.Level

func GetLevel

func GetLevel() (level Level)

func StringLevel

func StringLevel(level string) Level

type LoggerInterface

type LoggerInterface interface {
	SetOutputFile(logPath, fileName string) LoggerInterface
	SetFileRotate(fileMaxAge, fileRotation time.Duration) LoggerInterface
	SetLevel(level Level) LoggerInterface
	SetCallerShort(short bool) LoggerInterface
	SetName(name string) LoggerInterface
	SetIsOutputStdout(isOutputStdout bool) LoggerInterface
	SetCallerSkip(skip int) LoggerInterface
	SetOutputJson(json bool) LoggerInterface

	GetOutputFile() (logPath, fileName string)
	GetFileRotate() (fileMaxAge, fileRotation time.Duration)
	GetLevel() (level Level)
	GetCallerShort() (short bool)
	GetName() (name string)
	GetIsOutputStdout() (isOutputStdout bool)
	GetCallerSkip() (skip int)
	GetOutputJson() bool

	// InitLogger init logger should call this when change config
	InitLogger()
	// Sync terminal the logger should call this to flush
	Sync() error

	Panicf(template string, args ...interface{})
	Fatalf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Debugf(template string, args ...interface{})

	Panic(args ...interface{})
	Fatal(args ...interface{})
	Error(args ...interface{})
	Warn(args ...interface{})
	Info(args ...interface{})
	Debug(args ...interface{})

	PanicWithFields(fields map[string]interface{}, template string, args ...interface{})
	FatalWithFields(fields map[string]interface{}, template string, args ...interface{})
	ErrorWithFields(fields map[string]interface{}, template string, args ...interface{})
	WarnWithFields(fields map[string]interface{}, template string, args ...interface{})
	InfoWithFields(fields map[string]interface{}, template string, args ...interface{})
	DebugWithFields(fields map[string]interface{}, template string, args ...interface{})

	PanicContext(ctx context.Context, template string, args ...interface{})
	FatalContext(ctx context.Context, template string, args ...interface{})
	ErrorContext(ctx context.Context, template string, args ...interface{})
	WarnContext(ctx context.Context, template string, args ...interface{})
	InfoContext(ctx context.Context, template string, args ...interface{})
	DebugContext(ctx context.Context, template string, args ...interface{})

	PanicContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	FatalContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	ErrorContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	WarnContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	InfoContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})
	DebugContextWithFields(ctx context.Context, fields map[string]interface{}, template string, args ...interface{})

	// AddFieldFunc filter deal the fields
	AddFieldFunc(func(context.Context, map[string]interface{}))

	GetZapLogger() *zap.Logger
	GetZapSugaredLogger() *zap.SugaredLogger
}

LoggerInterface hide something you can implement new one

func Logger

func Logger() LoggerInterface

Logger default log which output to console if you want to log to file you must New() and set something then call InitLogger()

func New

func New() LoggerInterface

New can new a logger interface, you can config it by it's method

func SetCallerCallerSkip

func SetCallerCallerSkip(skip int) LoggerInterface

func SetCallerShort

func SetCallerShort(short bool) LoggerInterface

func SetFileRotate

func SetFileRotate(fileMaxAge, fileRotation time.Duration) LoggerInterface

func SetIsOutputStdout

func SetIsOutputStdout(isOutputStdout bool) LoggerInterface

func SetLevel

func SetLevel(level Level) LoggerInterface

func SetName

func SetName(name string) LoggerInterface

func SetOutputFile

func SetOutputFile(logPath, fileName string) LoggerInterface

func SetOutputJson

func SetOutputJson(json bool) LoggerInterface

Directories

Path Synopsis
demo

Jump to

Keyboard shortcuts

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