Giter Site home page Giter Site logo

golog's Introduction

golog

GoDoc Build Status Go Report Card codecov

Features

  1. Unstructured
  2. Leveled
  3. With caller (file path / name and line number)
  4. Customizable output format
  5. Rotating by size, date or hour
  6. Cross platform, tested on Linux, macOS and Windows
  7. No 3rd party dependance
  8. Fast

Installation

go get -u github.com/keakon/golog

Examples

Logging to console

package main

import (
    "github.com/keakon/golog"
    "github.com/keakon/golog/log"
)

func main() {
    l := golog.NewStdoutLogger()
    defer l.Close()

    l.Infof("hello %d", 1)

    log.SetDefaultLogger(l)
    test()
}

func test() {
    log.Infof("hello %d", 2)
}

Logging to file

func main() {
    w, _ := golog.NewBufferedFileWriter("test.log")
    l := golog.NewLoggerWithWriter(w)
    defer l.Close()

    l.Infof("hello world")
}

Rotating

func main() {
    w, _ := golog.NewTimedRotatingFileWriter("test", golog.RotateByDate, 30)
    l := golog.NewLoggerWithWriter(w)
    defer l.Close()

    l.Infof("hello world")
}

Formatting

func main() {
    w := golog.NewStdoutWriter()

    f := golog.ParseFormat("[%l %D %T %S] %m")
    h := golog.NewHandler(golog.InfoLevel, f)
    h.AddWriter(w)

    l := golog.NewLogger(golog.InfoLevel)
    l.AddHandler(h)
    defer l.Close()

    l.Infof("hello world")
}

Check document for more format directives.

Fast timer

func main() {
    golog.StartFastTimer()
    defer golog.StopFastTimer()

    l := golog.NewStdoutLogger()
    defer l.Close()

    l.Infof("hello world")
}

The fast timer is about 30% faster than calling time.Time() for each logging record. But it's not thread-safe which may cause some problems (I think those are neglectable in most cases):

  1. The timer updates every 1 second, so the logging time can be at most 1 second behind the real time.
  2. Each thread will notice the changes of timer in a few milliseconds, so the concurrent logging messages may get different logging time (less than 2% probability). eg:
[I 2021-09-13 14:31:25 log_test:206] test
[I 2021-09-13 14:31:24 log_test:206] test
[I 2021-09-13 14:31:25 log_test:206] test
  1. When the day changing, the logging date and time might from different day. eg:
[I 2021-09-12 23:59:59 log_test:206] test
[I 2021-09-13 23:59:59 log_test:206] test
[I 2021-09-12 00:00:00 log_test:206] test

Benchmarks

go1.17 darwin/amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz

BenchmarkDiscardLogger-12                 13788436       77.83 ns/op       0 B/op      0 allocs/op
BenchmarkDiscardLoggerWithoutTimer-12     10472464       112.1 ns/op       0 B/op      0 allocs/op
BenchmarkNopLog-12                      1000000000      0.1918 ns/op       0 B/op      0 allocs/op
BenchmarkMultiLevels-12                    3922504       304.8 ns/op       0 B/op      0 allocs/op
BenchmarkBufferedFileLogger-12             4937521       251.7 ns/op       0 B/op      0 allocs/op

BenchmarkDiscardZerolog-12                 4074019       289.8 ns/op     280 B/op      3 allocs/op
BenchmarkDiscardZap-12                     2908678       409.8 ns/op     321 B/op      7 allocs/op

Example output of the benchmarks:

[I 2018-11-20 17:05:37 log_test:118] test

golog's People

Contributors

he11olx avatar keakon 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.