mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 23:01:28 +00:00
code format
This commit is contained in:
parent
5511e03b52
commit
baa2e9d64a
31
logs/file.go
31
logs/file.go
@ -131,17 +131,20 @@ func (w *fileLogWriter) startLogger() error {
|
|||||||
|
|
||||||
func (w *fileLogWriter) doCheck(size int) {
|
func (w *fileLogWriter) doCheck(size int) {
|
||||||
w.startLock.Lock()
|
w.startLock.Lock()
|
||||||
defer w.startLock.Unlock()
|
if w.Rotate {
|
||||||
if w.Rotate && ((w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) ||
|
if (w.MaxLines > 0 && w.maxLinesCurLines >= w.MaxLines) ||
|
||||||
(w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) ||
|
(w.MaxSize > 0 && w.maxSizeCurSize >= w.MaxSize) ||
|
||||||
(w.Daily && time.Now().Day() != w.dailyOpenDate)) {
|
(w.Daily && time.Now().Day() != w.dailyOpenDate) {
|
||||||
if err := w.DoRotate(); err != nil {
|
if err := w.DoRotate(); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
|
fmt.Fprintf(os.Stderr, "FileLogWriter(%q): %s\n", w.Filename, err)
|
||||||
return
|
w.startLock.Unlock()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
w.maxLinesCurLines++
|
w.maxLinesCurLines++
|
||||||
w.maxSizeCurSize += size
|
w.maxSizeCurSize += size
|
||||||
|
w.startLock.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteMsg write logger message into file.
|
// WriteMsg write logger message into file.
|
||||||
@ -163,14 +166,14 @@ func (w *fileLogWriter) createLogFile() (*os.File, error) {
|
|||||||
|
|
||||||
func (w *fileLogWriter) initFd() error {
|
func (w *fileLogWriter) initFd() error {
|
||||||
fd := w.fd
|
fd := w.fd
|
||||||
finfo, err := fd.Stat()
|
fInfo, err := fd.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("get stat err: %s\n", err)
|
return fmt.Errorf("get stat err: %s\n", err)
|
||||||
}
|
}
|
||||||
w.maxSizeCurSize = int(finfo.Size())
|
w.maxSizeCurSize = int(fInfo.Size())
|
||||||
w.dailyOpenDate = time.Now().Day()
|
w.dailyOpenDate = time.Now().Day()
|
||||||
w.maxLinesCurLines = 0
|
w.maxLinesCurLines = 0
|
||||||
if finfo.Size() > 0 {
|
if fInfo.Size() > 0 {
|
||||||
count, err := w.lines()
|
count, err := w.lines()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -211,7 +214,8 @@ func (w *fileLogWriter) lines() (int, error) {
|
|||||||
// new file name like xx.2013-01-01.2.log
|
// new file name like xx.2013-01-01.2.log
|
||||||
func (w *fileLogWriter) DoRotate() error {
|
func (w *fileLogWriter) DoRotate() error {
|
||||||
_, err := os.Lstat(w.Filename)
|
_, err := os.Lstat(w.Filename)
|
||||||
if err == nil { // file exists
|
if err == nil {
|
||||||
|
// file exists
|
||||||
// Find the next available number
|
// Find the next available number
|
||||||
num := 1
|
num := 1
|
||||||
fName := ""
|
fName := ""
|
||||||
@ -237,7 +241,7 @@ func (w *fileLogWriter) DoRotate() error {
|
|||||||
fd.Close()
|
fd.Close()
|
||||||
|
|
||||||
// close fd before rename
|
// close fd before rename
|
||||||
// Rename the file to its newfound home
|
// Rename the file to its new found name
|
||||||
err = os.Rename(w.Filename, fName)
|
err = os.Rename(w.Filename, fName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Rotate: %s\n", err)
|
return fmt.Errorf("Rotate: %s\n", err)
|
||||||
@ -260,8 +264,7 @@ func (w *fileLogWriter) deleteOldLog() {
|
|||||||
filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
|
filepath.Walk(dir, func(path string, info os.FileInfo, err error) (returnErr error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
returnErr = fmt.Errorf("Unable to delete old log '%s', error: %+v", path, r)
|
fmt.Fprintf(os.Stderr, "Unable to delete old log '%s', error: %+v", path, r)
|
||||||
fmt.Println(returnErr)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user