| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 | package log/*	log module is used to write log info to log file	open a log file when log was created, and close it when log was destroyed*/import (	"fmt"	go_log "log"	"os"	"time")type Log struct {	Level int	//File of log	File *os.File	path string}const (	LOG_LEVEL_DEBUG = 0	LOG_LEVEL_INFO  = 1	LOG_LEVEL_WARN  = 2	LOG_LEVEL_ERROR = 3)func (l *Log) Debug(format string, stdout bool, v ...interface{}) {	if l.Level <= LOG_LEVEL_DEBUG {		l.writeLog("DEBUG", format, stdout, v...)	}}func (l *Log) Info(format string, stdout bool, v ...interface{}) {	if l.Level <= LOG_LEVEL_INFO {		l.writeLog("INFO", format, stdout, v...)	}}func (l *Log) Warn(format string, stdout bool, v ...interface{}) {	if l.Level <= LOG_LEVEL_WARN {		l.writeLog("WARN", format, stdout, v...)	}}func (l *Log) Error(format string, stdout bool, v ...interface{}) {	if l.Level <= LOG_LEVEL_ERROR {		l.writeLog("ERROR", format, stdout, v...)	}}func (l *Log) Panic(format string, stdout bool, v ...interface{}) {	l.writeLog("PANIC", format, stdout, v...)	panic("")}func (l *Log) writeLog(level string, format string, stdout bool, v ...interface{}) {	//if the next day is coming, reopen file	if time.Now().Format("/2006-01-02.log") != l.File.Name() {		l.File.Close()		l.OpenFile()	}	//test if file is closed	if l.File == nil {		//open file		err := l.OpenFile()		if err != nil {			panic(err)		}	}	//write log	format = fmt.Sprintf("["+level+"]"+format, v...)	if show_log && stdout {		logger.Output(4, format)	}	_, err := l.File.Write([]byte(format + "\n"))	if err != nil {		//reopen file		l.File.Close()		l.OpenFile()	}}func (l *Log) SetLogLevel(level int) {	l.Level = level}func (l *Log) OpenFile() error {	//test if file is closed	if l.File == nil {		//open file		file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)		if err != nil {			return err		}		l.File = file	}	//test if file is writable	_, err := l.File.Write([]byte(" "))	if err != nil {		//reopen file		l.File.Close()		file, err := os.OpenFile(l.path+time.Now().Format("/2006-01-02.log"), os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)		if err != nil {			return err		}		l.File = file	}	return nil}func NewLog(path string) (*Log, error) {	if path == "" {		path = "log"	}	//test if path is exist	_, err := os.Stat(path)	if err != nil {		//create path		err = os.MkdirAll(path, 0777)		if err != nil {			return nil, err		}	}	// test if path is a directory	fileInfo, err := os.Stat(path)	if err != nil {		return nil, err	}	if !fileInfo.IsDir() {		return nil, fmt.Errorf("log file path %s is not a directory", path)	}	log := &Log{		Level: LOG_LEVEL_DEBUG,		path:  path,	}	//open file	err = log.OpenFile()	if err != nil {		return nil, err	}	return log, nil}func init() {	// why logger will cause panic when call	initlog()}func initlog() {	var err error	main_log, err = NewLog("./logs")	if err != nil {		panic(err)	}}var main_log *Log // wapper of go_logvar show_log bool = truevar logger = go_log.New(os.Stdout, "", go_log.Ldate|go_log.Ltime|go_log.Lshortfile)func SetShowLog(show bool) {	show_log = show}func SetLogLevel(level int) {	if main_log == nil {		initlog()	}	main_log.SetLogLevel(level)}func Debug(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Debug(format, true, v...)}func Info(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Info(format, true, v...)}func Warn(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Warn(format, true, v...)}func Error(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Error(format, true, v...)}func Panic(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Panic(format, true, v...)}func SlientDebug(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Debug(format, false, v...)}func SlientInfo(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Info(format, false, v...)}func SlientWarn(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Warn(format, false, v...)}func SlientError(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Error(format, false, v...)}func SlientPanic(format string, v ...interface{}) {	if main_log == nil {		initlog()	}	main_log.Panic(format, false, v...)}
 |