logging

package module
v0.0.0-...-8a07c47 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2014 License: MIT Imports: 11 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

logging

GoDoc

logging for golang, support log level and multi handler, colorful stdout

features

  • support logging level and level range

  • support handler filter

  • support file handler

  • support time rotation handler

  • support size rotation handler

  • support multi handlers

  • colorful stdout logging(red errors, yellow warnings, green infos)

example

import "logging"

stdout handler:

logging.StdoutHandler.SetLevel(INFO)
logging.Debug("%d, %s", 1, "OK")
logging.Error("%d, %s", 1, "OK")

simple file handler:

l, err := logging.NewSingleFileHandler("/tmp/sf.log")
if err != nil {
	panic(err)
}
logging.AddHandler("file", l)
...

time rotation handler:

l, err := logging.NewTimeRotationHandler("/tmp/tr.log", "060102-15")
if err != nil {
	panic(err)
}
logging.AddHandler("rotation", l)
...

multi handler:

...
logging.StdoutHandler.SetLevel(INFO)
logging.AddHandler("file", file_handler)
logging.AddHandler("rotation", rotation_handler)
...

size rotation handler

l, err := logging.NewSizeRotationHandler("/tmp/sr.log", 1024, 5)
if err != nil {
	panic(err)
}
l.SetLevel(INFO)
logging.AddHandler("sr", l)
...

stdout colorful output

enable:(default)

logging.EnableColorful()

disable:

logging.DisableColorful()

Documentation

Index

Constants

View Source
const (
	DEBUG   logLevel = 1
	INFO    logLevel = 2
	WARNING logLevel = 3
	ERROR   logLevel = 4
	DISABLE logLevel = 255
)
View Source
const (
	DefaultTimeLayout = "2006-01-02 15:04:05"
)

Variables

View Source
var (
	FileCreateFlag             = os.O_CREATE | os.O_APPEND | os.O_WRONLY
	FileCreatePerm os.FileMode = 0640
)
View Source
var DefaultFormat = func(name, timeString string, rd *Record) string {
	return "[" + timeString + "] " + rd.Level.String() + " " + rd.Message + "\n"
}
View Source
var DefaultLogger = NewLogger()

Functions

func AddHandler

func AddHandler(name string, h Emitter)

func Debug

func Debug(format string, values ...interface{})

func DisableColorful

func DisableColorful()

func DisableStdout

func DisableStdout()

func EnableColorful

func EnableColorful()

func EnableStdout

func EnableStdout()

func Error

func Error(format string, values ...interface{})

func Info

func Info(format string, values ...interface{})

func Log

func Log(level logLevel, format string, values ...interface{})

func ResetLogLevel

func ResetLogLevel(level string)

func StringToLogLevel

func StringToLogLevel(s string) logLevel

func Warning

func Warning(format string, values ...interface{})

Types

type Emitter

type Emitter interface {
	Emit(string, *Record)
}

type Handler

type Handler struct {
	// contains filtered or unexported fields
}
var (
	StdoutHandler *Handler
)

func NewHandler

func NewHandler(out io.Writer) *Handler

func (*Handler) Close

func (h *Handler) Close() error

func (*Handler) Emit

func (h *Handler) Emit(name string, rd *Record)

func (*Handler) SetFilter

func (h *Handler) SetFilter(f func(*Record) bool)

func (*Handler) SetFormat

func (h *Handler) SetFormat(format func(string, string, *Record) string)

func (*Handler) SetLevel

func (h *Handler) SetLevel(level logLevel)

func (*Handler) SetLevelRange

func (h *Handler) SetLevelRange(minLevel, maxLevel logLevel)

func (*Handler) SetLevelRangeString

func (h *Handler) SetLevelRangeString(smin, smax string)

func (*Handler) SetLevelString

func (h *Handler) SetLevelString(s string)

func (*Handler) SetTimeLayout

func (h *Handler) SetTimeLayout(layout string)

type Logger

type Logger struct {
	Name     string
	Handlers map[string]Emitter
}

func NewLogger

func NewLogger() *Logger

func (*Logger) AddHandler

func (l *Logger) AddHandler(name string, h Emitter)

func (*Logger) Debug

func (l *Logger) Debug(format string, values ...interface{})

func (*Logger) Error

func (l *Logger) Error(format string, values ...interface{})

func (*Logger) Info

func (l *Logger) Info(format string, values ...interface{})

func (*Logger) Log

func (l *Logger) Log(level logLevel, format string, values ...interface{})

func (*Logger) ResetLogLevel

func (l *Logger) ResetLogLevel(level string)

func (*Logger) Warning

func (l *Logger) Warning(format string, values ...interface{})

type Record

type Record struct {
	Time       time.Time
	Level      logLevel
	Message    string
	LoggerName string
}

type SingleFileHandler

type SingleFileHandler struct {
	*Handler
}

func NewSingleFileHandler

func NewSingleFileHandler(file string) (*SingleFileHandler, error)

type SizeRotationHandler

type SizeRotationHandler struct {
	*Handler
	// contains filtered or unexported fields
}

func NewSizeRotationHandler

func NewSizeRotationHandler(fn string, size uint64, count uint32) (*SizeRotationHandler, error)

type TimeRotationHandler

type TimeRotationHandler struct {
	*Handler
	// contains filtered or unexported fields
}

func NewTimeRotationHandler

func NewTimeRotationHandler(shortfile string, suffix string) (*TimeRotationHandler, error)

Jump to

Keyboard shortcuts

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