Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
67b9c635
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,发现更多精彩内容 >>
提交
67b9c635
编写于
12月 19, 2013
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #390 from 1fei/master
更新logs的初始化部分
上级
c9ccb9cc
b87e122a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
64 addition
and
132 deletion
+64
-132
logs/conn.go
logs/conn.go
+13
-29
logs/console.go
logs/console.go
+4
-8
logs/file.go
logs/file.go
+30
-51
logs/smtp.go
logs/smtp.go
+17
-44
未找到文件。
logs/conn.go
浏览文件 @
67b9c635
...
...
@@ -10,45 +10,29 @@ import (
type
ConnWriter
struct
{
lg
*
log
.
Logger
innerWriter
io
.
WriteCloser
reconnectOnMsg
bool
reconnect
bool
net
string
addr
string
level
int
ReconnectOnMsg
bool
`json:"reconnectOnMsg"`
Reconnect
bool
`json:"reconnect"`
Net
string
`json:"net"`
Addr
string
`json:"addr"`
Level
int
`json:"level"`
}
func
NewConn
()
LoggerInterface
{
conn
:=
new
(
ConnWriter
)
conn
.
l
evel
=
LevelTrace
conn
.
L
evel
=
LevelTrace
return
conn
}
func
(
c
*
ConnWriter
)
Init
(
jsonconfig
string
)
error
{
var
m
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
&
m
)
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
c
)
if
err
!=
nil
{
return
err
}
if
rom
,
ok
:=
m
[
"reconnectOnMsg"
];
ok
{
c
.
reconnectOnMsg
=
rom
.
(
bool
)
}
if
rc
,
ok
:=
m
[
"reconnect"
];
ok
{
c
.
reconnect
=
rc
.
(
bool
)
}
if
nt
,
ok
:=
m
[
"net"
];
ok
{
c
.
net
=
nt
.
(
string
)
}
if
addr
,
ok
:=
m
[
"addr"
];
ok
{
c
.
addr
=
addr
.
(
string
)
}
if
lv
,
ok
:=
m
[
"level"
];
ok
{
c
.
level
=
int
(
lv
.
(
float64
))
}
return
nil
}
func
(
c
*
ConnWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
c
.
l
evel
{
if
level
<
c
.
L
evel
{
return
nil
}
if
c
.
neddedConnectOnMsg
()
{
...
...
@@ -58,7 +42,7 @@ func (c *ConnWriter) WriteMsg(msg string, level int) error {
}
}
if
c
.
r
econnectOnMsg
{
if
c
.
R
econnectOnMsg
{
defer
c
.
innerWriter
.
Close
()
}
c
.
lg
.
Println
(
msg
)
...
...
@@ -82,7 +66,7 @@ func (c *ConnWriter) connect() error {
c
.
innerWriter
=
nil
}
conn
,
err
:=
net
.
Dial
(
c
.
net
,
c
.
a
ddr
)
conn
,
err
:=
net
.
Dial
(
c
.
Net
,
c
.
A
ddr
)
if
err
!=
nil
{
return
err
}
...
...
@@ -97,8 +81,8 @@ func (c *ConnWriter) connect() error {
}
func
(
c
*
ConnWriter
)
neddedConnectOnMsg
()
bool
{
if
c
.
r
econnect
{
c
.
r
econnect
=
false
if
c
.
R
econnect
{
c
.
R
econnect
=
false
return
true
}
...
...
@@ -106,7 +90,7 @@ func (c *ConnWriter) neddedConnectOnMsg() bool {
return
true
}
return
c
.
r
econnectOnMsg
return
c
.
R
econnectOnMsg
}
func
init
()
{
...
...
logs/console.go
浏览文件 @
67b9c635
...
...
@@ -8,30 +8,26 @@ import (
type
ConsoleWriter
struct
{
lg
*
log
.
Logger
level
int
Level
int
`json:"level"`
}
func
NewConsole
()
LoggerInterface
{
cw
:=
new
(
ConsoleWriter
)
cw
.
lg
=
log
.
New
(
os
.
Stdout
,
""
,
log
.
Ldate
|
log
.
Ltime
)
cw
.
l
evel
=
LevelTrace
cw
.
L
evel
=
LevelTrace
return
cw
}
func
(
c
*
ConsoleWriter
)
Init
(
jsonconfig
string
)
error
{
var
m
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
&
m
)
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
c
)
if
err
!=
nil
{
return
err
}
if
lv
,
ok
:=
m
[
"level"
];
ok
{
c
.
level
=
int
(
lv
.
(
float64
))
}
return
nil
}
func
(
c
*
ConsoleWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
c
.
l
evel
{
if
level
<
c
.
L
evel
{
return
nil
}
c
.
lg
.
Println
(
msg
)
...
...
logs/file.go
浏览文件 @
67b9c635
...
...
@@ -18,25 +18,25 @@ type FileLogWriter struct {
*
log
.
Logger
mw
*
MuxWriter
// The opened file
filename
string
Filename
string
`json:"filename"`
maxlines
int
Maxlines
int
`json:"maxlines"`
maxlines_curlines
int
// Rotate at size
maxsize
int
Maxsize
int
`json:"maxsize"`
maxsize_cursize
int
// Rotate daily
daily
bool
maxdays
int64
Daily
bool
`json:"daily"`
Maxdays
int64
`json:"maxdays`
daily_opendate
int
rotate
bool
Rotate
bool
`json:"rotate"`
startLock
sync
.
Mutex
// Only one log can write to the file
level
int
Level
int
`json:"level"`
}
type
MuxWriter
struct
{
...
...
@@ -59,13 +59,13 @@ func (l *MuxWriter) SetFd(fd *os.File) {
func
NewFileWriter
()
LoggerInterface
{
w
:=
&
FileLogWriter
{
f
ilename
:
""
,
m
axlines
:
1000000
,
m
axsize
:
1
<<
28
,
//256 MB
d
aily
:
true
,
m
axdays
:
7
,
r
otate
:
true
,
l
evel
:
LevelTrace
,
F
ilename
:
""
,
M
axlines
:
1000000
,
M
axsize
:
1
<<
28
,
//256 MB
D
aily
:
true
,
M
axdays
:
7
,
R
otate
:
true
,
L
evel
:
LevelTrace
,
}
// use MuxWriter instead direct use os.File for lock write when rotate
w
.
mw
=
new
(
MuxWriter
)
...
...
@@ -84,33 +84,12 @@ func NewFileWriter() LoggerInterface {
// "rotate":true
//}
func
(
w
*
FileLogWriter
)
Init
(
jsonconfig
string
)
error
{
var
m
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
&
m
)
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
w
)
if
err
!=
nil
{
return
err
}
if
fn
,
ok
:=
m
[
"filename"
];
!
ok
{
if
len
(
w
.
Filename
)
==
0
{
return
errors
.
New
(
"jsonconfig must have filename"
)
}
else
{
w
.
filename
=
fn
.
(
string
)
}
if
ml
,
ok
:=
m
[
"maxlines"
];
ok
{
w
.
maxlines
=
int
(
ml
.
(
float64
))
}
if
ms
,
ok
:=
m
[
"maxsize"
];
ok
{
w
.
maxsize
=
int
(
ms
.
(
float64
))
}
if
dl
,
ok
:=
m
[
"daily"
];
ok
{
w
.
daily
=
dl
.
(
bool
)
}
if
md
,
ok
:=
m
[
"maxdays"
];
ok
{
w
.
maxdays
=
int64
(
md
.
(
float64
))
}
if
rt
,
ok
:=
m
[
"rotate"
];
ok
{
w
.
rotate
=
rt
.
(
bool
)
}
if
lv
,
ok
:=
m
[
"level"
];
ok
{
w
.
level
=
int
(
lv
.
(
float64
))
}
err
=
w
.
StartLogger
()
return
err
...
...
@@ -132,11 +111,11 @@ func (w *FileLogWriter) StartLogger() error {
func
(
w
*
FileLogWriter
)
docheck
(
size
int
)
{
w
.
startLock
.
Lock
()
defer
w
.
startLock
.
Unlock
()
if
(
w
.
maxlines
>
0
&&
w
.
maxlines_curlines
>=
w
.
m
axlines
)
||
(
w
.
maxsize
>
0
&&
w
.
maxsize_cursize
>=
w
.
m
axsize
)
||
(
w
.
d
aily
&&
time
.
Now
()
.
Day
()
!=
w
.
daily_opendate
)
{
if
(
w
.
Maxlines
>
0
&&
w
.
maxlines_curlines
>=
w
.
M
axlines
)
||
(
w
.
Maxsize
>
0
&&
w
.
maxsize_cursize
>=
w
.
M
axsize
)
||
(
w
.
D
aily
&&
time
.
Now
()
.
Day
()
!=
w
.
daily_opendate
)
{
if
err
:=
w
.
DoRotate
();
err
!=
nil
{
fmt
.
Fprintf
(
os
.
Stderr
,
"FileLogWriter(%q): %s
\n
"
,
w
.
f
ilename
,
err
)
fmt
.
Fprintf
(
os
.
Stderr
,
"FileLogWriter(%q): %s
\n
"
,
w
.
F
ilename
,
err
)
return
}
}
...
...
@@ -145,7 +124,7 @@ func (w *FileLogWriter) docheck(size int) {
}
func
(
w
*
FileLogWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
w
.
l
evel
{
if
level
<
w
.
L
evel
{
return
nil
}
n
:=
24
+
len
(
msg
)
// 24 stand for the length "2013/06/23 21:00:22 [T] "
...
...
@@ -156,7 +135,7 @@ func (w *FileLogWriter) WriteMsg(msg string, level int) error {
func
(
w
*
FileLogWriter
)
createLogFile
()
(
*
os
.
File
,
error
)
{
// Open the log file
fd
,
err
:=
os
.
OpenFile
(
w
.
f
ilename
,
os
.
O_WRONLY
|
os
.
O_APPEND
|
os
.
O_CREATE
,
0660
)
fd
,
err
:=
os
.
OpenFile
(
w
.
F
ilename
,
os
.
O_WRONLY
|
os
.
O_APPEND
|
os
.
O_CREATE
,
0660
)
return
fd
,
err
}
...
...
@@ -169,7 +148,7 @@ func (w *FileLogWriter) initFd() error {
w
.
maxsize_cursize
=
int
(
finfo
.
Size
())
w
.
daily_opendate
=
time
.
Now
()
.
Day
()
if
finfo
.
Size
()
>
0
{
content
,
err
:=
ioutil
.
ReadFile
(
w
.
f
ilename
)
content
,
err
:=
ioutil
.
ReadFile
(
w
.
F
ilename
)
if
err
!=
nil
{
return
err
}
...
...
@@ -181,18 +160,18 @@ func (w *FileLogWriter) initFd() error {
}
func
(
w
*
FileLogWriter
)
DoRotate
()
error
{
_
,
err
:=
os
.
Lstat
(
w
.
f
ilename
)
_
,
err
:=
os
.
Lstat
(
w
.
F
ilename
)
if
err
==
nil
{
// file exists
// Find the next available number
num
:=
1
fname
:=
""
for
;
err
==
nil
&&
num
<=
999
;
num
++
{
fname
=
w
.
f
ilename
+
fmt
.
Sprintf
(
".%s.%03d"
,
time
.
Now
()
.
Format
(
"2006-01-02"
),
num
)
fname
=
w
.
F
ilename
+
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
.
f
ilename
)
return
fmt
.
Errorf
(
"Rotate: Cannot find free log number to rename %s
\n
"
,
w
.
F
ilename
)
}
// block Logger's io.Writer
...
...
@@ -204,7 +183,7 @@ func (w *FileLogWriter) DoRotate() error {
// close fd before rename
// Rename the file to its newfound home
err
=
os
.
Rename
(
w
.
f
ilename
,
fname
)
err
=
os
.
Rename
(
w
.
F
ilename
,
fname
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"Rotate: %s
\n
"
,
err
)
}
...
...
@@ -222,10 +201,10 @@ func (w *FileLogWriter) DoRotate() error {
}
func
(
w
*
FileLogWriter
)
deleteOldLog
()
{
dir
:=
path
.
Dir
(
w
.
f
ilename
)
dir
:=
path
.
Dir
(
w
.
F
ilename
)
filepath
.
Walk
(
dir
,
func
(
path
string
,
info
os
.
FileInfo
,
err
error
)
error
{
if
!
info
.
IsDir
()
&&
info
.
ModTime
()
.
Unix
()
<
(
time
.
Now
()
.
Unix
()
-
60
*
60
*
24
*
w
.
m
axdays
)
{
if
strings
.
HasPrefix
(
filepath
.
Base
(
path
),
filepath
.
Base
(
w
.
f
ilename
))
{
if
!
info
.
IsDir
()
&&
info
.
ModTime
()
.
Unix
()
<
(
time
.
Now
()
.
Unix
()
-
60
*
60
*
24
*
w
.
M
axdays
)
{
if
strings
.
HasPrefix
(
filepath
.
Base
(
path
),
filepath
.
Base
(
w
.
F
ilename
))
{
os
.
Remove
(
path
)
}
}
...
...
logs/smtp.go
浏览文件 @
67b9c635
...
...
@@ -2,7 +2,6 @@ package logs
import
(
"encoding/json"
"errors"
"fmt"
"net/smtp"
"strings"
...
...
@@ -15,77 +14,51 @@ const (
// smtpWriter is used to send emails via given SMTP-server.
type
SmtpWriter
struct
{
username
string
password
string
host
string
subject
string
recipientAddresses
[]
string
level
int
Username
string
`json:"Username"`
Password
string
`json:"password"`
Host
string
`json:"Host"`
Subject
string
`json:"subject"`
RecipientAddresses
[]
string
`json:"sendTos"`
Level
int
`json:"level"`
}
func
NewSmtpWriter
()
LoggerInterface
{
return
&
SmtpWriter
{
l
evel
:
LevelTrace
}
return
&
SmtpWriter
{
L
evel
:
LevelTrace
}
}
func
(
s
*
SmtpWriter
)
Init
(
jsonconfig
string
)
error
{
var
m
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
&
m
)
err
:=
json
.
Unmarshal
([]
byte
(
jsonconfig
),
s
)
if
err
!=
nil
{
return
err
}
if
username
,
ok
:=
m
[
"username"
];
!
ok
{
return
errors
.
New
(
"smtp config must have auth username"
)
}
else
if
password
,
ok
:=
m
[
"password"
];
!
ok
{
return
errors
.
New
(
"smtp config must have auth password"
)
}
else
if
hostname
,
ok
:=
m
[
"host"
];
!
ok
{
return
errors
.
New
(
"smtp config must have host like 'mail.example.com:25'"
)
}
else
if
sendTos
,
ok
:=
m
[
"sendTos"
];
!
ok
{
return
errors
.
New
(
"smtp config must have sendTos"
)
}
else
{
s
.
username
=
username
.
(
string
)
s
.
password
=
password
.
(
string
)
s
.
host
=
hostname
.
(
string
)
for
_
,
v
:=
range
sendTos
.
([]
interface
{})
{
s
.
recipientAddresses
=
append
(
s
.
recipientAddresses
,
v
.
(
string
))
}
}
if
subject
,
ok
:=
m
[
"subject"
];
ok
{
s
.
subject
=
subject
.
(
string
)
}
else
{
s
.
subject
=
subjectPhrase
}
if
lv
,
ok
:=
m
[
"level"
];
ok
{
s
.
level
=
int
(
lv
.
(
float64
))
}
return
nil
}
func
(
s
*
SmtpWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
s
.
l
evel
{
if
level
<
s
.
L
evel
{
return
nil
}
hp
:=
strings
.
Split
(
s
.
h
ost
,
":"
)
hp
:=
strings
.
Split
(
s
.
H
ost
,
":"
)
// Set up authentication information.
auth
:=
smtp
.
PlainAuth
(
""
,
s
.
u
sername
,
s
.
p
assword
,
s
.
U
sername
,
s
.
P
assword
,
hp
[
0
],
)
// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
content_type
:=
"Content-Type: text/plain"
+
"; charset=UTF-8"
mailmsg
:=
[]
byte
(
"To: "
+
strings
.
Join
(
s
.
recipientAddresses
,
";"
)
+
"
\r\n
From: "
+
s
.
username
+
"<"
+
s
.
u
sername
+
">
\r\n
Subject: "
+
s
.
s
ubject
+
"
\r\n
"
+
content_type
+
"
\r\n\r\n
"
+
fmt
.
Sprintf
(
".%s"
,
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
))
+
msg
)
mailmsg
:=
[]
byte
(
"To: "
+
strings
.
Join
(
s
.
RecipientAddresses
,
";"
)
+
"
\r\n
From: "
+
s
.
Username
+
"<"
+
s
.
U
sername
+
">
\r\n
Subject: "
+
s
.
S
ubject
+
"
\r\n
"
+
content_type
+
"
\r\n\r\n
"
+
fmt
.
Sprintf
(
".%s"
,
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
))
+
msg
)
err
:=
smtp
.
SendMail
(
s
.
h
ost
,
s
.
H
ost
,
auth
,
s
.
u
sername
,
s
.
r
ecipientAddresses
,
s
.
U
sername
,
s
.
R
ecipientAddresses
,
mailmsg
,
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录