Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
8611862f
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,发现更多精彩内容 >>
提交
8611862f
编写于
1月 26, 2016
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1609 from youngsterxyf/fix-issue1566
fix issue #1566
上级
15e9ba19
bcac4bb8
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
72 addition
and
56 deletion
+72
-56
logs/conn.go
logs/conn.go
+6
-2
logs/console.go
logs/console.go
+4
-2
logs/es/es.go
logs/es/es.go
+4
-4
logs/file.go
logs/file.go
+4
-40
logs/log.go
logs/log.go
+52
-6
logs/smtp.go
logs/smtp.go
+2
-2
未找到文件。
logs/conn.go
浏览文件 @
8611862f
...
...
@@ -19,6 +19,7 @@ import (
"io"
"log"
"net"
"time"
)
// connWriter implements LoggerInterface.
...
...
@@ -48,7 +49,7 @@ func (c *connWriter) Init(jsonconfig string) error {
// WriteMsg write message in connection.
// if connection is down, try to re-connect.
func
(
c
*
connWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
func
(
c
*
connWriter
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
level
>
c
.
Level
{
return
nil
}
...
...
@@ -62,6 +63,9 @@ func (c *connWriter) WriteMsg(msg string, level int) error {
if
c
.
ReconnectOnMsg
{
defer
c
.
innerWriter
.
Close
()
}
msg
=
formatLogTime
(
when
)
+
msg
c
.
lg
.
Println
(
msg
)
return
nil
}
...
...
@@ -94,7 +98,7 @@ func (c *connWriter) connect() error {
}
c
.
innerWriter
=
conn
c
.
lg
=
log
.
New
(
conn
,
""
,
log
.
Ldate
|
log
.
Ltime
)
c
.
lg
=
log
.
New
(
conn
,
""
,
0
)
return
nil
}
...
...
logs/console.go
浏览文件 @
8611862f
...
...
@@ -19,6 +19,7 @@ import (
"log"
"os"
"runtime"
"time"
)
// brush is a color join function
...
...
@@ -53,7 +54,7 @@ type consoleWriter struct {
// NewConsole create ConsoleWriter returning as LoggerInterface.
func
NewConsole
()
Logger
{
cw
:=
&
consoleWriter
{
lg
:
log
.
New
(
os
.
Stdout
,
""
,
log
.
Ldate
|
log
.
Ltime
),
lg
:
log
.
New
(
os
.
Stdout
,
""
,
0
),
Level
:
LevelDebug
,
}
return
cw
...
...
@@ -69,10 +70,11 @@ func (c *consoleWriter) Init(jsonconfig string) error {
}
// WriteMsg write message in console.
func
(
c
*
consoleWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
func
(
c
*
consoleWriter
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
level
>
c
.
Level
{
return
nil
}
msg
=
formatLogTime
(
when
)
+
msg
if
goos
:=
runtime
.
GOOS
;
goos
==
"windows"
{
c
.
lg
.
Println
(
msg
)
return
nil
...
...
logs/es/es.go
浏览文件 @
8611862f
...
...
@@ -48,16 +48,16 @@ func (el *esLogger) Init(jsonconfig string) error {
}
// WriteMsg will write the msg and level into es
func
(
el
*
esLogger
)
WriteMsg
(
msg
string
,
level
int
)
error
{
func
(
el
*
esLogger
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
level
>
el
.
Level
{
return
nil
}
t
:=
time
.
Now
()
vals
:=
make
(
map
[
string
]
interface
{})
vals
[
"@timestamp"
]
=
t
.
Format
(
time
.
RFC3339
)
vals
[
"@timestamp"
]
=
when
.
Format
(
time
.
RFC3339
)
vals
[
"@msg"
]
=
msg
d
:=
goes
.
Document
{
Index
:
fmt
.
Sprintf
(
"%04d.%02d.%02d"
,
t
.
Year
(),
t
.
Month
(),
t
.
Day
()),
Index
:
fmt
.
Sprintf
(
"%04d.%02d.%02d"
,
when
.
Year
(),
when
.
Month
(),
when
.
Day
()),
Type
:
"logs"
,
Fields
:
vals
,
}
...
...
logs/file.go
浏览文件 @
8611862f
...
...
@@ -114,50 +114,14 @@ func (w *fileLogWriter) needRotate(size int, day int) bool {
}
// WriteMsg write logger message into file.
func
(
w
*
fileLogWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
func
(
w
*
fileLogWriter
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
level
>
w
.
Level
{
return
nil
}
//2016/01/12 21:34:33
now
:=
time
.
Now
()
y
,
mo
,
d
:=
now
.
Date
()
h
,
mi
,
s
:=
now
.
Clock
()
//len(2006/01/02 15:03:04)==19
var
buf
[
20
]
byte
t
:=
3
for
y
>=
10
{
p
:=
y
/
10
buf
[
t
]
=
byte
(
'0'
+
y
-
p
*
10
)
y
=
p
t
--
}
buf
[
0
]
=
byte
(
'0'
+
y
)
buf
[
4
]
=
'/'
if
mo
>
9
{
buf
[
5
]
=
'1'
buf
[
6
]
=
byte
(
'0'
+
mo
-
9
)
}
else
{
buf
[
5
]
=
'0'
buf
[
6
]
=
byte
(
'0'
+
mo
)
}
buf
[
7
]
=
'/'
t
=
d
/
10
buf
[
8
]
=
byte
(
'0'
+
t
)
buf
[
9
]
=
byte
(
'0'
+
d
-
t
*
10
)
buf
[
10
]
=
' '
t
=
h
/
10
buf
[
11
]
=
byte
(
'0'
+
t
)
buf
[
12
]
=
byte
(
'0'
+
h
-
t
*
10
)
buf
[
13
]
=
':'
t
=
mi
/
10
buf
[
14
]
=
byte
(
'0'
+
t
)
buf
[
15
]
=
byte
(
'0'
+
mi
-
t
*
10
)
buf
[
16
]
=
':'
t
=
s
/
10
buf
[
17
]
=
byte
(
'0'
+
t
)
buf
[
18
]
=
byte
(
'0'
+
s
-
t
*
10
)
buf
[
19
]
=
' '
msg
=
string
(
buf
[
0
:
])
+
msg
+
"
\n
"
// now := time.Now()
d
:=
when
.
Day
()
msg
=
formatLogTime
(
when
)
+
msg
+
"
\n
"
if
w
.
Rotate
{
if
w
.
needRotate
(
len
(
msg
),
d
)
{
...
...
logs/log.go
浏览文件 @
8611862f
...
...
@@ -40,6 +40,7 @@ import (
"runtime"
"strconv"
"sync"
"time"
)
// RFC5424 log message levels.
...
...
@@ -68,7 +69,7 @@ type loggerType func() Logger
// Logger defines the behavior of a log provider.
type
Logger
interface
{
Init
(
config
string
)
error
WriteMsg
(
msg
string
,
level
int
)
error
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
Destroy
()
Flush
()
}
...
...
@@ -108,6 +109,7 @@ type nameLogger struct {
type
logMsg
struct
{
level
int
msg
string
when
time
.
Time
}
var
logMsgPool
*
sync
.
Pool
...
...
@@ -173,9 +175,9 @@ func (bl *BeeLogger) DelLogger(adapterName string) error {
return
nil
}
func
(
bl
*
BeeLogger
)
writeToLoggers
(
msg
string
,
level
int
)
{
func
(
bl
*
BeeLogger
)
writeToLoggers
(
when
time
.
Time
,
msg
string
,
level
int
)
{
for
_
,
l
:=
range
bl
.
outputs
{
err
:=
l
.
WriteMsg
(
msg
,
level
)
err
:=
l
.
WriteMsg
(
when
,
msg
,
level
)
if
err
!=
nil
{
fmt
.
Fprintf
(
os
.
Stderr
,
"unable to WriteMsg to adapter:%v,error:%v
\n
"
,
l
.
name
,
err
)
}
...
...
@@ -183,6 +185,7 @@ func (bl *BeeLogger) writeToLoggers(msg string, level int) {
}
func
(
bl
*
BeeLogger
)
writeMsg
(
logLevel
int
,
msg
string
)
error
{
when
:=
time
.
Now
()
if
bl
.
enableFuncCallDepth
{
_
,
file
,
line
,
ok
:=
runtime
.
Caller
(
bl
.
loggerFuncCallDepth
)
if
!
ok
{
...
...
@@ -196,9 +199,10 @@ func (bl *BeeLogger) writeMsg(logLevel int, msg string) error {
lm
:=
logMsgPool
.
Get
()
.
(
*
logMsg
)
lm
.
level
=
logLevel
lm
.
msg
=
msg
lm
.
when
=
when
bl
.
msgChan
<-
lm
}
else
{
bl
.
writeToLoggers
(
msg
,
logLevel
)
bl
.
writeToLoggers
(
when
,
msg
,
logLevel
)
}
return
nil
}
...
...
@@ -231,7 +235,7 @@ func (bl *BeeLogger) startLogger() {
for
{
select
{
case
bm
:=
<-
bl
.
msgChan
:
bl
.
writeToLoggers
(
bm
.
msg
,
bm
.
level
)
bl
.
writeToLoggers
(
bm
.
when
,
bm
.
msg
,
bm
.
level
)
logMsgPool
.
Put
(
bm
)
}
}
...
...
@@ -351,7 +355,7 @@ func (bl *BeeLogger) Close() {
for
{
if
len
(
bl
.
msgChan
)
>
0
{
bm
:=
<-
bl
.
msgChan
bl
.
writeToLoggers
(
bm
.
msg
,
bm
.
level
)
bl
.
writeToLoggers
(
bm
.
when
,
bm
.
msg
,
bm
.
level
)
logMsgPool
.
Put
(
bm
)
continue
}
...
...
@@ -362,3 +366,45 @@ func (bl *BeeLogger) Close() {
l
.
Destroy
()
}
}
func
formatLogTime
(
when
time
.
Time
)
string
{
y
,
mo
,
d
:=
when
.
Date
()
h
,
mi
,
s
:=
when
.
Clock
()
//len(2006/01/02 15:03:04)==19
var
buf
[
20
]
byte
t
:=
3
for
y
>=
10
{
p
:=
y
/
10
buf
[
t
]
=
byte
(
'0'
+
y
-
p
*
10
)
y
=
p
t
--
}
buf
[
0
]
=
byte
(
'0'
+
y
)
buf
[
4
]
=
'/'
if
mo
>
9
{
buf
[
5
]
=
'1'
buf
[
6
]
=
byte
(
'0'
+
mo
-
9
)
}
else
{
buf
[
5
]
=
'0'
buf
[
6
]
=
byte
(
'0'
+
mo
)
}
buf
[
7
]
=
'/'
t
=
d
/
10
buf
[
8
]
=
byte
(
'0'
+
t
)
buf
[
9
]
=
byte
(
'0'
+
d
-
t
*
10
)
buf
[
10
]
=
' '
t
=
h
/
10
buf
[
11
]
=
byte
(
'0'
+
t
)
buf
[
12
]
=
byte
(
'0'
+
h
-
t
*
10
)
buf
[
13
]
=
':'
t
=
mi
/
10
buf
[
14
]
=
byte
(
'0'
+
t
)
buf
[
15
]
=
byte
(
'0'
+
mi
-
t
*
10
)
buf
[
16
]
=
':'
t
=
s
/
10
buf
[
17
]
=
byte
(
'0'
+
t
)
buf
[
18
]
=
byte
(
'0'
+
s
-
t
*
10
)
buf
[
19
]
=
' '
return
string
(
buf
[
0
:
])
}
logs/smtp.go
浏览文件 @
8611862f
...
...
@@ -126,7 +126,7 @@ func (s *SMTPWriter) sendMail(hostAddressWithPort string, auth smtp.Auth, fromAd
// WriteMsg write message in smtp writer.
// it will send an email with subject and only this message.
func
(
s
*
SMTPWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
func
(
s
*
SMTPWriter
)
WriteMsg
(
when
time
.
Time
,
msg
string
,
level
int
)
error
{
if
level
>
s
.
Level
{
return
nil
}
...
...
@@ -140,7 +140,7 @@ func (s *SMTPWriter) WriteMsg(msg string, level int) error {
// and send the email all in one step.
contentType
:=
"Content-Type: text/plain"
+
"; charset=UTF-8"
mailmsg
:=
[]
byte
(
"To: "
+
strings
.
Join
(
s
.
RecipientAddresses
,
";"
)
+
"
\r\n
From: "
+
s
.
FromAddress
+
"<"
+
s
.
FromAddress
+
">
\r\n
Subject: "
+
s
.
Subject
+
"
\r\n
"
+
contentType
+
"
\r\n\r\n
"
+
fmt
.
Sprintf
(
".%s"
,
time
.
Now
()
.
Format
(
"2006-01-02 15:04:05"
))
+
msg
)
">
\r\n
Subject: "
+
s
.
Subject
+
"
\r\n
"
+
contentType
+
"
\r\n\r\n
"
+
fmt
.
Sprintf
(
".%s"
,
when
.
Format
(
"2006-01-02 15:04:05"
))
+
msg
)
return
s
.
sendMail
(
s
.
Host
,
auth
,
s
.
FromAddress
,
s
.
RecipientAddresses
,
mailmsg
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录