12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package tyr
- import (
- "log"
- "os"
- )
- // Logger logger
- type Logger struct {
- FP *os.File
- Log *log.Logger
- }
- var (
- deflog *Logger
- )
- // NewDefaultLogger defult logger
- func NewDefaultLogger(path string) (*Logger, error) {
- var err error
- fp := &os.File{}
- if fp, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666); err != nil {
- return nil, err
- }
- deflog = &Logger{}
- deflog.FP = fp
- deflog.Log = log.New(fp, "[INFO]", log.LstdFlags)
- return deflog, err
- }
- // ReopenDefaultLogger re open default logger
- func ReopenDefaultLogger(path string) (*Logger, error) {
- var err error
- fp := &os.File{}
- if fp, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0666); err != nil {
- return nil, err
- }
- if deflog == nil {
- deflog = &Logger{}
- }
- fpo := deflog.FP
- deflog.FP = fp
- deflog.Log = log.New(fp, "[INFO]", log.LstdFlags)
- fpo.Close()
- return deflog, err
- }
- // CloseDefaultLogger close
- func CloseDefaultLogger() error {
- return deflog.FP.Close()
- }
- // LogInfo log info
- func LogInfo(v ...interface{}) {
- deflog.Log.SetPrefix("[INFO]")
- deflog.Log.Println(v...)
- }
- // LogDebug log debug
- func LogDebug(v ...interface{}) {
- deflog.Log.SetPrefix("[DEBUG]")
- deflog.Log.Println(v...)
- }
- // NewLogger new
- func NewLogger(path string) (logger *Logger, err error) {
- fp := &os.File{}
- if fp, err = os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, os.ModePerm); err != nil {
- return
- }
- logger.FP = fp
- logger.Log = log.New(fp, "[INFO]", log.Ldate|log.Ltime)
- return
- }
- // Close close
- func (logger Logger) Close() error {
- return logger.FP.Close()
- }
- // Info log info
- func (logger Logger) Info(v ...interface{}) {
- logger.Log.SetPrefix("[INFO]")
- logger.Log.Println(v...)
- }
- // Debug log debug
- func (logger Logger) Debug(v ...interface{}) {
- logger.Log.SetPrefix("[DEBUG]")
- logger.Log.Println(v...)
- }
|