From: bruce Date: Fri, 26 Apr 2024 02:50:52 +0000 (+0000) Subject: feat(components): 1, add NewWriter to create a logger from IO for X-Git-Tag: v0.2.5 X-Git-Url: https://git.uzoombox.com/git/?a=commitdiff_plain;h=61a25da2f6cbc07af7b46a74560e61849292d5fe;p=uzsdk.git feat(components): 1, add NewWriter to create a logger from IO for Logger. 2, add method Filepath to return log's filepath for Logger. 3, rename method IOWriter to Writer in Logger. --- diff --git a/uzsdk/logger/logger.go b/uzsdk/logger/logger.go index 30cd55a..84a1fd7 100644 --- a/uzsdk/logger/logger.go +++ b/uzsdk/logger/logger.go @@ -6,11 +6,9 @@ import ( "bytes" "fmt" "io" - "log" "os" "path/filepath" "runtime" - "sync" "time" ) @@ -25,21 +23,23 @@ const ( type Logger struct { // params filepath string + writer *os.File level int prefix string - maxline int64 - - // status - mutex sync.Mutex - out *os.File - linenum int64 } // New creates a new Logger, the path is the filepath of logfile, // logv is an string in ['fatal','error','warn','info','debug'], -// prefix is the content write after timestamp and before message, -// maxline[default: 100000] create a new logfile if linenum exceed it. +// prefix is the content write after timestamp and before message. func New(path, logv, prefix string, options ...interface{}) *Logger { + var writer *os.File + if path != "" { + writer, _ = os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) + } + if writer == nil { + writer = os.Stdout + } + level := Info switch logv { case "debug": @@ -54,32 +54,50 @@ func New(path, logv, prefix string, options ...interface{}) *Logger { level = Fatal } - maxline := int64(100000) - for _, option := range options { - switch option.(type) { - case int64: - maxline = option.(int64) - } + return &Logger{ + filepath: writer.Name(), + writer: writer, + level: level, + prefix: prefix, } +} - linenum := int64(0) - if path != "" { - if r, err := os.Open(path); err == nil { - linenum, _ = lineCounter(r) - } +// NewWriter creates a new Logger, the writer is the *os.File of logfile, +// logv is an string in ['fatal','error','warn','info','debug'], +// prefix is the content write after timestamp and before message. +func NewWriter(writer *os.File, logv, prefix string, options ...interface{}) *Logger { + if writer == nil { + writer = os.Stdout } + + level := Info + switch logv { + case "debug": + level = Debug + case "info": + level = Info + case "warn": + level = Warn + case "error": + level = Error + case "fatal": + level = Fatal + } + return &Logger{ - filepath: path, + filepath: writer.Name(), + writer: writer, level: level, prefix: prefix, - maxline: maxline, - out: nil, - linenum: linenum, } } -func (l *Logger) IOWriter() *os.File { - return l.out +func (l *Logger) Filepath() string { + return l.filepath +} + +func (l *Logger) Writer() *os.File { + return l.writer } // Fatal calls output(Fatal, Sprint(v)) to print the log. @@ -146,40 +164,9 @@ func (l *Logger) Output(level int, ltype, msg string) { } text := fmt.Sprintf("[%v] %v %v %v:%v: %v\n", ltype, timestamp, l.prefix, filepath.Base(file), line, msg) - l.mutex.Lock() - defer l.mutex.Unlock() - - // make a new logfile. - if l.maxline <= l.linenum { - if l.filepath != "" { - if l.out != nil { - l.out.Close() - l.out = nil - } - newname := fmt.Sprintf("%v.%v", l.filepath, time.Now().Format("20060102")) - if err := os.Rename(l.filepath, newname); err == nil { - l.linenum = 0 - } - } else { - l.linenum = 0 - } - } - if l.out == nil { - if l.filepath != "" { - l.out, _ = os.OpenFile(l.filepath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - } else { - l.out = os.Stdout - } - } - if l.out == nil { - log.Println("logger's output is nil") - } - // write to logfile. buf := []byte(text) - if _, err := l.out.Write(buf); err == nil { - l.linenum++ - } + l.writer.Write(buf) } func lineCounter(r io.Reader) (int64, error) { diff --git a/version.go b/version.go index 602740c..61e98f8 100644 --- a/version.go +++ b/version.go @@ -66,4 +66,4 @@ require ( package uzsdk // Version is the current opensdk's version. -const Version = "0.2.4" +const Version = "0.2.5"