Skip to content
Simple and configurable Logging in Go, with level, formatters and writers
Branch: master
Clone or download
Latest commit f16df80 May 19, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples Fixes fileline in formatters May 17, 2018
formatters fixes go-vet error: Format passes lock by value May 19, 2018
vendor/golang.org/x
writers Update comments on public structs and methods May 19, 2018
.travis.yml
Gopkg.lock New logger May 17, 2018
Gopkg.toml New logger May 17, 2018
LICENSE Add LICENSE Oct 23, 2016
README.md Update README.md May 19, 2018
doc.go
formatter.go fixes go-vet error: Format passes lock by value May 19, 2018
level.go
level_test.go Update tests May 17, 2018
log.go Update comments on public structs and methods May 19, 2018
log_interface.go New logger May 17, 2018
logger.go Update comments on public structs and methods May 19, 2018
logger_test.go Remove zero value assignment (golint) May 19, 2018

README.md

go-log

GoDoc Build Status Coverage Status Go Report Card License

Logging package similar to log4j for the Golang.

  • Support dynamic log level
  • Support customized formatter
    • TextFormatter
    • JSONFormatter
  • Support multiple rolling file writers
    • FixedSizeFileWriter
    • DailyFileWriter
    • AlwaysNewFileWriter

Installation

$ go get github.com/subchen/go-log

Usage

package main

import (
	"os"
	"errors"
	"github.com/subchen/go-log"
)

func main() {
	log.Debugf("app = %s", os.Args[0])
	log.Errorf("error = %v", errors.New("some error"))

	// dynamic set level
	log.Default.Level = log.WARN

	log.Debug("cannot output debug message")
	log.Errorln("can output error message", errors.New("some error"))
}

Output

Default log to console, you can set Logger.Out to set a file writer into log.

import (
	"github.com/subchen/go-log"
	"github.com/subchen/go-log/writers"
)

log.Default.Out = &writers.FixedSizeFileWriter{
	Name:	 "/tmp/test.log",
	MaxSize:  10 * 1024 * 1024, // 10m
	MaxCount: 10,
})

Three builtin writers for use

// Create log file if file size large than fixed size (10m)
// files: /tmp/test.log.0 .. test.log.10
&writers.FixedSizeFileWriter{
	Name:	 "/tmp/test.log",
	MaxSize:  10 * 1024 * 1024, // 10m
	MaxCount: 10,
}

// Create log file every day.
// files: /tmp/test.log.20160102
&writers.DailyFileWriter{
	Name: "/tmp/test.log",
	MaxCount: 10,
}

// Create log file every process.
// files: /tmp/test.log.20160102_150405
&writers.AlwaysNewFileWriter{
	Name: "/tmp/test.log",
	MaxCount: 10,
}

// Output to multiple writes
io.MultiWriter(
	os.Stdout,
	&writers.DailyFileWriter{
		Name: "/tmp/test.log",
		MaxCount: 10,
	}
	//...
)

Formatter

import (
	"github.com/subchen/go-log"
	"github.com/subchen/go-log/formatters"
)

log.Default.Formatter = new(formatters.TextFormatter)

New Logger instance

import (
	"github.com/subchen/go-log"
)

func main() {
	logger := &log.Logger{
		Level:     log.INFO,
		Formatter: new(formatters.JSONFormatter),
		Out:       os.Stdout,
	}

	logger.Infof("i = %d", 99)
}

LICENSE

Apache 2.0

You can’t perform that action at this time.