minlog

package module
v0.0.0-...-34d7dae Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: MIT Imports: 6 Imported by: 0

README

minlog

  • use std lib impl logger with go, help you to collect your log file with configurable
  • minlog have dev|release mode, dev mode don't save log to file but to std, release mode will save to file
  • support trace request in log

How to use

ctx := context.WithValue(context.Background(), "userID", "9527")
minlog := New(&Option{
Release:  true,
Lvl:      Info,
TraceKey: "userID",
Writer:   assertFile("min.log"),
})

minlog.Info(ctx, "this is New info test")
minlog.Infof(ctx, "this is New %s test", "infof")
minlog.Warn(ctx, "this is New warn test")
minlog.Warnf(ctx, "this is New %s test", "warnf")
minlog.Error(ctx, "this is New error test")
minlog.Errorf(ctx, "this is New %s test", "errorf")
go test -benchmem -run=^$minlog -bench '^(BenchmarkNewWithOutF)$'
go test -benchmem -run=^$minlog -bench '^(BenchmarkNewWithF)$'
go test -benchmem -run=^$minlog -bench '^(BenchmarkNewParallelWithOutF)$'
go test -benchmem -run=^$minlog -bench '^(BenchmarkNewParallelWithF)$'

Test with std close and no trace tag

goos: windows
goarch: amd64
pkg: minlog
cpu: Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
BenchmarkNewWithOutF:
 - info-12              315081              5452 ns/op             144 B/op          3 allocs/op
 - debug-12             236989              4344 ns/op             144 B/op          3 allocs/op
 - error-12             187606              6633 ns/op             704 B/op          8 allocs/op
 - warn-12              298220              4073 ns/op             144 B/op          3 allocs/op
BenchmarkNewWithF:
 - infof-12             304359              4655 ns/op             192 B/op          4 allocs/op
 - debugf-12            297622              4515 ns/op             192 B/op          4 allocs/op
 - errorf-12            169093             10873 ns/op             816 B/op          9 allocs/op
 - warnf-12             297420              5000 ns/op             192 B/op          4 allocs/op
BenchmarkNewParallelWithOutF:
 - info-12              383181              3008 ns/op             144 B/op          3 allocs/op
 - debug-12             507474              2995 ns/op             144 B/op          3 allocs/op
 - error-12             391755              3687 ns/op             704 B/op          8 allocs/op
 - warn-12              403269              3005 ns/op             144 B/op          3 allocs/op
BenchmarkNewParallelWithF:
 - infof-12             452118              3080 ns/op             192 B/op          4 allocs/op
 - debugf-12            386314              3089 ns/op             192 B/op          4 allocs/op
 - errorf-12            330423              3664 ns/op             816 B/op          9 allocs/op
 - warnf-12             378901              3099 ns/op             192 B/op          4 allocs/op

example with gin

package main

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"

	"github.com/0x2d3c/minlog"
	"github.com/gin-gonic/gin"
)

var mlog *minlog.Minlog

func init() {
	mlog = minlog.New(&minlog.Option{
		Lvl:      minlog.Info,
		Writer:   file(),
		Release:  false,
		TraceKey: "min-example",
	})
}
func file() io.StringWriter {
	name := "min.log"
	f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
	if os.IsNotExist(err) {
		f, _ = os.Create(name)
	}
	return f
}

func RecordRequest(ctx *gin.Context) {
	body, _ := ioutil.ReadAll(ctx.Request.Body)

	uri := ctx.Request.RequestURI
	ctx.Request.Body = ioutil.NopCloser(bytes.NewReader(body))

	cc := ctx.Copy()

	mlog.Info(cc, fmt.Sprintf("[uri:%s][body: %s]", uri, body))
	mlog.Debug(cc, fmt.Sprintf("[uri:%s][body: %s]", uri, body))
	mlog.Error(cc, fmt.Sprintf("[uri:%s][body: %s]", uri, body))

	ctx.Next()
}

func main() {
	gin.SetMode(gin.ReleaseMode)

	r := gin.New()

	r.Use(RecordRequest)

	r.POST("/ping", func(ctx *gin.Context) {
		var data interface{}
		ctx.Bind(&data)
		ctx.JSON(200, gin.H{"message": data})
	})

	// listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
	r.Run()
}

Documentation

Index

Constants

View Source
const (
	KB // 1KB
	MB // 1MB
	GB // 1GB

	Trace = iota // Trace log level, must have a trace key with value in context
	Info         // Info level
	Warn         // Warn level
	Debug        // Debug level
	Error        // Error level
	Panic        // Panic level
	Fatal        // Fatal level

	INFO  = "[INFO]"
	WARN  = "[WARN]"
	DEBUG = "[DEBUG]"
	ERROR = "[ERROR]"
	PANIC = "[PANIC]"
	FATAL = "[FATAL]"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Minlog

type Minlog struct {
	io.StringWriter // log file writer
	// contains filtered or unexported fields
}

Minlog minlog define

func New

func New(op *Option) *Minlog

New create minlog

func (*Minlog) Debug

func (m *Minlog) Debug(ctx context.Context, msg string)

Debug debug log method

func (*Minlog) Debugf

func (m *Minlog) Debugf(ctx context.Context, msg string, args ...interface{})

func (*Minlog) Error

func (m *Minlog) Error(ctx context.Context, msg string)

Error err log method

func (*Minlog) Errorf

func (m *Minlog) Errorf(ctx context.Context, msg string, args ...interface{})

func (*Minlog) Exit

func (m *Minlog) Exit()

Exit exit minlog

func (*Minlog) Fatal

func (m *Minlog) Fatal(ctx context.Context, msg string)

Fatal fatal log method

func (*Minlog) Fatalf

func (m *Minlog) Fatalf(ctx context.Context, msg string, args ...interface{})

func (*Minlog) Info

func (m *Minlog) Info(ctx context.Context, msg string)

Info info log method

func (*Minlog) Infof

func (m *Minlog) Infof(ctx context.Context, msg string, args ...interface{})

func (*Minlog) Panic

func (m *Minlog) Panic(ctx context.Context, msg string)

Panic panic log method

func (*Minlog) Panicf

func (m *Minlog) Panicf(ctx context.Context, msg string, args ...interface{})

func (*Minlog) Warn

func (m *Minlog) Warn(ctx context.Context, msg string)

Warn warn log method

func (*Minlog) Warnf

func (m *Minlog) Warnf(ctx context.Context, msg string, args ...interface{})

type Option

type Option struct {
	Release  bool            // when value is true which error, fatal, panic will be write to log file
	Lvl      int             // log level
	TraceKey string          // trace customer in log with key
	Writer   io.StringWriter // log writer
}

Option minlog cfg define

Jump to

Keyboard shortcuts

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