Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
45355794
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
45355794
编写于
6月 27, 2013
作者:
S
slene
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix logrotate close fd before rename file, add a MuxWriter for Logger
上级
4033692d
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
79 addition
and
33 deletion
+79
-33
log.go
log.go
+79
-33
未找到文件。
log.go
浏览文件 @
45355794
...
...
@@ -14,6 +14,7 @@ import (
type
FileLogWriter
struct
{
*
log
.
Logger
mw
*
MuxWriter
// The opened file
filename
string
...
...
@@ -34,6 +35,24 @@ type FileLogWriter struct {
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
{
w
:=
&
FileLogWriter
{
filename
:
fname
,
...
...
@@ -43,6 +62,10 @@ func NewFileWriter(fname string, rotate bool) *FileLogWriter {
maxdays
:
7
,
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
}
...
...
@@ -71,9 +94,16 @@ func (w *FileLogWriter) SetRotateMaxDays(maxdays int64) *FileLogWriter {
}
func
(
w
*
FileLogWriter
)
StartLogger
()
error
{
if
err
:=
w
.
DoRotate
(
false
);
err
!=
nil
{
fd
,
err
:=
w
.
createLogFile
()
if
err
!=
nil
{
return
err
}
w
.
mw
.
SetFd
(
fd
)
err
=
w
.
initFd
()
if
err
!=
nil
{
return
err
}
BeeLogger
=
w
return
nil
}
...
...
@@ -83,7 +113,7 @@ func (w *FileLogWriter) docheck(size int) {
if
(
w
.
maxlines
>
0
&&
w
.
maxlines_curlines
>=
w
.
maxlines
)
||
(
w
.
maxsize
>
0
&&
w
.
maxsize_cursize
>=
w
.
maxsize
)
||
(
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
)
return
}
...
...
@@ -101,37 +131,14 @@ func (w *FileLogWriter) Printf(format string, v ...interface{}) {
w
.
Logger
.
Printf
(
format
,
v
...
)
}
func
(
w
*
FileLogWriter
)
DoRotate
(
rotate
bool
)
error
{
if
rotate
{
_
,
err
:=
os
.
Lstat
(
w
.
filename
)
if
err
==
nil
{
// file exists
// Find the next available number
num
:=
1
fname
:=
""
for
;
err
==
nil
&&
num
<=
999
;
num
++
{
fname
=
w
.
filename
+
fmt
.
Sprintf
(
".%s.%03d"
,
time
.
Now
()
.
Format
(
"2006-01-02"
),
num
)
_
,
err
=
os
.
Lstat
(
fname
)
}
// return error if the last file checked still existed
if
err
==
nil
{
return
fmt
.
Errorf
(
"Rotate: Cannot find free log number to rename %s
\n
"
,
w
.
filename
)
}
// Rename the file to its newfound home
err
=
os
.
Rename
(
w
.
filename
,
fname
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Rotate: %s
\n
"
,
err
)
}
go
w
.
deleteOldLog
()
}
}
func
(
w
*
FileLogWriter
)
createLogFile
()
(
*
os
.
File
,
error
)
{
// Open the log file
fd
,
err
:=
os
.
OpenFile
(
w
.
filename
,
os
.
O_WRONLY
|
os
.
O_APPEND
|
os
.
O_CREATE
,
0660
)
if
err
!=
nil
{
return
err
}
w
.
Logger
=
log
.
New
(
fd
,
""
,
log
.
Ldate
|
log
.
Ltime
)
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
)
...
...
@@ -144,11 +151,50 @@ func (w *FileLogWriter) DoRotate(rotate bool) error {
fmt
.
Println
(
err
)
}
w
.
maxlines_curlines
=
len
(
strings
.
Split
(
string
(
content
),
"
\n
"
))
}
else
{
w
.
maxlines_curlines
=
0
}
BeeLogger
=
w
return
nil
}
func
(
w
*
FileLogWriter
)
DoRotate
()
error
{
_
,
err
:=
os
.
Lstat
(
w
.
filename
)
if
err
==
nil
{
// file exists
// Find the next available number
num
:=
1
fname
:=
""
for
;
err
==
nil
&&
num
<=
999
;
num
++
{
fname
=
w
.
filename
+
fmt
.
Sprintf
(
".%s.%03d"
,
time
.
Now
()
.
Format
(
"2006-01-02"
),
num
)
_
,
err
=
os
.
Lstat
(
fname
)
}
// return error if the last file checked still existed
if
err
==
nil
{
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
err
=
os
.
Rename
(
w
.
filename
,
fname
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Rotate: %s
\n
"
,
err
)
}
// re-start logger
err
=
w
.
StartLogger
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Rotate StartLogger: %s
\n
"
,
err
)
}
go
w
.
deleteOldLog
()
}
return
nil
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录