迁移项目。

Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
rick.chan 2025-04-01 13:54:55 +08:00
parent 4473d46af6
commit ac9c772fb6
4 changed files with 46 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.vscode/settings.json

11
demo/main.go Normal file
View File

@ -0,0 +1,11 @@
package main
import (
"github.com/lion187chen/simplelog"
)
var sl *simplelog.SimpleLog
func main() {
sl = new(simplelog.SimpleLog).InitRotating("./log/MS.log", 1024*1024*2, 100, simplelog.LevelTrace)
}

View File

@ -7,6 +7,16 @@ import (
"time"
)
func isFileExist(filename string) bool {
info, e := os.Stat(filename)
if e == nil {
return !info.IsDir()
} else {
return os.IsExist(e)
}
}
// FileHandler writes log to a file.
type FileHandler struct {
fd *os.File
@ -48,6 +58,25 @@ type RotatingFileHandler struct {
}
func (h *RotatingFileHandler) InitRotating(name string, maxBytes int, backupCount int) (*RotatingFileHandler, error) {
if isFileExist(name) {
var err error
h.fd, err = os.OpenFile(name, os.O_CREATE|os.O_RDONLY, 0666)
if err != nil {
return nil, err
}
f, err := h.fd.Stat()
if err != nil {
h.fd.Close()
return nil, err
}
if f.Size() > 0 {
h._doRollover()
return h, nil
} else {
h.fd.Close()
}
}
dir := path.Dir(name)
os.MkdirAll(dir, 0777)
@ -106,6 +135,10 @@ func (h *RotatingFileHandler) doRollover() {
return
}
h._doRollover()
}
func (h *RotatingFileHandler) _doRollover() {
if h.backupCount > 0 {
h.fd.Close()

2
go.mod
View File

@ -1,3 +1,3 @@
module blacktea.cpolar.cn/OrgGo/simplelog
module github.com/lion187chen/simplelog
go 1.21.0