1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 19:30:55 +00:00

fix bug with file permission in log module

This commit is contained in:
nullne 2016-05-14 10:54:09 +08:00
parent dae2a36eb5
commit d679a4b865
2 changed files with 34 additions and 5 deletions

View File

@ -22,6 +22,7 @@ import (
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -53,7 +54,8 @@ type fileLogWriter struct {
Level int `json:"level"` Level int `json:"level"`
Perm os.FileMode `json:"perm"` Perm string `json:"perm"`
perm os.FileMode
fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix fileNameOnly, suffix string // like "project.log", project is fileNameOnly and .log is suffix
} }
@ -65,7 +67,8 @@ func newFileWriter() Logger {
MaxDays: 7, MaxDays: 7,
Rotate: true, Rotate: true,
Level: LevelTrace, Level: LevelTrace,
Perm: 0660, Perm: "0660",
perm: 0660,
} }
return w return w
} }
@ -89,6 +92,11 @@ func (w *fileLogWriter) Init(jsonConfig string) error {
if len(w.Filename) == 0 { if len(w.Filename) == 0 {
return errors.New("jsonconfig must have filename") return errors.New("jsonconfig must have filename")
} }
perm, err := strconv.ParseInt(w.Perm, 8, 64)
if err != nil {
return err
}
w.perm = os.FileMode(perm)
w.suffix = filepath.Ext(w.Filename) w.suffix = filepath.Ext(w.Filename)
w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix) w.fileNameOnly = strings.TrimSuffix(w.Filename, w.suffix)
if w.suffix == "" { if w.suffix == "" {
@ -153,7 +161,7 @@ func (w *fileLogWriter) WriteMsg(when time.Time, msg string, level int) error {
func (w *fileLogWriter) createLogFile() (*os.File, error) { func (w *fileLogWriter) createLogFile() (*os.File, error) {
// Open the log file // Open the log file
fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.Perm) fd, err := os.OpenFile(w.Filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, w.perm)
return fd, err return fd, err
} }

View File

@ -24,6 +24,27 @@ import (
"time" "time"
) )
func TestFilePerm(t *testing.T) {
log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log", "perm": "0600"}`)
log.Debug("debug")
log.Informational("info")
log.Notice("notice")
log.Warning("warning")
log.Error("error")
log.Alert("alert")
log.Critical("critical")
log.Emergency("emergency")
file, err := os.Stat("test.log")
if err != nil {
t.Fatal(err)
}
if file.Mode() != 0600 {
t.Fatal("unexpected log file permission")
}
os.Remove("test.log")
}
func TestFile1(t *testing.T) { func TestFile1(t *testing.T) {
log := NewLogger(10000) log := NewLogger(10000)
log.SetLogger("file", `{"filename":"test.log"}`) log.SetLogger("file", `{"filename":"test.log"}`)
@ -147,7 +168,7 @@ func testFileRotate(t *testing.T, fn1, fn2 string) {
MaxDays: 7, MaxDays: 7,
Rotate: true, Rotate: true,
Level: LevelTrace, Level: LevelTrace,
Perm: 0660, Perm: "0660",
} }
fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1))
fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) fw.dailyOpenTime = time.Now().Add(-24 * time.Hour)
@ -170,7 +191,7 @@ func testFileDailyRotate(t *testing.T, fn1, fn2 string) {
MaxDays: 7, MaxDays: 7,
Rotate: true, Rotate: true,
Level: LevelTrace, Level: LevelTrace,
Perm: 0660, Perm: "0660",
} }
fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1)) fw.Init(fmt.Sprintf(`{"filename":"%v","maxdays":1}`, fn1))
fw.dailyOpenTime = time.Now().Add(-24 * time.Hour) fw.dailyOpenTime = time.Now().Add(-24 * time.Hour)