log

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: May 20, 2025 License: MIT Imports: 15 Imported by: 45

README

log

Go Report Card go.dev reference

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

Features

  • consolt and filelog support

  • WithFields support

  • 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
2. file shots

output to stdout and file both.

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 split 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 SetTimeFormat added in v1.0.5

func SetTimeFormat(b bool, layout string)

func Warn

func Warn(args ...interface{})

Warn .

func Warnf

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

Warnf .

func WithContext added in v1.0.4

func WithContext(ctx context.Context) *entry

WithContext .

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 ContextParser added in v1.0.4

type ContextParser interface {
	// Parse contains the logic code which indicates how to parse the context.
	Parse(ctx context.Context) interface{}

	// FieldName is field which will be used in log fields.
	FieldName() string
}

ContextParser want to to parse context into `context` field and log into fields. it would be called only when `ctx` it not nil

func DefaultContextParserFunc added in v1.0.4

func DefaultContextParserFunc(parseFunc func(ctx context.Context) interface{}) ContextParser

DefaultContextParserFunc use default field name as output context field name.

func NewContextParserFunc added in v1.0.4

func NewContextParserFunc(f func(ctx context.Context) interface{}, fieldName string) ContextParser

NewContextParserFunc parseFunc `func(ctx context.Context) interface{}` as the primary parameter, fieldName `string` as secondary parameter.

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, string) ([]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() string

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) SetTimeFormat added in v1.0.5

func (l *Logger) SetTimeFormat(b bool, layout string)

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) WithContext added in v1.0.4

func (l *Logger) WithContext(ctx context.Context) *entry

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 WithContextParser added in v1.0.4

func WithContextParser(parser ContextParser) LoggerOption

WithContextParser set a custom ContextParser for parsing context. maybe you want to auto log opentracing traceId, this could help you.

func WithCustomWriter added in v1.0.1

func WithCustomWriter(w io.Writer) LoggerOption

WithCustomWriter using custom writer to log

func WithFieldsSort added in v1.1.0

func WithFieldsSort(sortField bool) LoggerOption

WithFieldsSort print fields in order of fields' keys lexicographical order.

func WithFileLog added in v1.0.0

func WithFileLog(fp 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

WithReportCaller b is a switch to open print caller or not.

func WithStdout added in v1.0.0

func WithStdout(v bool) LoggerOption

WithStdout output to os.Stdout also.

func WithTimeFormat added in v1.0.5

func WithTimeFormat(b bool, layout string) LoggerOption

WithTimeFormat to output time as the layout you want.

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, msg string) ([]byte, error)

Format entry into log

Directories

Path Synopsis
example
file command
stdout command

Jump to

Keyboard shortcuts

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