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
9507e59c
Commit
9507e59c
authored
Jan 12, 2016
by
JessonChan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
camel name fixed
parent
2479e61d
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
36 deletions
+36
-36
file.go
logs/file.go
+36
-36
No files found.
logs/file.go
View file @
9507e59c
...
...
@@ -32,29 +32,29 @@ import (
// It writes messages by lines limit, file size limit, or time frequency.
type
fileLogWriter
struct
{
*
log
.
Logger
mw
*
MuxWriter
mw
*
MuxWriter
// The opened file
Filename
string
`json:"filename"`
Filename
string
`json:"filename"`
Max
l
ines
int
`json:"maxlines"`
max
linesCurl
ines
int
Max
L
ines
int
`json:"maxlines"`
max
LinesCurL
ines
int
// Rotate at size
Max
size
int
`json:"maxsize"`
max
sizeCursize
int
Max
Size
int
`json:"maxsize"`
max
SizeCurSize
int
// Rotate daily
Daily
bool
`json:"daily"`
Max
days
int64
`json:"maxdays"`
dailyOpen
date
int
Daily
bool
`json:"daily"`
Max
Days
int64
`json:"maxdays"`
dailyOpen
Date
int
Rotate
bool
`json:"rotate"`
Rotate
bool
`json:"rotate"`
startLock
sync
.
Mutex
// Only one log can write to the file
startLock
sync
.
Mutex
// Only one log can write to the file
Level
int
`json:"level"`
Level
int
`json:"level"`
Perm
os
.
FileMode
`json:"perm"`
Perm
os
.
FileMode
`json:"perm"`
}
// MuxWriter is an *os.File writer with locker.
...
...
@@ -63,7 +63,7 @@ type MuxWriter struct {
fd
*
os
.
File
}
//
w
rite to os.File.
//
W
rite to os.File.
func
(
l
*
MuxWriter
)
Write
(
b
[]
byte
)
(
int
,
error
)
{
l
.
Lock
()
defer
l
.
Unlock
()
...
...
@@ -82,10 +82,10 @@ func (l *MuxWriter) SetFd(fd *os.File) {
func
newFileWriter
()
Logger
{
w
:=
&
fileLogWriter
{
Filename
:
""
,
Max
l
ines
:
1000000
,
Max
s
ize
:
1
<<
28
,
//256 MB
Max
L
ines
:
1000000
,
Max
S
ize
:
1
<<
28
,
//256 MB
Daily
:
true
,
Max
d
ays
:
7
,
Max
D
ays
:
7
,
Rotate
:
true
,
Level
:
LevelTrace
,
Perm
:
0660
,
...
...
@@ -98,18 +98,18 @@ func newFileWriter() Logger {
}
// Init file logger with json config.
// json
c
onfig like:
// json
C
onfig like:
// {
// "filename":"logs/beego.log",
// "max
l
ines":10000,
// "max
L
ines":10000,
// "maxsize":1<<30,
// "daily":true,
// "max
d
ays":15,
// "max
D
ays":15,
// "rotate":true,
// "perm":0600
//
"perm":0600
// }
func
(
w
*
fileLogWriter
)
Init
(
json
c
onfig
string
)
error
{
err
:=
json
.
Unmarshal
([]
byte
(
json
c
onfig
),
w
)
func
(
w
*
fileLogWriter
)
Init
(
json
C
onfig
string
)
error
{
err
:=
json
.
Unmarshal
([]
byte
(
json
C
onfig
),
w
)
if
err
!=
nil
{
return
err
}
...
...
@@ -130,19 +130,19 @@ func (w *fileLogWriter) startLogger() error {
return
w
.
initFd
()
}
func
(
w
*
fileLogWriter
)
do
c
heck
(
size
int
)
{
func
(
w
*
fileLogWriter
)
do
C
heck
(
size
int
)
{
w
.
startLock
.
Lock
()
defer
w
.
startLock
.
Unlock
()
if
w
.
Rotate
&&
((
w
.
Max
lines
>
0
&&
w
.
maxlinesCurlines
>=
w
.
Maxl
ines
)
||
(
w
.
Max
size
>
0
&&
w
.
maxsizeCursize
>=
w
.
Maxs
ize
)
||
(
w
.
Daily
&&
time
.
Now
()
.
Day
()
!=
w
.
dailyOpen
d
ate
))
{
if
w
.
Rotate
&&
((
w
.
Max
Lines
>
0
&&
w
.
maxLinesCurLines
>=
w
.
MaxL
ines
)
||
(
w
.
Max
Size
>
0
&&
w
.
maxSizeCurSize
>=
w
.
MaxS
ize
)
||
(
w
.
Daily
&&
time
.
Now
()
.
Day
()
!=
w
.
dailyOpen
D
ate
))
{
if
err
:=
w
.
DoRotate
();
err
!=
nil
{
fmt
.
Fprintf
(
os
.
Stderr
,
"FileLogWriter(%q): %s
\n
"
,
w
.
Filename
,
err
)
return
}
}
w
.
max
linesCurl
ines
++
w
.
max
sizeCurs
ize
+=
size
w
.
max
LinesCurL
ines
++
w
.
max
SizeCurS
ize
+=
size
}
// WriteMsg write logger message into file.
...
...
@@ -151,7 +151,7 @@ func (w *fileLogWriter) WriteMsg(msg string, level int) error {
return
nil
}
n
:=
24
+
len
(
msg
)
// 24 stand for the length "2013/06/23 21:00:22 [T] "
w
.
do
c
heck
(
n
)
w
.
do
C
heck
(
n
)
w
.
Logger
.
Println
(
msg
)
return
nil
}
...
...
@@ -168,15 +168,15 @@ func (w *fileLogWriter) initFd() error {
if
err
!=
nil
{
return
fmt
.
Errorf
(
"get stat err: %s
\n
"
,
err
)
}
w
.
max
sizeCurs
ize
=
int
(
finfo
.
Size
())
w
.
dailyOpen
d
ate
=
time
.
Now
()
.
Day
()
w
.
max
linesCurl
ines
=
0
w
.
max
SizeCurS
ize
=
int
(
finfo
.
Size
())
w
.
dailyOpen
D
ate
=
time
.
Now
()
.
Day
()
w
.
max
LinesCurL
ines
=
0
if
finfo
.
Size
()
>
0
{
count
,
err
:=
w
.
lines
()
if
err
!=
nil
{
return
err
}
w
.
max
linesCurl
ines
=
count
w
.
max
LinesCurL
ines
=
count
}
return
nil
}
...
...
@@ -266,7 +266,7 @@ func (w *fileLogWriter) deleteOldLog() {
}
}()
if
!
info
.
IsDir
()
&&
info
.
ModTime
()
.
Unix
()
<
(
time
.
Now
()
.
Unix
()
-
60
*
60
*
24
*
w
.
Max
d
ays
)
{
if
!
info
.
IsDir
()
&&
info
.
ModTime
()
.
Unix
()
<
(
time
.
Now
()
.
Unix
()
-
60
*
60
*
24
*
w
.
Max
D
ays
)
{
if
strings
.
HasPrefix
(
filepath
.
Base
(
path
),
filepath
.
Base
(
w
.
Filename
))
{
os
.
Remove
(
path
)
}
...
...
@@ -275,7 +275,7 @@ func (w *fileLogWriter) deleteOldLog() {
})
}
// Destroy close the file desciption, close file writer.
// Destroy close the file desc
r
iption, close file writer.
func
(
w
*
fileLogWriter
)
Destroy
()
{
w
.
mw
.
fd
.
Close
()
}
...
...
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