迁移项目。
Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
parent
4473d46af6
commit
ac9c772fb6
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
.vscode/settings.json
|
11
demo/main.go
Normal file
11
demo/main.go
Normal 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)
|
||||||
|
}
|
@ -7,6 +7,16 @@ import (
|
|||||||
"time"
|
"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.
|
// FileHandler writes log to a file.
|
||||||
type FileHandler struct {
|
type FileHandler struct {
|
||||||
fd *os.File
|
fd *os.File
|
||||||
@ -48,6 +58,25 @@ type RotatingFileHandler struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *RotatingFileHandler) InitRotating(name string, maxBytes int, backupCount int) (*RotatingFileHandler, error) {
|
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)
|
dir := path.Dir(name)
|
||||||
os.MkdirAll(dir, 0777)
|
os.MkdirAll(dir, 0777)
|
||||||
|
|
||||||
@ -106,6 +135,10 @@ func (h *RotatingFileHandler) doRollover() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h._doRollover()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *RotatingFileHandler) _doRollover() {
|
||||||
if h.backupCount > 0 {
|
if h.backupCount > 0 {
|
||||||
h.fd.Close()
|
h.fd.Close()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user