Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
45355794
Commit
45355794
authored
Jun 27, 2013
by
slene
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix logrotate close fd before rename file, add a MuxWriter for Logger
parent
4033692d
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
72 additions
and
26 deletions
+72
-26
log.go
log.go
+72
-26
No files found.
log.go
View file @
45355794
...
@@ -14,6 +14,7 @@ import (
...
@@ -14,6 +14,7 @@ import (
type
FileLogWriter
struct
{
type
FileLogWriter
struct
{
*
log
.
Logger
*
log
.
Logger
mw
*
MuxWriter
// The opened file
// The opened file
filename
string
filename
string
...
@@ -34,6 +35,24 @@ type FileLogWriter struct {
...
@@ -34,6 +35,24 @@ type FileLogWriter struct {
startLock
sync
.
Mutex
//only one log can writer to the file
startLock
sync
.
Mutex
//only one log can writer to the file
}
}
type
MuxWriter
struct
{
sync
.
Mutex
fd
*
os
.
File
}
func
(
l
*
MuxWriter
)
Write
(
b
[]
byte
)
(
int
,
error
)
{
l
.
Lock
()
defer
l
.
Unlock
()
return
l
.
fd
.
Write
(
b
)
}
func
(
l
*
MuxWriter
)
SetFd
(
fd
*
os
.
File
)
{
if
l
.
fd
!=
nil
{
l
.
fd
.
Close
()
}
l
.
fd
=
fd
}
func
NewFileWriter
(
fname
string
,
rotate
bool
)
*
FileLogWriter
{
func
NewFileWriter
(
fname
string
,
rotate
bool
)
*
FileLogWriter
{
w
:=
&
FileLogWriter
{
w
:=
&
FileLogWriter
{
filename
:
fname
,
filename
:
fname
,
...
@@ -43,6 +62,10 @@ func NewFileWriter(fname string, rotate bool) *FileLogWriter {
...
@@ -43,6 +62,10 @@ func NewFileWriter(fname string, rotate bool) *FileLogWriter {
maxdays
:
7
,
maxdays
:
7
,
rotate
:
rotate
,
rotate
:
rotate
,
}
}
// use MuxWriter instead direct use os.File for lock write when rotate
w
.
mw
=
new
(
MuxWriter
)
// set MuxWriter as Logger's io.Writer
w
.
Logger
=
log
.
New
(
w
.
mw
,
""
,
log
.
Ldate
|
log
.
Ltime
)
return
w
return
w
}
}
...
@@ -71,9 +94,16 @@ func (w *FileLogWriter) SetRotateMaxDays(maxdays int64) *FileLogWriter {
...
@@ -71,9 +94,16 @@ func (w *FileLogWriter) SetRotateMaxDays(maxdays int64) *FileLogWriter {
}
}
func
(
w
*
FileLogWriter
)
StartLogger
()
error
{
func
(
w
*
FileLogWriter
)
StartLogger
()
error
{
if
err
:=
w
.
DoRotate
(
false
);
err
!=
nil
{
fd
,
err
:=
w
.
createLogFile
()
if
err
!=
nil
{
return
err
return
err
}
}
w
.
mw
.
SetFd
(
fd
)
err
=
w
.
initFd
()
if
err
!=
nil
{
return
err
}
BeeLogger
=
w
return
nil
return
nil
}
}
...
@@ -83,7 +113,7 @@ func (w *FileLogWriter) docheck(size int) {
...
@@ -83,7 +113,7 @@ func (w *FileLogWriter) docheck(size int) {
if
(
w
.
maxlines
>
0
&&
w
.
maxlines_curlines
>=
w
.
maxlines
)
||
if
(
w
.
maxlines
>
0
&&
w
.
maxlines_curlines
>=
w
.
maxlines
)
||
(
w
.
maxsize
>
0
&&
w
.
maxsize_cursize
>=
w
.
maxsize
)
||
(
w
.
maxsize
>
0
&&
w
.
maxsize_cursize
>=
w
.
maxsize
)
||
(
w
.
daily
&&
time
.
Now
()
.
Day
()
!=
w
.
daily_opendate
)
{
(
w
.
daily
&&
time
.
Now
()
.
Day
()
!=
w
.
daily_opendate
)
{
if
err
:=
w
.
DoRotate
(
true
);
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
return
}
}
...
@@ -101,8 +131,33 @@ func (w *FileLogWriter) Printf(format string, v ...interface{}) {
...
@@ -101,8 +131,33 @@ func (w *FileLogWriter) Printf(format string, v ...interface{}) {
w
.
Logger
.
Printf
(
format
,
v
...
)
w
.
Logger
.
Printf
(
format
,
v
...
)
}
}
func
(
w
*
FileLogWriter
)
DoRotate
(
rotate
bool
)
error
{
func
(
w
*
FileLogWriter
)
createLogFile
()
(
*
os
.
File
,
error
)
{
if
rotate
{
// Open the log file
fd
,
err
:=
os
.
OpenFile
(
w
.
filename
,
os
.
O_WRONLY
|
os
.
O_APPEND
|
os
.
O_CREATE
,
0660
)
return
fd
,
err
}
func
(
w
*
FileLogWriter
)
initFd
()
error
{
fd
:=
w
.
mw
.
fd
finfo
,
err
:=
fd
.
Stat
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"get stat err: %s
\n
"
,
err
)
}
w
.
maxsize_cursize
=
int
(
finfo
.
Size
())
w
.
daily_opendate
=
time
.
Now
()
.
Day
()
if
finfo
.
Size
()
>
0
{
content
,
err
:=
ioutil
.
ReadFile
(
w
.
filename
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
}
w
.
maxlines_curlines
=
len
(
strings
.
Split
(
string
(
content
),
"
\n
"
))
}
else
{
w
.
maxlines_curlines
=
0
}
return
nil
}
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
...
@@ -117,38 +172,29 @@ func (w *FileLogWriter) DoRotate(rotate bool) error {
...
@@ -117,38 +172,29 @@ func (w *FileLogWriter) DoRotate(rotate bool) error {
return
fmt
.
Errorf
(
"Rotate: Cannot find free log number to rename %s
\n
"
,
w
.
filename
)
return
fmt
.
Errorf
(
"Rotate: Cannot find free log number to rename %s
\n
"
,
w
.
filename
)
}
}
// block Logger's io.Writer
w
.
mw
.
Lock
()
defer
w
.
mw
.
Unlock
()
fd
:=
w
.
mw
.
fd
fd
.
Close
()
// close fd before rename
// Rename the file to its newfound home
// Rename the file to its newfound home
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
)
}
}
go
w
.
deleteOldLog
()
}
}
// Open the log file
// re-start logger
fd
,
err
:=
os
.
OpenFile
(
w
.
filename
,
os
.
O_WRONLY
|
os
.
O_APPEND
|
os
.
O_CREATE
,
0660
)
err
=
w
.
StartLogger
(
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
fmt
.
Errorf
(
"Rotate StartLogger: %s
\n
"
,
err
)
}
}
w
.
Logger
=
log
.
New
(
fd
,
""
,
log
.
Ldate
|
log
.
Ltime
)
finfo
,
err
:=
fd
.
Stat
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"get stat err: %s
\n
"
,
err
)
}
w
.
maxsize_cursize
=
int
(
finfo
.
Size
())
w
.
daily_opendate
=
time
.
Now
()
.
Day
()
if
finfo
.
Size
()
>
0
{
content
,
err
:=
ioutil
.
ReadFile
(
w
.
filename
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
}
w
.
maxlines_curlines
=
len
(
strings
.
Split
(
string
(
content
),
"
\n
"
))
}
else
{
go
w
.
deleteOldLog
()
w
.
maxlines_curlines
=
0
}
}
BeeLogger
=
w
return
nil
return
nil
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment