log

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

README

log

A log framework for Golang.

GitHub release codecov GoReport License

Documentation

Index

Constants

View Source
const (
	LogLevelUnspecified int = iota
	LogLevelTrace
	LogLevelDebug
	LogLevelInfo
	LogLevelWarn
	LogLevelError
	LogLevelFatal
)

UNSPECIFIED means no log level

View Source
const (
	EnvLogLevelTrace = "TRACE"
	EnvLogLevelDebug = "DEBUG"
	EnvLogLevelInfo  = "INFO"
	EnvLogLevelWarn  = "WARN"
	EnvLogLevelError = "ERROR"
	EnvLogLevelFatal = "FATAL"
)
View Source
const (
	// CallPath is The depth of a function is called
	CallPathDepth1  = 1
	CallPathDepth2  = 2
	CallPathDepth3  = 3
	CallPathDefault = CallPathDepth3
)
View Source
const (
	// Color refers to if
	ColorOn  = true
	ColorOff = false

	// Default LogLevel
	LogLevelDefault = LogLevelInfo

	// Local is the default time zone
	LocationLocal = "Local"

	// TimeFormatDefault is The default format of time
	TimeFormatDefault = "2006-01-02 15:04:05.0000"

	// DefaultLogFile is the default log file path
	DefaultLogFile = "./access.log"
)

Variables

View Source
var LogLevelMap = map[int]string{
	LogLevelUnspecified: "UNSPECIFIED",
	LogLevelTrace:       "TRACE",
	LogLevelDebug:       "DEBUG",
	LogLevelInfo:        "INFO",
	LogLevelWarn:        "WARN",
	LogLevelError:       "ERROR",
	LogLevelFatal:       "FATAL",
}

LogLevelMap is log level map

Functions

func Debugf

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

Debugf print debug level logs in a specific format

func Debugln

func Debugln(v ...interface{})

Debugln print debug level logs in a line

func Errorf

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

Errorf print error level logs in a specific format

func Errorln

func Errorln(v ...interface{})

Errorln print error level logs in a line

func Fatalf

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

Fatalf print fatal level logs in a specific format

func Fatalln

func Fatalln(v ...interface{})

Fatalln print fatal level logs in a line

func Infof

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

Infof print info level logs in a specific format

func Infoln

func Infoln(v ...interface{})

Infoln print info level logs in a line

func NewDefaultLogger

func NewDefaultLogger()

NewDefaultLogger returns a instance of Logger with default configurations

func SetCallPath

func SetCallPath(caller int)

SetCallPath set caller path

func SetDefaultLogFile

func SetDefaultLogFile()

func SetFormatter

func SetFormatter(f Formatter)

func SetLevel

func SetLevel(lv string)

SetLevel set log level by name

func SetLogFile

func SetLogFile(path string)

func Tracef

func Tracef(format string, v ...interface{})

Tracef print trace level logs in a specific format

func Traceln

func Traceln(v ...interface{})

Traceln print trace level logs in a line

func Warnf

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

Warnf print warn level logs in a specific format

func Warnln

func Warnln(v ...interface{})

Warnln print warn level logs in a line

Types

type Context

type Context map[string]string

type Entry

type Entry struct {

	// Ctx context.Context
	Context Context
	// contains filtered or unexported fields
}

func NewEntry

func NewEntry() *Entry

func SetContext

func SetContext(ctx Context) *Entry

func (*Entry) Debugf

func (e *Entry) Debugf(format string, v ...interface{})

Debugf print debug level logs in a specific format

func (*Entry) Debugln

func (e *Entry) Debugln(v ...interface{})

Debugln print debug level logs in a line

func (*Entry) Errorf

func (e *Entry) Errorf(format string, v ...interface{})

Errorf print error level logs in a specific format

func (*Entry) Errorln

func (e *Entry) Errorln(v ...interface{})

Errorln print error level logs in a line

func (*Entry) Fatalf

func (e *Entry) Fatalf(format string, v ...interface{})

Fatalf print fatal level logs in a specific format

func (*Entry) Fatalln

func (e *Entry) Fatalln(v ...interface{})

Fatalln print fatal level logs in a line

func (*Entry) Infof

func (e *Entry) Infof(format string, v ...interface{})

Infof print info level logs in a specific format

func (*Entry) Infoln

func (e *Entry) Infoln(v ...interface{})

Infoln print info level logs in a line

func (*Entry) Tracef

func (e *Entry) Tracef(format string, v ...interface{})

Tracef print trace level logs in a specific format

func (*Entry) Traceln

func (e *Entry) Traceln(v ...interface{})

Traceln print trace level logs in a line

func (*Entry) Warnf

func (e *Entry) Warnf(format string, v ...interface{})

Warnf print warn level logs in a specific format

func (*Entry) Warnln

func (e *Entry) Warnln(v ...interface{})

Warnln print warn level logs in a line

func (*Entry) WithContext

func (e *Entry) WithContext(ctx Context) *Entry

type Fields

type Fields struct {
	Timestamp string
	Level     string
	Msg       string
	Func      string
	File      string
	Line      int

	Context
}

type Formatter

type Formatter interface {
	Print(fields *Fields, ctx Context) string
	SetColor(color bool)
}

Formatter will decide how logs are printed Default consist of: * TextFormatter, print as "deployment=kubestar namespace=default msg=deployment not found" * JSONFormatter, print as "{"deployment": "kubestar", "namespace": "default", "msg": "deployment not found"}"

type JSONFormatter

type JSONFormatter struct {
	Color bool
}

func (*JSONFormatter) Print

func (t *JSONFormatter) Print(fields *Fields, ctx Context) string

func (*JSONFormatter) SetColor

func (t *JSONFormatter) SetColor(color bool)

type Logger

type Logger struct {
	Writer io.Writer

	Level    int
	CallPath int
	Async    bool
	// contains filtered or unexported fields
}

Logger defines a general logger which could write specific logs

func NewLogger

func NewLogger(writer io.Writer, level, caller int) *Logger

NewLogger returns a instance of Logger

func (*Logger) Debugf

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

Debugf print debug level logs in a specific format

func (*Logger) Debugln

func (l *Logger) Debugln(v ...interface{})

Debugln print debug level logs in a line

func (*Logger) EnableAsync

func (l *Logger) EnableAsync() *Logger

func (*Logger) Errorf

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

Errorf print error level logs in a specific format

func (*Logger) Errorln

func (l *Logger) Errorln(v ...interface{})

Errorln print error level logs in a line

func (*Logger) Fatalf

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

Fatalf print fatal level logs in a specific format

func (*Logger) Fatalln

func (l *Logger) Fatalln(v ...interface{})

Fatalln print fatal level logs in a line

func (*Logger) Infof

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

Infof print info level logs in a specific format

func (*Logger) Infoln

func (l *Logger) Infoln(v ...interface{})

Infoln print info level logs in a line

func (*Logger) SetCallPath

func (l *Logger) SetCallPath(callPath int)

SetCallPath set caller path

func (*Logger) SetContext

func (l *Logger) SetContext(ctx Context) *Entry

func (*Logger) SetFormatter

func (l *Logger) SetFormatter(f Formatter) *Logger

func (*Logger) SetLevel

func (l *Logger) SetLevel(level int)

SetLevel set the level of log

func (*Logger) SetLevelByName

func (l *Logger) SetLevelByName(level string)

SetLevelByName set the log level by name

func (*Logger) Tracef

func (l *Logger) Tracef(format string, v ...interface{})

Tracef print trace level logs in a specific format

func (*Logger) Traceln

func (l *Logger) Traceln(v ...interface{})

Traceln print trace level logs in a line

func (*Logger) Warnf

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

Warnf print warn level logs in a specific format

func (*Logger) Warnln

func (l *Logger) Warnln(v ...interface{})

Warnln print warn level logs in a line

type TextFormatter

type TextFormatter struct {
	Color bool
}

func (*TextFormatter) Print

func (t *TextFormatter) Print(fields *Fields, ctx Context) string

func (*TextFormatter) SetColor

func (t *TextFormatter) SetColor(color bool)

Jump to

Keyboard shortcuts

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