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
22617aeb
Commit
22617aeb
authored
Apr 12, 2016
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
file rotate name fixed
parent
9c400778
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
20 additions
and
9 deletions
+20
-9
file.go
logs/file.go
+20
-9
No files found.
logs/file.go
View file @
22617aeb
...
...
@@ -47,6 +47,7 @@ type fileLogWriter struct {
Daily
bool
`json:"daily"`
MaxDays
int64
`json:"maxdays"`
dailyOpenDate
int
dailyOpenTime
time
.
Time
Rotate
bool
`json:"rotate"`
...
...
@@ -163,7 +164,8 @@ func (w *fileLogWriter) initFd() error {
return
fmt
.
Errorf
(
"get stat err: %s
\n
"
,
err
)
}
w
.
maxSizeCurSize
=
int
(
fInfo
.
Size
())
w
.
dailyOpenDate
=
time
.
Now
()
.
Day
()
w
.
dailyOpenTime
=
time
.
Now
()
w
.
dailyOpenDate
=
w
.
dailyOpenTime
.
Day
()
w
.
maxLinesCurLines
=
0
if
fInfo
.
Size
()
>
0
{
count
,
err
:=
w
.
lines
()
...
...
@@ -205,22 +207,29 @@ func (w *fileLogWriter) lines() (int, error) {
// DoRotate means it need to write file in new file.
// new file name like xx.2013-01-01.log (daily) or xx.001.log (by line or size)
func
(
w
*
fileLogWriter
)
doRotate
(
logTime
time
.
Time
)
error
{
_
,
err
:=
os
.
Lstat
(
w
.
Filename
)
if
err
!=
nil
{
return
err
}
// file exists
// Find the next available number
num
:=
1
fName
:=
""
_
,
err
:=
os
.
Lstat
(
w
.
Filename
)
if
err
!=
nil
{
//even if the file is not exist or other ,we should RESTART the logger
goto
RESTART_LOGGER
}
if
w
.
MaxLines
>
0
||
w
.
MaxSize
>
0
{
for
;
err
==
nil
&&
num
<=
999
;
num
++
{
fName
=
w
.
fileNameOnly
+
fmt
.
Sprintf
(
".%s.%03d%s"
,
logTime
.
Format
(
"2006-01-02"
),
num
,
w
.
suffix
)
_
,
err
=
os
.
Lstat
(
fName
)
}
}
else
{
fName
=
fmt
.
Sprintf
(
"%s.%s%s"
,
w
.
fileNameOnly
,
log
Time
.
Format
(
"2006-01-02"
),
w
.
suffix
)
fName
=
fmt
.
Sprintf
(
"%s.%s%s"
,
w
.
fileNameOnly
,
w
.
dailyOpen
Time
.
Format
(
"2006-01-02"
),
w
.
suffix
)
_
,
err
=
os
.
Lstat
(
fName
)
for
;
err
==
nil
&&
num
<=
999
;
num
++
{
fName
=
w
.
fileNameOnly
+
fmt
.
Sprintf
(
".%s.%03d%s"
,
w
.
dailyOpenTime
.
Format
(
"2006-01-02"
),
num
,
w
.
suffix
)
_
,
err
=
os
.
Lstat
(
fName
)
}
}
// return error if the last file checked still existed
if
err
==
nil
{
...
...
@@ -232,16 +241,18 @@ func (w *fileLogWriter) doRotate(logTime time.Time) error {
// Rename the file to its new found name
// even if occurs error,we MUST guarantee to restart new logger
renameErr
:
=
os
.
Rename
(
w
.
Filename
,
fName
)
err
=
os
.
Rename
(
w
.
Filename
,
fName
)
// re-start logger
RESTART_LOGGER
:
startLoggerErr
:=
w
.
startLogger
()
go
w
.
deleteOldLog
()
if
startLoggerErr
!=
nil
{
return
fmt
.
Errorf
(
"Rotate StartLogger: %s
\n
"
,
startLoggerErr
)
}
if
renameE
rr
!=
nil
{
return
fmt
.
Errorf
(
"Rotate: %s
\n
"
,
renameE
rr
)
if
e
rr
!=
nil
{
return
fmt
.
Errorf
(
"Rotate: %s
\n
"
,
e
rr
)
}
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