Code Monkey home page Code Monkey logo

wlog's Introduction

wlog

A fast, highly scalable log library in Golang, it support different log levels: "debug", "info", "earn", "error", "fatal", "panic".

Features

  • Easy to use (See Quick Start).
  • High efficiency (See Performance).
  • Support the file rotation by size and day.
  • Provide many highly scalable interfaces such as Encoder, Writer, Handler and etc.

Install

go get -u github.com/happyxcj/wlog

Quick Start

package main

import "github.com/happyxcj/wlog"

func main() {
	wlog.UseGlobalLogger(nil)
	defer wlog.Close()
	wlog.Infow("failed to login account",
		wlog.String("username", "root"),
		wlog.String("password", "root"))
}

Performance

The follow benchmarks are mainly to compare the performance of different log libraries. the log libraries are as follow:

​ github.com/happyxcj/wlog

​ go.uber.org/zap

​ standard log (fmt.Println)

​ github.com/Sirupsen/logrus

For more details and more benchmarks , see internal benchmarks.

Benchmark of logging a message with 12 fields:

goos: windows
goarch: amd64
BenchmarkWithFields/wlog-4               1000000   1320 ns/op  816 B/op   32 allocs/op
BenchmarkWithFields/uber-go/zap-4        1000000   2187 ns/op  985 B/op   18 allocs/op
BenchmarkWithFields/fmt.Println-4         300000   4906 ns/op  3785 B/op  129 allocs/op
BenchmarkWithFields/Sirupsen/logrus-4     200000   6921 ns/op  9201 B/op  99 allocs/op

Benchmark of logging a message with 12 key-value pairs:

goos: windows
goarch: amd64
BenchmarkWithPairs/wlog-4                1000000   1875 ns/op  1562 B/op  54 allocs/op
BenchmarkWithPairs/uber-go/zap-4          500000   3017 ns/op  3325 B/op  37 allocs/op
BenchmarkWithPairs/fmt.Println-4 	      300000   5006 ns/op  3785 B/op  129 allocs/op
BenchmarkWithPairs/Sirupsen/logrus-4      200000   6891 ns/op  9202 B/op  99 allocs/op

Examples

Create a Logger with any expected Encoder, Writer and Handler.

Note that If logging a string message with any fields, method Infow(string, ...Field) is a better choice than method With(...Field).Info(...Interface{}).

package main

import "github.com/happyxcj/wlog"

func main() {
	w := wlog.NewIOWriter(os.Stdout)
	bw := wlog.NewBufWriter(w, wlog.SetBufMaxSize(50*1<<20))
	fw := wlog.NewTimingFlushWriter(bw, 5*time.Second)
	h := wlog.NewBaseHandler(fw, wlog.NewTextEncoder(wlog.DisableTime()))
	logger := wlog.NewLogger(h)
	defer logger.Close()
    
   logger.With(wlog.String("username", "root"),
		wlog.String("password", "root"),
		wlog.Int("retry", 5)).
		Info("failed to login account")
    
	logger.Infow("failed to login account",
		wlog.String("username", "root"),
		wlog.String("password", "root"),
		wlog.Int("retry", 5))
}

For more simple usage, see the test file

For more advanced usage such as to configure file rotation, configure multiple loggers and etc, see the examples

References

  • github.com/Sirupsen/logrus
  • go.uber.org/zap

wlog's People

Contributors

happyxcj avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.