You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dingjun 6b72e5702c update version 5 years ago
examples remove submodule 6 years ago
.travis.yml Update .travis.yml 7 years ago
Gopkg.lock New logger 7 years ago
Gopkg.toml New logger 7 years ago
LICENSE Add LICENSE 8 years ago
README.md remove submodule 6 years ago
always_new_file_writer.go remove submodule 6 years ago
daily_file_writer.go remove submodule 6 years ago
doc.go remove submodule 6 years ago
fixed_size_file_writer.go remove submodule 6 years ago
formatter.go set textFormatter as default formatter 6 years ago
go.mod update version 5 years ago
go.sum add go.mod 5 years ago
json_formatter.go remove submodule 6 years ago
level.go New logger 7 years ago
level_test.go Update tests 7 years ago
log.go Update comments on public structs and methods 7 years ago
log_interface.go remove submodule 6 years ago
logger.go set textFormatter as default formatter 6 years ago
logger_test.go Remove zero value assignment (golint) 7 years ago
text_formatter.go remove submodule 6 years ago
util_fileline.go fix filename issue in go module mode 6 years ago
util_terminal.go remove submodule 6 years ago

README.md

go-log

Logging package similar to log4j for the Golang.

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

Installation

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

Usage

package main

import (
	"os"
	"errors"
	"github.com/fangdingjun/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/fangdingjun/go-log"
	"github.com/fangdingjun/go-log/log"
)

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

Three builtin log for use

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

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

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

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

Formatter

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

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

New Logger instance

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

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

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

LICENSE

Apache 2.0