提供日志等级类型,修正帮助说明。

Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
rick.chan 2025-04-01 15:03:52 +08:00
parent cb55fecbc6
commit f82e6db6f8
3 changed files with 69 additions and 14 deletions

View File

@ -1,3 +1,44 @@
# Simple Log # Simple Log
A very simple log system(golang). A very simple log system(golang).
## Install
```bash
go get github.com/lion187chen/simplelog
```
## Demo
```go
package main
import (
"time"
"github.com/lion187chen/simplelog"
)
func CreateLog(file string, LogLevel simplelog.LogLevel) *simplelog.SimpleLog {
switch file {
case "":
return new(simplelog.SimpleLog).InitStd(LogLevel, simplelog.Ltime|simplelog.Lfile|simplelog.Llevel)
default:
return new(simplelog.SimpleLog).InitRotating(file, 1024*10, 10, LogLevel)
}
}
func main() {
log := CreateLog("./log/MS.log", simplelog.LevelInfo)
for i := 0; i < 10000000; i++ {
log.Trace("hello world")
log.Debug("hello world")
log.Info("hello world")
log.Warn("hello world")
log.Error("hello world")
log.Fatal("hello world")
time.Sleep(8 * time.Millisecond)
}
}
```

View File

@ -6,12 +6,24 @@ import (
"github.com/lion187chen/simplelog" "github.com/lion187chen/simplelog"
) )
var sl *simplelog.SimpleLog func CreateLog(file string, LogLevel simplelog.LogLevel) *simplelog.SimpleLog {
switch file {
case "":
return new(simplelog.SimpleLog).InitStd(LogLevel, simplelog.Ltime|simplelog.Lfile|simplelog.Llevel)
default:
return new(simplelog.SimpleLog).InitRotating(file, 1024*10, 10, LogLevel)
}
}
func main() { func main() {
sl = new(simplelog.SimpleLog).InitRotating("./log/MS.log", 1024*10, 10, simplelog.LevelTrace) log := CreateLog("./log/MS.log", simplelog.LevelInfo)
for i := 0; i < 10000000; i++ { for i := 0; i < 10000000; i++ {
sl.Debug("hello world") log.Trace("hello world")
log.Debug("hello world")
log.Info("hello world")
log.Warn("hello world")
log.Error("hello world")
log.Fatal("hello world")
time.Sleep(8 * time.Millisecond) time.Sleep(8 * time.Millisecond)
} }
} }

View File

@ -28,9 +28,11 @@ import (
"time" "time"
) )
type LogLevel int
// log level, from low to high, more higher means more serious // log level, from low to high, more higher means more serious
const ( const (
LevelTrace = iota LevelTrace LogLevel = iota
LevelDebug LevelDebug
LevelInfo LevelInfo
LevelWarn LevelWarn
@ -91,8 +93,8 @@ type SimpleLog struct {
// log.Lfile // log.Lfile
// log.Llevel // log.Llevel
// 的组合 // 的组合
func (l *SimpleLog) Init(handler StreamHandler, level, flag int) *SimpleLog { func (l *SimpleLog) Init(handler StreamHandler, level LogLevel, flag int) *SimpleLog {
l.level.Set(level) l.level.Set(int(level))
l.handler = handler l.handler = handler
l.flag = flag l.flag = flag
@ -103,7 +105,7 @@ func (l *SimpleLog) Init(handler StreamHandler, level, flag int) *SimpleLog {
return l return l
} }
func (l *SimpleLog) InitStd(level, flag int) *SimpleLog { func (l *SimpleLog) InitStd(level LogLevel, flag int) *SimpleLog {
handler, e := NewStreamHandle(os.Stdout) handler, e := NewStreamHandle(os.Stdout)
if e != nil { if e != nil {
panic(e) panic(e)
@ -112,7 +114,7 @@ func (l *SimpleLog) InitStd(level, flag int) *SimpleLog {
return l.Init(handler, level, flag) return l.Init(handler, level, flag)
} }
func (l *SimpleLog) InitFile(name string, level, flag int) *SimpleLog { func (l *SimpleLog) InitFile(name string, level LogLevel, flag int) *SimpleLog {
handler, e := new(FileHandler).InitFile(name) handler, e := new(FileHandler).InitFile(name)
if e != nil { if e != nil {
panic(e) panic(e)
@ -121,7 +123,7 @@ func (l *SimpleLog) InitFile(name string, level, flag int) *SimpleLog {
return l.Init(handler, level, flag) return l.Init(handler, level, flag)
} }
func (l *SimpleLog) InitRotating(name string, maxBytes, backupCount, level int) *SimpleLog { func (l *SimpleLog) InitRotating(name string, maxBytes, backupCount int, level LogLevel) *SimpleLog {
handler, e := new(RotatingFileHandler).InitRotating(name, maxBytes, backupCount) handler, e := new(RotatingFileHandler).InitRotating(name, maxBytes, backupCount)
if e != nil { if e != nil {
panic(e) panic(e)
@ -130,7 +132,7 @@ func (l *SimpleLog) InitRotating(name string, maxBytes, backupCount, level int)
return l.Init(handler, level, Ltime|Lfile|Llevel) return l.Init(handler, level, Ltime|Lfile|Llevel)
} }
func (l *SimpleLog) InitTimedRotating(name string, when int8, interval, level int) *SimpleLog { func (l *SimpleLog) InitTimedRotating(name string, when int8, interval int, level LogLevel) *SimpleLog {
handler, e := new(TimedRotatingFileHandler).InitTimedRotating(name, when, interval) handler, e := new(TimedRotatingFileHandler).InitTimedRotating(name, when, interval)
if e != nil { if e != nil {
panic(e) panic(e)
@ -172,8 +174,8 @@ func (l *SimpleLog) Close() {
} }
// set log level, any log level less than it will not log // set log level, any log level less than it will not log
func (l *SimpleLog) SetLevel(level int) { func (l *SimpleLog) SetLevel(level LogLevel) {
l.level.Set(level) l.level.Set(int(level))
} }
// name can be in ["trace", "debug", "info", "warn", "error", "fatal"] // name can be in ["trace", "debug", "info", "warn", "error", "fatal"]
@ -208,13 +210,13 @@ func (l *SimpleLog) SetHandler(h StreamHandler) {
l.hMutex.Unlock() l.hMutex.Unlock()
} }
func (l *SimpleLog) Output(callDepth int, level int, format string, v ...interface{}) { func (l *SimpleLog) Output(callDepth int, level LogLevel, format string, v ...interface{}) {
if l.closed.Get() == 1 { if l.closed.Get() == 1 {
// closed // closed
return return
} }
if l.level.Get() > level { if l.level.Get() > int(level) {
// higher level can be logged // higher level can be logged
return return
} }