123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package myth
- import (
- "io"
- "log"
- "os"
- )
- type Logger struct {
- logger *log.Logger
- }
- var (
- defLog *Logger
- defFP *os.File
- )
- 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 = NewLogger(fp)
- defFP = fp
- return defLog, nil
- }
- 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 = NewLogger(fp)
- }
- fpo := defFP
- defFP = fp
- defLog.logger = log.New(fp, "[INFO]", log.LstdFlags)
- fpo.Close()
- return defLog, nil
- }
- func CloseDefaultLogger() error {
- return defFP.Close()
- }
- func LogDebug(v ...interface{}) {
- defLog.logger.SetPrefix("[DEBUG]")
- defLog.logger.Println(v...)
- }
- func LogInfo(v ...interface{}) {
- defLog.logger.SetPrefix("[INFO]")
- defLog.logger.Println(v...)
- }
- func LogWarn(v ...interface{}) {
- defLog.logger.SetPrefix("[Warn]")
- defLog.logger.Println(v...)
- }
- func LogErr(v ...interface{}) {
- defLog.logger.SetPrefix("[Err]")
- defLog.logger.Println(v...)
- }
- func LogAlert(v ...interface{}) {
- defLog.logger.SetPrefix("[Alert]")
- defLog.logger.Println(v...)
- }
- func LogPanic(v ...interface{}) {
- defLog.logger.SetPrefix("[Panic]")
- defLog.logger.Println(v...)
- }
- func NewLogger(out io.Writer) *Logger {
- logger := log.New(out, "[INFO]", log.Ldate|log.Ltime)
- return &Logger{logger: logger}
- }
- func (l *Logger) Info(v ...interface{}) {
- l.logger.SetPrefix("[INFO]")
- l.logger.Println(v...)
- }
- func (l *Logger) Debug(v ...interface{}) {
- l.logger.SetPrefix("[DEBUG]")
- l.logger.Println(v...)
- }
- func (l *Logger) Warn(v ...interface{}) {
- l.logger.SetPrefix("[Warn]")
- l.logger.Println(v...)
- }
- func (l *Logger) Err(v ...interface{}) {
- l.logger.SetPrefix("[Err]")
- l.logger.Println(v...)
- }
- func (l *Logger) Alert(v ...interface{}) {
- l.logger.SetPrefix("[Alert]")
- l.logger.Println(v...)
- }
- func (l *Logger) Panic(v ...interface{}) {
- l.logger.SetPrefix("[Panic]")
- l.logger.Println(v...)
- }
|