log

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: MIT Imports: 13 Imported by: 45

README

log

Go Report Card go.dev reference

a lite golang log library, easy to get start and no dependency.

Features

[x] consolt and filelog support

[x] WithFields support

[x] lite and easy to use

Install

go get -u github.com/yeqown/log 

Quick Start

There is sample code of using log.

package main

type embed struct {
	FieldA string
	FieldB int
}

func main() {
	// using builtin logger
	log.Info(1, 2, 3, 4, 5)
	log.Infof("this is format: %d", 2)

	log.
		WithField("key1", "value1").
		WithFields(log.Fields{
			"key2": "value2",
			"key3": "value3",
			"key4": "value4",
			"key5": "value5",
			"key6": "value6",
			"key7": "value7",
			"key8": "value8",
		}).Error("test error")

	// using new logger
	logger, _ := log.NewLogger(
		log.WithLevel(log.LevelError),
		log.WithGlobalFields(log.Fields{"global_key": "global_value"}),
	)
	logger.Info(1, 2, 3, 4, 5)
	logger.Infof("this is format: %d", 2)
	logger.WithField("logger", "it's me").
		WithFields(log.Fields{
			"key2": "value2",
			"key3": "value3",
			"key4": "value4",
			"key5": "value5",
			"key6": "value6",
			"key7": "value7",
			"embed": embed{
				FieldA: "aaa",
				FieldB: 112091,
			},
			"embed_ptr": &embed{
				FieldA: "aaa",
				FieldB: 112091,
			},
		}).Error("test error")
    // [Error] file="/Users/yeqown/projects/opensource/log/logger_entry.go" fn="github.com/yeqown/log.(*entry).output" 
    // line="109" timestamp="1596090798" formatted_time="2020-07-30T14:33:18+08:00" embed="{aaa 112091}" 
    // embed_ptr="&{aaa 112091}" global_key="global_value" key2="value2" logger="it's me" msg="test error"
}

Migrate

Here is a broken change from d68941c to v1.x. v1.x is advised to use.

shots

Here are some shots of using example.

1. stdout shots

shot1

2. file shots

output to stdout and file both.

shot2

Documentation

Overview

Package log .

this log is inspired by `https://github.com/silenceper/log` and `https://github.com/sirupsen/logrus` 1. can be set to output to file 2. log file can be splitted into files day by day, just like `app.20060102.log`

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug .

func Debugf

func Debugf(format string, args ...interface{})

Debugf .

func Error

func Error(args ...interface{})

Error .

func Errorf

func Errorf(format string, args ...interface{})

Errorf .

func Fatal

func Fatal(args ...interface{})

Fatal .

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf .

func GetCallerForTest added in v1.0.0

func GetCallerForTest() *runtime.Frame

only use for test

func Info

func Info(args ...interface{})

Info .

func Infof

func Infof(format string, args ...interface{})

Infof .

func SetCallerReporter added in v1.0.3

func SetCallerReporter(b bool)

func SetLogLevel

func SetLogLevel(level Level)

SetLogLevel .

func Warn

func Warn(args ...interface{})

Warn .

func Warnf

func Warnf(format string, args ...interface{})

Warnf .

func WithField added in v1.0.0

func WithField(key string, value interface{}) *entry

WithField .

func WithFields added in v1.0.0

func WithFields(fields Fields) *entry

WithFields .

Types

type Fields added in v1.0.0

type Fields map[string]interface{}

Fields to contains a batch field to log

type Formatter added in v1.0.0

type Formatter interface {
	Format(*entry) ([]byte, error)
}

Formatter to format entry fields and other field

type Level

type Level uint

Level of log

const (
	// LevelFatal .
	LevelFatal Level = iota
	// LevelError .
	LevelError
	// LevelWarning .
	LevelWarning
	// LevelInfo .
	LevelInfo
	// LevelDebug .
	LevelDebug
)

func (Level) Color added in v1.0.0

func (lv Level) Color() int

func (Level) String added in v1.0.0

func (lv Level) String() string

type Logger added in v1.0.0

type Logger struct {
	// contains filtered or unexported fields
}

func NewLogger

func NewLogger(opts ...LoggerOption) (*Logger, error)

NewLogger using os.Stdout and LevelDebug to print log

func (*Logger) Debug added in v1.0.0

func (l *Logger) Debug(args ...interface{})

func (*Logger) Debugf added in v1.0.0

func (l *Logger) Debugf(format string, args ...interface{})

func (*Logger) Error added in v1.0.0

func (l *Logger) Error(args ...interface{})

func (*Logger) Errorf added in v1.0.0

func (l *Logger) Errorf(format string, args ...interface{})

func (*Logger) Fatal added in v1.0.0

func (l *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf added in v1.0.0

func (l *Logger) Fatalf(format string, args ...interface{})

func (*Logger) Info added in v1.0.0

func (l *Logger) Info(args ...interface{})

func (*Logger) Infof added in v1.0.0

func (l *Logger) Infof(format string, args ...interface{})

func (*Logger) SetCallerReporter added in v1.0.3

func (l *Logger) SetCallerReporter(b bool)

func (*Logger) SetLogLevel added in v1.0.0

func (l *Logger) SetLogLevel(level Level)

func (*Logger) Warn added in v1.0.0

func (l *Logger) Warn(args ...interface{})

func (*Logger) Warnf added in v1.0.0

func (l *Logger) Warnf(format string, args ...interface{})

func (*Logger) WithField added in v1.0.0

func (l *Logger) WithField(key string, value interface{}) *entry

func (*Logger) WithFields added in v1.0.0

func (l *Logger) WithFields(fields Fields) *entry

type LoggerOption added in v1.0.0

type LoggerOption func(lo *options) error

LoggerOption to apply single function into `lo`

func WithCustomWriter added in v1.0.1

func WithCustomWriter(w io.Writer) LoggerOption

WithCustomWriter using custom writer to log

func WithFileLog added in v1.0.0

func WithFileLog(file string, autoRotate bool) LoggerOption

WithFileLog store log into file, if autoRotate is set, it will start a goroutine to split log file by day. TODO(@yeqown): using time round instead of ticker

func WithGlobalFields added in v1.0.0

func WithGlobalFields(fields Fields) LoggerOption

WithGlobalFields set global fields those would be logged in every log.

func WithLevel added in v1.0.0

func WithLevel(lv Level) LoggerOption

WithLevel setting the level, this could change dynamic

func WithReportCaller added in v1.0.3

func WithReportCaller(b bool) LoggerOption

func WithStdout added in v1.0.0

func WithStdout(v bool) LoggerOption

WithStdout output to os.Stdout this only affect when file log is opening

type TextFormatter added in v1.0.0

type TextFormatter struct {
	// contains filtered or unexported fields
}

func (*TextFormatter) Format added in v1.0.0

func (f *TextFormatter) Format(e *entry) ([]byte, error)

Format entry into log

Directories

Path Synopsis
example
file-log command
stdout-log command

Jump to

Keyboard shortcuts

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