log

package
v0.0.0-...-1e957dd Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2019 License: MIT Imports: 11 Imported by: 419

Documentation

Overview

Package log supplies more advanced features than go orign log package.

It supports log different level: trace, debug, info, warn, error, fatal.

It also supports different log handlers which you can log to stdout, file, socket, etc...

Use

import "github.com/siddontang/go-log/log"

//log with different level
log.Info("hello world")
log.Error("hello world")

//create a logger with specified handler
h := NewStreamHandler(os.Stdout)
l := log.NewDefault(h)
l.Info("hello world")

Index

Constants

View Source
const (
	WhenSecond = iota
	WhenMinute
	WhenHour
	WhenDay
)

TimeRotating way

View Source
const (
	Ltime  = 1 << iota // time format "2006/01/02 15:04:05"
	Lfile              // file.go:123
	Llevel             // [Trace|Debug|Info...]
)

Logger flag

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug records the log with debug level

func DebugJson

func DebugJson(body interface{})

Debug records the log with debug level

func Debugf

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

Debugf records the log with debug level

func Debugln

func Debugln(args ...interface{})

Debugln records the log with debug level

func Error

func Error(args ...interface{})

Error records the log with error level

func ErrorJson

func ErrorJson(body interface{})

Error records the log with error level

func Errorf

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

Errorf records the log with error level

func Errorln

func Errorln(args ...interface{})

Errorln records the log with error level

func Fatal

func Fatal(args ...interface{})

Fatal records the log with fatal level and exits

func Fatalf

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

Fatalf records the log with fatal level and exits

func Fatalln

func Fatalln(args ...interface{})

Fatalln records the log with fatal level and exits

func Info

func Info(args ...interface{})

Info records the log with info level

func InfoJson

func InfoJson(body interface{})

Info records the log with info level by json format

func Infof

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

Infof records the log with info level

func Infoln

func Infoln(args ...interface{})

Infoln records the log with info level

func Panic

func Panic(args ...interface{})

Panic records the log with fatal level and panics

func Panicf

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

Panicf records the log with fatal level and panics

func Panicln

func Panicln(args ...interface{})

Panicln records the log with fatal level and panics

func Print

func Print(args ...interface{})

Print records the log with trace level

func PrintJson

func PrintJson(body interface{})

Print records the log with trace level

func Printf

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

Printf records the log with trace level

func Println

func Println(args ...interface{})

Println records the log with trace level

func SetDefaultLogger

func SetDefaultLogger(l *Logger)

SetDefaultLogger changes the global logger

func SetLevel

func SetLevel(level Level)

SetLevel changes the logger level

func SetLevelByName

func SetLevelByName(name string)

SetLevelByName changes the logger level by name

func Warn

func Warn(args ...interface{})

Warn records the log with warn level

func Warnf

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

Warnf records the log with warn level

func Warnln

func Warnln(args ...interface{})

Warnln records the log with warn level

Types

type FileHandler

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

FileHandler writes log to a file.

func NewFileHandler

func NewFileHandler(fileName string, flag int) (*FileHandler, error)

NewFileHandler creates a FileHander

func (*FileHandler) Close

func (h *FileHandler) Close() error

Close implements Handler interface

func (*FileHandler) Write

func (h *FileHandler) Write(b []byte) (n int, err error)

Write implements Handler interface

type Handler

type Handler interface {
	Write(p []byte) (n int, err error)
	Close() error
}

Handler writes logs to somewhere

type Level

type Level int

Level type

const (
	LevelTrace Level = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

Log level, from low to high, more high means more serious

func (Level) String

func (l Level) String() string

String returns level String

type Logger

type Logger struct {
	// TODO: support logger.Contextual
	loggers.Advanced
	// contains filtered or unexported fields
}

Logger is the logger to record log

func New

func New(handler Handler, flag int) *Logger

New creates a logger with specified handler and flag

func NewDefault

func NewDefault(handler Handler) *Logger

NewDefault creates default logger with specified handler and flag: Ltime|Lfile|Llevel

func (*Logger) Close

func (l *Logger) Close()

Close closes the logger

func (*Logger) Debug

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

Debug records the log with debug level

func (*Logger) Debugf

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

Debugf records the log with debug level

func (*Logger) Debugln

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

Debugln records the log with debug level

func (*Logger) Error

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

Error records the log with error level

func (*Logger) Errorf

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

Errorf records the log with error level

func (*Logger) Errorln

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

Errorln records the log with error level

func (*Logger) Fatal

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

Fatal records the log with fatal level and exits

func (*Logger) Fatalf

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

Fatalf records the log with fatal level and exits

func (*Logger) Fatalln

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

Fatalln records the log with fatal level and exits

func (*Logger) Info

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

Info records the log with info level

func (*Logger) Infof

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

Infof records the log with info level

func (*Logger) Infoln

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

Infoln records the log with info level

func (*Logger) Output

func (l *Logger) Output(callDepth int, level Level, msg string)

Output records the log with special callstack depth and log level.

func (*Logger) OutputJson

func (l *Logger) OutputJson(callDepth int, level Level, body interface{})

Output json format records the log with special callstack depth and log level.

func (*Logger) Panic

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

Panic records the log with fatal level and panics

func (*Logger) Panicf

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

Panicf records the log with fatal level and panics

func (*Logger) Panicln

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

Panicln records the log with fatal level and panics

func (*Logger) Print

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

Print records the log with trace level

func (*Logger) Printf

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

Printf records the log with trace level

func (*Logger) Println

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

Println records the log with trace level

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets log level, any log level less than it will not log

func (*Logger) SetLevelByName

func (l *Logger) SetLevelByName(name string)

SetLevelByName sets log level by name

func (*Logger) Warn

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

Warn records the log with warn level

func (*Logger) Warnf

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

Warnf records the log with warn level

func (*Logger) Warnln

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

Warnln records the log with warn level

type NullHandler

type NullHandler struct {
}

NullHandler does nothing, it discards anything.

func NewNullHandler

func NewNullHandler() (*NullHandler, error)

NewNullHandler creates a NullHandler

func (*NullHandler) Close

func (h *NullHandler) Close() error

Close implements Handler interface

func (*NullHandler) Write

func (h *NullHandler) Write(b []byte) (n int, err error)

// Write implements Handler interface

type RotatingFileHandler

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

RotatingFileHandler writes log a file, if file size exceeds maxBytes, it will backup current file and open a new one.

max backup file number is set by backupCount, it will delete oldest if backups too many.

func NewRotatingFileHandler

func NewRotatingFileHandler(fileName string, maxBytes int, backupCount int) (*RotatingFileHandler, error)

NewRotatingFileHandler creates a RotatingFileHandler

func (*RotatingFileHandler) Close

func (h *RotatingFileHandler) Close() error

Close implements Handler interface

func (*RotatingFileHandler) Write

func (h *RotatingFileHandler) Write(p []byte) (n int, err error)

Write implements Handler interface

type StreamHandler

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

StreamHandler writes logs to a specified io Writer, maybe stdout, stderr, etc...

func NewStreamHandler

func NewStreamHandler(w io.Writer) (*StreamHandler, error)

NewStreamHandler creates a StreamHandler

func (*StreamHandler) Close

func (h *StreamHandler) Close() error

Close implements Handler interface

func (*StreamHandler) Write

func (h *StreamHandler) Write(b []byte) (n int, err error)

Write implements Handler interface

type TimeRotatingFileHandler

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

TimeRotatingFileHandler writes log to a file, it will backup current and open a new one, with a period time you sepecified.

refer: http://docs.python.org/2/library/logging.handlers.html. same like python TimedRotatingFileHandler.

func NewTimeRotatingFileHandler

func NewTimeRotatingFileHandler(baseName string, when int8, interval int) (*TimeRotatingFileHandler, error)

NewTimeRotatingFileHandler creates a TimeRotatingFileHandler

func (*TimeRotatingFileHandler) Close

func (h *TimeRotatingFileHandler) Close() error

Close implements Handler interface

func (*TimeRotatingFileHandler) Write

func (h *TimeRotatingFileHandler) Write(b []byte) (n int, err error)

Write implements Handler interface

Jump to

Keyboard shortcuts

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