Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
0e3e22ef
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,发现更多精彩内容 >>
提交
0e3e22ef
编写于
7月 12, 2014
作者:
F
fuxiaohei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
merge update
上级
a906bf11
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
137 addition
and
72 deletion
+137
-72
logs/conn.go
logs/conn.go
+1
-1
logs/conn_test.go
logs/conn_test.go
+1
-1
logs/console.go
logs/console.go
+9
-7
logs/console_test.go
logs/console_test.go
+22
-15
logs/file.go
logs/file.go
+1
-1
logs/file_test.go
logs/file_test.go
+25
-16
logs/log.go
logs/log.go
+77
-30
logs/smtp.go
logs/smtp.go
+1
-1
未找到文件。
logs/conn.go
浏览文件 @
0e3e22ef
...
...
@@ -48,7 +48,7 @@ func (c *ConnWriter) Init(jsonconfig string) error {
// write message in connection.
// if connection is down, try to re-connect.
func
(
c
*
ConnWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
c
.
Level
{
if
level
>
c
.
Level
{
return
nil
}
if
c
.
neddedConnectOnMsg
()
{
...
...
logs/conn_test.go
浏览文件 @
0e3e22ef
...
...
@@ -16,5 +16,5 @@ import (
func
TestConn
(
t
*
testing
.
T
)
{
log
:=
NewLogger
(
1000
)
log
.
SetLogger
(
"conn"
,
`{"net":"tcp","addr":":7020"}`
)
log
.
Info
(
"info
"
)
log
.
Info
rmational
(
"informational
"
)
}
logs/console.go
浏览文件 @
0e3e22ef
...
...
@@ -27,12 +27,14 @@ func NewBrush(color string) Brush {
}
var
colors
=
[]
Brush
{
NewBrush
(
"1;36"
),
// Trace cyan
NewBrush
(
"1;34"
),
// Debug blue
NewBrush
(
"1;32"
),
// Info green
NewBrush
(
"1;33"
),
// Warn yellow
NewBrush
(
"1;37"
),
// Emergency white
NewBrush
(
"1;36"
),
// Alert cyan
NewBrush
(
"1;35"
),
// Critical magenta
NewBrush
(
"1;31"
),
// Error red
NewBrush
(
"1;35"
),
// Critical purple
NewBrush
(
"1;33"
),
// Warning yellow
NewBrush
(
"1;32"
),
// Notice green
NewBrush
(
"1;34"
),
// Informational green
NewBrush
(
"1;30"
),
// Debug black
}
// ConsoleWriter implements LoggerInterface and writes messages to terminal.
...
...
@@ -45,7 +47,7 @@ type ConsoleWriter struct {
func
NewConsole
()
LoggerInterface
{
cw
:=
new
(
ConsoleWriter
)
cw
.
lg
=
log
.
New
(
os
.
Stdout
,
""
,
log
.
Ldate
|
log
.
Ltime
)
cw
.
Level
=
Level
Trace
cw
.
Level
=
Level
Debug
return
cw
}
...
...
@@ -64,7 +66,7 @@ func (c *ConsoleWriter) Init(jsonconfig string) error {
// write message in console.
func
(
c
*
ConsoleWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
c
.
Level
{
if
level
>
c
.
Level
{
return
nil
}
if
goos
:=
runtime
.
GOOS
;
goos
==
"windows"
{
...
...
logs/console_test.go
浏览文件 @
0e3e22ef
...
...
@@ -13,22 +13,29 @@ import (
"testing"
)
// Try each log level in decreasing order of priority.
func
testConsoleCalls
(
bl
*
BeeLogger
)
{
bl
.
Emergency
(
"emergency"
)
bl
.
Alert
(
"alert"
)
bl
.
Critical
(
"critical"
)
bl
.
Error
(
"error"
)
bl
.
Warning
(
"warning"
)
bl
.
Notice
(
"notice"
)
bl
.
Informational
(
"informational"
)
bl
.
Debug
(
"debug"
)
}
// Test console logging by visually comparing the lines being output with and
// without a log level specification.
func
TestConsole
(
t
*
testing
.
T
)
{
log
:=
NewLogger
(
10000
)
log
.
EnableFuncCallDepth
(
true
)
log
.
SetLogger
(
"console"
,
""
)
log
.
Trace
(
"trace"
)
log
.
Info
(
"info"
)
log
.
Warn
(
"warning"
)
log
.
Debug
(
"debug"
)
log
.
Critical
(
"critical"
)
log1
:=
NewLogger
(
10000
)
log1
.
EnableFuncCallDepth
(
true
)
log1
.
SetLogger
(
"console"
,
""
)
testConsoleCalls
(
log1
)
log2
:=
NewLogger
(
100
)
log2
.
SetLogger
(
"console"
,
`{"level":1}`
)
log
.
Trace
(
"trace"
)
log
.
Info
(
"info"
)
log
.
Warn
(
"warning"
)
log
.
Debug
(
"debug"
)
log
.
Critical
(
"critical"
)
log2
.
SetLogger
(
"console"
,
`{"level":3}`
)
testConsoleCalls
(
log2
)
}
func
BenchmarkConsole
(
b
*
testing
.
B
)
{
...
...
@@ -36,6 +43,6 @@ func BenchmarkConsole(b *testing.B) {
log
.
EnableFuncCallDepth
(
true
)
log
.
SetLogger
(
"console"
,
""
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
log
.
Trace
(
"trace
"
)
log
.
Debug
(
"debug
"
)
}
}
logs/file.go
浏览文件 @
0e3e22ef
...
...
@@ -141,7 +141,7 @@ func (w *FileLogWriter) docheck(size int) {
// write logger message into file.
func
(
w
*
FileLogWriter
)
WriteMsg
(
msg
string
,
level
int
)
error
{
if
level
<
w
.
Level
{
if
level
>
w
.
Level
{
return
nil
}
n
:=
24
+
len
(
msg
)
// 24 stand for the length "2013/06/23 21:00:22 [T] "
...
...
logs/file_test.go
浏览文件 @
0e3e22ef
...
...
@@ -13,6 +13,7 @@ import (
"bufio"
"fmt"
"os"
"strconv"
"testing"
"time"
)
...
...
@@ -20,12 +21,14 @@ import (
func
TestFile
(
t
*
testing
.
T
)
{
log
:=
NewLogger
(
10000
)
log
.
SetLogger
(
"file"
,
`{"filename":"test.log"}`
)
log
.
Trace
(
"test"
)
log
.
Info
(
"info"
)
log
.
Debug
(
"debug"
)
log
.
Warn
(
"warning"
)
log
.
Informational
(
"info"
)
log
.
Notice
(
"notice"
)
log
.
Warning
(
"warning"
)
log
.
Error
(
"error"
)
log
.
Alert
(
"alert"
)
log
.
Critical
(
"critical"
)
log
.
Emergency
(
"emergency"
)
time
.
Sleep
(
time
.
Second
*
4
)
f
,
err
:=
os
.
Open
(
"test.log"
)
if
err
!=
nil
{
...
...
@@ -42,21 +45,24 @@ func TestFile(t *testing.T) {
linenum
++
}
}
if
linenum
!=
6
{
t
.
Fatal
(
linenum
,
"not line 6"
)
var
expected
=
LevelDebug
+
1
if
linenum
!=
expected
{
t
.
Fatal
(
linenum
,
"not "
+
strconv
.
Itoa
(
expected
)
+
" lines"
)
}
os
.
Remove
(
"test.log"
)
}
func
TestFile2
(
t
*
testing
.
T
)
{
log
:=
NewLogger
(
10000
)
log
.
SetLogger
(
"file"
,
`{"filename":"test2.log","level":2}`
)
log
.
Trace
(
"test"
)
log
.
Info
(
"info"
)
log
.
SetLogger
(
"file"
,
fmt
.
Sprintf
(
`{"filename":"test2.log","level":%d}`
,
LevelError
))
log
.
Debug
(
"debug"
)
log
.
Warn
(
"warning"
)
log
.
Info
(
"info"
)
log
.
Notice
(
"notice"
)
log
.
Warning
(
"warning"
)
log
.
Error
(
"error"
)
log
.
Alert
(
"alert"
)
log
.
Critical
(
"critical"
)
log
.
Emergency
(
"emergency"
)
time
.
Sleep
(
time
.
Second
*
4
)
f
,
err
:=
os
.
Open
(
"test2.log"
)
if
err
!=
nil
{
...
...
@@ -73,8 +79,9 @@ func TestFile2(t *testing.T) {
linenum
++
}
}
if
linenum
!=
4
{
t
.
Fatal
(
linenum
,
"not line 4"
)
var
expected
=
LevelError
+
1
if
linenum
!=
expected
{
t
.
Fatal
(
linenum
,
"not "
+
strconv
.
Itoa
(
expected
)
+
" lines"
)
}
os
.
Remove
(
"test2.log"
)
}
...
...
@@ -82,17 +89,19 @@ func TestFile2(t *testing.T) {
func
TestFileRotate
(
t
*
testing
.
T
)
{
log
:=
NewLogger
(
10000
)
log
.
SetLogger
(
"file"
,
`{"filename":"test3.log","maxlines":4}`
)
log
.
Trace
(
"test"
)
log
.
Info
(
"info"
)
log
.
Debug
(
"debug"
)
log
.
Warn
(
"warning"
)
log
.
Info
(
"info"
)
log
.
Notice
(
"notice"
)
log
.
Warning
(
"warning"
)
log
.
Error
(
"error"
)
log
.
Alert
(
"alert"
)
log
.
Critical
(
"critical"
)
log
.
Emergency
(
"emergency"
)
time
.
Sleep
(
time
.
Second
*
4
)
rotatename
:=
"test3.log"
+
fmt
.
Sprintf
(
".%s.%03d"
,
time
.
Now
()
.
Format
(
"2006-01-02"
),
1
)
b
,
err
:=
exists
(
rotatename
)
if
!
b
||
err
!=
nil
{
t
.
Fatal
(
"rotate not gen"
)
t
.
Fatal
(
"rotate not gen
erated
"
)
}
os
.
Remove
(
rotatename
)
os
.
Remove
(
"test3.log"
)
...
...
@@ -113,7 +122,7 @@ func BenchmarkFile(b *testing.B) {
log
:=
NewLogger
(
100000
)
log
.
SetLogger
(
"file"
,
`{"filename":"test4.log"}`
)
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
log
.
Trace
(
"trace
"
)
log
.
Debug
(
"debug
"
)
}
os
.
Remove
(
"test4.log"
)
}
logs/log.go
浏览文件 @
0e3e22ef
...
...
@@ -16,14 +16,25 @@ import (
"sync"
)
// RFC5424 log message levels.
const
(
// log message levels
LevelTrace
=
iota
LevelDebug
LevelInfo
LevelWarn
LevelError
LevelEmergency
=
iota
LevelAlert
LevelCritical
LevelError
LevelWarning
LevelNotice
LevelInformational
LevelDebug
)
// Legacy loglevel constants to ensure backwards compatibility.
//
// Deprecated: will be removed in 1.5.0.
const
(
LevelInfo
=
LevelInformational
LevelTrace
=
LevelDebug
LevelWarn
=
LevelWarning
)
type
loggerType
func
()
LoggerInterface
...
...
@@ -72,6 +83,7 @@ type logMsg struct {
// if the buffering chan is full, logger adapters write to file or other way.
func
NewLogger
(
channellen
int64
)
*
BeeLogger
{
bl
:=
new
(
BeeLogger
)
bl
.
level
=
LevelDebug
bl
.
loggerFuncCallDepth
=
2
bl
.
msg
=
make
(
chan
*
logMsg
,
channellen
)
bl
.
outputs
=
make
(
map
[
string
]
LoggerInterface
)
...
...
@@ -113,7 +125,7 @@ func (bl *BeeLogger) DelLogger(adaptername string) error {
}
func
(
bl
*
BeeLogger
)
writerMsg
(
loglevel
int
,
msg
string
)
error
{
if
bl
.
level
>
log
level
{
if
loglevel
>
bl
.
level
{
return
nil
}
lm
:=
new
(
logMsg
)
...
...
@@ -133,8 +145,10 @@ func (bl *BeeLogger) writerMsg(loglevel int, msg string) error {
return
nil
}
// set log message level.
// if message level (such as LevelTrace) is less than logger level (such as LevelWarn), ignore message.
// Set log message level.
//
// If message level (such as LevelDebug) is higher than logger level (such as LevelWarning),
// log providers will not even be sent the message.
func
(
bl
*
BeeLogger
)
SetLevel
(
l
int
)
{
bl
.
level
=
l
}
...
...
@@ -162,40 +176,73 @@ func (bl *BeeLogger) startLogger() {
}
}
//
log trace
level message.
func
(
bl
*
BeeLogger
)
Trace
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[
T
] "
+
format
,
v
...
)
bl
.
writerMsg
(
Level
Trace
,
msg
)
//
Log EMERGENCY
level message.
func
(
bl
*
BeeLogger
)
Emergency
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[
D
] "
+
format
,
v
...
)
bl
.
writerMsg
(
Level
Emergency
,
msg
)
}
//
log debug
level message.
func
(
bl
*
BeeLogger
)
Debug
(
format
string
,
v
...
interface
{})
{
//
Log ALERT
level message.
func
(
bl
*
BeeLogger
)
Alert
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[D] "
+
format
,
v
...
)
bl
.
writerMsg
(
Level
Debug
,
msg
)
bl
.
writerMsg
(
Level
Alert
,
msg
)
}
// log info level message.
func
(
bl
*
BeeLogger
)
Info
(
format
string
,
v
...
interface
{})
{
// Log CRITICAL level message.
func
(
bl
*
BeeLogger
)
Critical
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[C] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelCritical
,
msg
)
}
// Log ERROR level message.
func
(
bl
*
BeeLogger
)
Error
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[E] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelError
,
msg
)
}
// Log WARNING level message.
func
(
bl
*
BeeLogger
)
Warning
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[W] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelWarning
,
msg
)
}
// Log NOTICE level message.
func
(
bl
*
BeeLogger
)
Notice
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[W] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelNotice
,
msg
)
}
// Log INFORMATIONAL level message.
func
(
bl
*
BeeLogger
)
Informational
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[I] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelInfo
,
msg
)
bl
.
writerMsg
(
LevelInfo
rmational
,
msg
)
}
// log warn level message.
// Log DEBUG level message.
func
(
bl
*
BeeLogger
)
Debug
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[D] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelDebug
,
msg
)
}
// Log WARN level message.
//
// Deprecated: compatibility alias for Warning(), Will be removed in 1.5.0.
func
(
bl
*
BeeLogger
)
Warn
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[W] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelWarn
,
msg
)
bl
.
Warning
(
format
,
v
...
)
}
// log error level message.
func
(
bl
*
BeeLogger
)
Error
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[E] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelError
,
msg
)
// Log INFO level message.
//
// Deprecated: compatibility alias for Informational(), Will be removed in 1.5.0.
func
(
bl
*
BeeLogger
)
Info
(
format
string
,
v
...
interface
{})
{
bl
.
Informational
(
format
,
v
...
)
}
// log critical level message.
func
(
bl
*
BeeLogger
)
Critical
(
format
string
,
v
...
interface
{})
{
msg
:=
fmt
.
Sprintf
(
"[C] "
+
format
,
v
...
)
bl
.
writerMsg
(
LevelCritical
,
msg
)
// Log TRACE level message.
//
// Deprecated: compatibility alias for Debug(), Will be removed in 1.5.0.
func
(
bl
*
BeeLogger
)
Trace
(
format
string
,
v
...
interface
{})
{
bl
.
Debug
(
format
,
v
...
)
}
// flush all chan data.
...
...
logs/smtp.go
浏览文件 @
0e3e22ef
...
...
@@ -57,7 +57,7 @@ func (s *SmtpWriter) Init(jsonconfig string) error {
// 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
{
if
level
<
s
.
Level
{
if
level
>
s
.
Level
{
return
nil
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录