Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
6e638ef6
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,发现更多精彩内容 >>
提交
6e638ef6
编写于
9月 19, 2020
作者:
W
wangle
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Provides a quick format method by PatternLogFormatter struct
上级
df043f22
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
83 addition
and
0 deletion
+83
-0
pkg/infrastructure/logs/formatter.go
pkg/infrastructure/logs/formatter.go
+55
-0
pkg/infrastructure/logs/formatter_test.go
pkg/infrastructure/logs/formatter_test.go
+28
-0
未找到文件。
pkg/infrastructure/logs/formatter.go
浏览文件 @
6e638ef6
...
...
@@ -14,12 +14,39 @@
package
logs
import
(
"path"
"strconv"
)
var
formatterMap
=
make
(
map
[
string
]
LogFormatter
,
4
)
type
LogFormatter
interface
{
Format
(
lm
*
LogMsg
)
string
}
// PatternLogFormatter provides a quick format method
// for example:
// tes := PatternLogFormatter{Pattern: "%F:%n|%w %t>> %m", WhenFormat: "2006-01-02"}
// RegisterFormatter("tes", tes)
// SetGlobalFormatter("tes")
type
PatternLogFormatter
struct
{
Pattern
string
WhenFormat
string
}
func
(
p
PatternLogFormatter
)
getWhenFormatter
()
string
{
s
:=
p
.
WhenFormat
if
s
==
""
{
s
=
"2006/01/02 15:04:05.123"
// default style
}
return
s
}
func
(
p
PatternLogFormatter
)
Format
(
lm
*
LogMsg
)
string
{
return
p
.
ToString
(
lm
)
}
// RegisterFormatter register an formatter. Usually you should use this to extend your custom formatter
// for example:
// RegisterFormatter("my-fmt", &MyFormatter{})
...
...
@@ -32,3 +59,31 @@ func GetFormatter(name string) (LogFormatter, bool) {
res
,
ok
:=
formatterMap
[
name
]
return
res
,
ok
}
// 'w' when, 'm' msg,'f' filename,'F' full path,'n' line number
// 'l' level number, 't' prefix of level type, 'T' full name of level type
func
(
p
PatternLogFormatter
)
ToString
(
lm
*
LogMsg
)
string
{
s
:=
[]
rune
(
p
.
Pattern
)
m
:=
map
[
rune
]
string
{
'w'
:
lm
.
When
.
Format
(
p
.
getWhenFormatter
()),
'm'
:
lm
.
Msg
,
'n'
:
strconv
.
Itoa
(
lm
.
LineNumber
),
'l'
:
strconv
.
Itoa
(
lm
.
Level
),
't'
:
levelPrefix
[
lm
.
Level
-
1
],
'T'
:
levelNames
[
lm
.
Level
-
1
],
'F'
:
lm
.
FilePath
,
}
_
,
m
[
'f'
]
=
path
.
Split
(
lm
.
FilePath
)
res
:=
""
for
i
:=
0
;
i
<
len
(
s
)
-
1
;
i
++
{
if
s
[
i
]
==
'%'
{
if
k
,
ok
:=
m
[
s
[
i
+
1
]];
ok
{
res
+=
k
i
++
continue
}
}
res
+=
string
(
s
[
i
])
}
return
res
}
pkg/infrastructure/logs/formatter_test.go
0 → 100644
浏览文件 @
6e638ef6
package
logs
import
(
"strconv"
"testing"
"time"
)
func
TestPatternLogFormatter
(
t
*
testing
.
T
)
{
tes
:=
PatternLogFormatter
{
Pattern
:
"%F:%n|%w%t>> %m"
,
WhenFormat
:
"2006-01-02"
,
}
when
:=
time
.
Now
()
lm
:=
&
LogMsg
{
Msg
:
"message"
,
FilePath
:
"/User/go/beego/main.go"
,
Level
:
LevelWarn
,
LineNumber
:
10
,
When
:
when
,
}
got
:=
tes
.
ToString
(
lm
)
want
:=
lm
.
FilePath
+
":"
+
strconv
.
Itoa
(
lm
.
LineNumber
)
+
"|"
+
when
.
Format
(
tes
.
WhenFormat
)
+
levelPrefix
[
lm
.
Level
-
1
]
+
">> "
+
lm
.
Msg
if
got
!=
want
{
t
.
Errorf
(
"want %s, got %s"
,
want
,
got
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录