golog

package module
v0.0.0-...-d59ac6d Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2015 License: MIT Imports: 9 Imported by: 8

README

#golog

simple log library for Golang

##case

package main

import (
	"fmt"
	. "github.com/flike/golog"
	"os"
)

func main() {
	path := "/tmp/test_log"
	os.RemoveAll(path)
	os.Mkdir(path, 0777)
	fileName := path + "/test.log"

	h, err := NewRotatingFileHandler(fileName, 1024*1024, 2)
	if err != nil {
		fmt.Println(err.Error())
	}
	//GlobalLogger is a global variable
	GlobalLogger = New(h, Lfile|Ltime|Llevel)
	GlobalLogger.SetLevel(LevelTrace)
	args1 := "go"
	args2 := "log"
	args3 := "golog"
	//the log will record into the file(/tmp/test_log/test.log)
	Debug("Mode", "main", "OK", 0, "args1", args1, "args2", args2, "args3", args3)
	GlobalLogger.Close()
}

Documentation

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 Close

func Close()

func Debug

func Debug(module string, method string, msg string, reqId int64, args ...interface{})

func Error

func Error(module string, method string, msg string, reqId int64, args ...interface{})

func Fatal

func Fatal(module string, method string, msg string, reqId int64, args ...interface{})

func GetLevel

func GetLevel() int

func Info

func Info(module string, method string, msg string, reqId int64, args ...interface{})

func SetLevel

func SetLevel(level int)

func Trace

func Trace(module string, method string, msg string, reqId int64, args ...interface{})

func Warn

func Warn(module string, method string, msg string, reqId int64, args ...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 {
	sync.Mutex
	// contains filtered or unexported fields
}
var GlobalLogger *Logger = StdLogger()

全局变量

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 StdLogger

func StdLogger() *Logger

func (*Logger) Close

func (l *Logger) Close()

func (*Logger) Level

func (l *Logger) Level() int

func (*Logger) Output

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

a low interface, maybe you can use it for your special log format but it may be not exported later......

func (*Logger) SetLevel

func (l *Logger) SetLevel(level int)

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

type NullHandler

type NullHandler struct {
}

NullHandler does nothing, it discards anything.

func NewNullHandler

func NewNullHandler() (*NullHandler, error)

func (*NullHandler) Close

func (h *NullHandler) Close() error

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 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
y or Y : Canonical URL