log

package module
v0.0.0-...-466de78 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2022 License: MIT Imports: 10 Imported by: 0

README

log

simple log for Go.

Features
  1. structured
  2. compatible with std log
  3. log on demand: real-time view, not stored
  4. logging with extra data: WithFields()
  5. level control: Only log the specified severity or above
  6. count limit
  7. query
Example
func TestLog(t *testing.T) {
	dbname := "test_log.db"
	options := Options{
		FIFO:       "test_log.fifo",
		DB:         dbname,
		CountLimit: 1000,
		Level:      LevelError,
	}
	logger, err := New(options)
	if err != nil {
		t.Fatalf("New error: %v", err)
	}
	// level
	logger.WithFields(Fields{"module": "my_module", "version": "my_version1"}).Errorf("error: %v", "1")
	logger.WithFields(Fields{"module": "my_module1", "version": "my_version"}).Warnf("error: %v", "2")
	logger.WithFields(Fields{"module": "my_module1", "version": "my_version"}).Errorf("error: %v", "3")
	logger.WithFields(Fields{"module": "my_module", "version": "my_version1"}).Warnf("error: %v", "4")
	// query all
	rs, err := Query(dbname, Fields{})
	if err != nil {
		t.Fatalf("query error: %v", err)
	}
	if len(rs) != 2 {
		t.Fatalf("query len not match: %v", len(rs))
	}
	// query filter
	rs, err = Query(dbname, Fields{"module": "my_module", "version": "my_version1"})
	if err != nil {
		t.Fatalf("query error: %v", err)
	}
	if len(rs) != 1 {
		t.Fatalf("query len not match: %v", len(rs))
	}
}
% cat test_log.db
{"level":"error","module":"my_module","msg":"error: 1","time":"2022-02-15T16:44:47+08:00","version":"my_version1"}
{"level":"error","module":"my_module1","msg":"error: 3","time":"2022-02-15T16:44:47+08:00","version":"my_version"}

TODO

  • logondemand模式需要确保任何级别均可查看
  • 远程模式:区分客户端,为不同客户端保存不同日志文件
  • 无需写入到本地文件(暂不开放此能力)
  • 用test hook做测试
  • count limit在server端支持

Documentation

Index

Constants

View Source
const (
	LevelDebug = logrus.DebugLevel
	LevelInfo  = logrus.InfoLevel
	LevelWarn  = logrus.WarnLevel
	LevelError = logrus.ErrorLevel
	LevelFatal = logrus.FatalLevel
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Fields

type Fields = logrus.Fields

func Query

func Query(filename string, filter Fields) ([]Fields, error)

type Level

type Level = logrus.Level

type LogServer

type LogServer struct {
	Server string // RPC node name
	// contains filtered or unexported fields
}

func NewLogServer

func NewLogServer(server string) (*LogServer, error)

NewLogServer 创建log server(remote模式)

func (*LogServer) Stop

func (s *LogServer) Stop()

func (*LogServer) Write

func (s *LogServer) Write(req *WriteArgs, reply *Reply) error

type Logger

type Logger struct {
	*logrus.Logger
	// contains filtered or unexported fields
}

func New

func New(options Options) (*Logger, error)

func (*Logger) Close

func (l *Logger) Close()

func (*Logger) Write

func (l *Logger) Write(p []byte) (int, error)

type Options

type Options struct {
	Name         string // name of log client and log file
	CountLimit   int    // remote log count limit
	Level        Level  // remote level, 不影响fifo level
	RemoteServer string // remote log server
}

type Reply

type Reply struct {
}

type WriteArgs

type WriteArgs struct {
	Client     string
	CountLimit int
	Bytes      []byte
}

Jump to

Keyboard shortcuts

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