log

package
Version: v0.0.0-...-bdc7756 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

log package 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 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")
l.Infof("%s %d", "hello", 123)

Index

Constants

View Source
const (
	WhenSecond = iota
	WhenMinute
	WhenHour
	WhenDay
)
View Source
const (
	LevelTrace = iota
	LevelDebug
	LevelInfo
	LevelWarn
	LevelError
	LevelFatal
)

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

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

Variables

View Source
var LevelName [6]string = [6]string{"Trace", "Debug", "Info", "Warn", "Error", "Fatal"}

Functions

func Debug

func Debug(v ...interface{})

func Debugf

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

func Error

func Error(v ...interface{})

func Errorf

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

func Fatal

func Fatal(v ...interface{})

func Fatalf

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

func Info

func Info(v ...interface{})

func Infof

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

func SetHandler

func SetHandler(h Handler)

func SetLevel

func SetLevel(level int)

func SetLevelByName

func SetLevelByName(name string)

name can be in ["trace", "debug", "info", "warn", "error", "fatal"]

func Trace

func Trace(v ...interface{})

func Tracef

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

func Warn

func Warn(v ...interface{})

func Warnf

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

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)

func (*FileHandler) Close

func (h *FileHandler) Close() error

func (*FileHandler) Write

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

type Handler

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

Handler writes logs to somewhere

type Logger

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

func New

func New(handler Handler, flag int) *Logger

new a logger with specified handler and flag

func NewDefault

func NewDefault(handler Handler) *Logger

new a default logger with specified handler and flag: Ltime|Lfile|Llevel

func (*Logger) Close

func (l *Logger) Close()

func (*Logger) Debug

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

log with Debug level

func (*Logger) Debugf

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

log with Debug level

func (*Logger) Error

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

log with error level

func (*Logger) Errorf

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

log with error level

func (*Logger) Fatal

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

log with fatal level

func (*Logger) Fatalf

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

log with fatal level

func (*Logger) Info

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

log with info level

func (*Logger) Infof

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

log with info level

func (*Logger) Output

func (l *Logger) Output(callDepth int, level int, format string, v ...interface{})

func (*Logger) SetHandler

func (l *Logger) SetHandler(h Handler)

func (*Logger) SetLevel

func (l *Logger) SetLevel(level int)

set log level, any log level less than it will not log

func (*Logger) SetLevelByName

func (l *Logger) SetLevelByName(name string)

name can be in ["trace", "debug", "info", "warn", "error", "fatal"]

func (*Logger) Trace

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

log with Trace level

func (*Logger) Tracef

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

log with Trace level

func (*Logger) Warn

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

log with warn level

func (*Logger) Warnf

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

log with warn level

type NullHandler

type NullHandler struct {
}

NullHandler does nothing, it discards anything.

func NewNullHandler

func NewNullHandler() (*NullHandler, error)

func (*NullHandler) Close

func (h *NullHandler) Close()

func (*NullHandler) Write

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

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)

func (*RotatingFileHandler) Close

func (h *RotatingFileHandler) Close() error

func (*RotatingFileHandler) Write

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

type SocketHandler

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

SocketHandler writes log to a connectionl. Network protocol is simple: log length + log | log length + log. log length is uint32, bigendian. you must implement your own log server, maybe you can use logd instead simply.

func NewSocketHandler

func NewSocketHandler(protocol string, addr string) (*SocketHandler, error)

func (*SocketHandler) Close

func (h *SocketHandler) Close() error

func (*SocketHandler) Write

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

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)

func (*StreamHandler) Close

func (h *StreamHandler) Close() error

func (*StreamHandler) Write

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

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)

func (*TimeRotatingFileHandler) Close

func (h *TimeRotatingFileHandler) Close() error

func (*TimeRotatingFileHandler) Write

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL