mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-12 01:10:00 +08:00
Compare commits
470 Commits
tools/goct
...
v1.4.4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3369f8e81 | ||
|
|
c9b05ae07e | ||
|
|
32a59dbc27 | ||
|
|
ba0dff2d61 | ||
|
|
10da5e0424 | ||
|
|
4bed34090f | ||
|
|
2bfecf9354 | ||
|
|
6d129e0264 | ||
|
|
a2df1bb164 | ||
|
|
5f02e623f5 | ||
|
|
963b52fb1b | ||
|
|
02265d0bfe | ||
|
|
2e57e91826 | ||
|
|
82c642d3f4 | ||
|
|
b2571883ca | ||
|
|
00ff50c2cc | ||
|
|
4d7fa08b0b | ||
|
|
367afb544c | ||
|
|
43b8c7f641 | ||
|
|
a2dcb0079a | ||
|
|
f9619328f2 | ||
|
|
bae061a67e | ||
|
|
0b176e17ac | ||
|
|
6340e24c17 | ||
|
|
74e0676617 | ||
|
|
0defb7522f | ||
|
|
0c786ca849 | ||
|
|
26c541b9cb | ||
|
|
ade6f9ee46 | ||
|
|
f4502171ea | ||
|
|
8157e2118d | ||
|
|
e52dace416 | ||
|
|
dc260f196a | ||
|
|
559726112c | ||
|
|
a5fcf24c04 | ||
|
|
fc9b3ffdc1 | ||
|
|
e71c505e94 | ||
|
|
21c49009c0 | ||
|
|
69d355eb4b | ||
|
|
83f88d177f | ||
|
|
641ebf1667 | ||
|
|
cf435bfcc1 | ||
|
|
28f1b15b8e | ||
|
|
42413dc294 | ||
|
|
ec7ac43948 | ||
|
|
deefc1a8eb | ||
|
|
036328f1ea | ||
|
|
85057a623d | ||
|
|
1c544a26be | ||
|
|
20a61ce43e | ||
|
|
dd294e8cd6 | ||
|
|
3e9d0161bc | ||
|
|
cf6c349118 | ||
|
|
c7a0ec428c | ||
|
|
ce1c02f4f9 | ||
|
|
c3756a8f1c | ||
|
|
f4fd735aee | ||
|
|
683d793719 | ||
|
|
affbcb5698 | ||
|
|
f0d1722bbd | ||
|
|
c4f8eca459 | ||
|
|
251c071418 | ||
|
|
6652c4e445 | ||
|
|
f73613dff0 | ||
|
|
7a75dce465 | ||
|
|
801f1adf71 | ||
|
|
f76b976262 | ||
|
|
a49f9060c2 | ||
|
|
ebe28882eb | ||
|
|
fdc57d07d7 | ||
|
|
ef22042f4d | ||
|
|
944193ce25 | ||
|
|
dcfc9b79f1 | ||
|
|
b7052854bb | ||
|
|
4729a16142 | ||
|
|
3604659027 | ||
|
|
9f7f94b673 | ||
|
|
0b3629b636 | ||
|
|
a644ec7edd | ||
|
|
9941055eaa | ||
|
|
10fd9131a1 | ||
|
|
90828a0d4a | ||
|
|
b1c3c21c81 | ||
|
|
97a8b3ade5 | ||
|
|
95a5f64493 | ||
|
|
20e659749a | ||
|
|
94708cc78f | ||
|
|
06fafd2153 | ||
|
|
79de932646 | ||
|
|
b562e940e7 | ||
|
|
69068cdaf0 | ||
|
|
f25788ebea | ||
|
|
1293c4321b | ||
|
|
e3e08a7396 | ||
|
|
4b071f4c33 | ||
|
|
81831b60a9 | ||
|
|
1677a4dceb | ||
|
|
dac3600b53 | ||
|
|
3db64c7d47 | ||
|
|
7eb6aae949 | ||
|
|
07128213d6 | ||
|
|
9504d30049 | ||
|
|
ce73b9a85c | ||
|
|
4d2a146733 | ||
|
|
46e236fef7 | ||
|
|
06e4914e41 | ||
|
|
9cadab2684 | ||
|
|
7fe2492009 | ||
|
|
22bdf0bbd5 | ||
|
|
c92a2d1b77 | ||
|
|
b21162d638 | ||
|
|
7c9ef3ca67 | ||
|
|
bbadbe0175 | ||
|
|
f9beab1095 | ||
|
|
de5c59aad3 | ||
|
|
36d3765c5c | ||
|
|
d326e6f813 | ||
|
|
ea52fe2e0d | ||
|
|
05a5de7c6d | ||
|
|
d4c9fd2aff | ||
|
|
776673d57d | ||
|
|
1b87f5e30d | ||
|
|
bc47959384 | ||
|
|
9f6d926455 | ||
|
|
f7a4e3a19e | ||
|
|
a515a3c735 | ||
|
|
6f6f1ae21f | ||
|
|
10f94ffcc2 | ||
|
|
f068062b13 | ||
|
|
799c118d95 | ||
|
|
74cc6b55e8 | ||
|
|
fc59aec2e7 | ||
|
|
7868667b4f | ||
|
|
773b59106b | ||
|
|
97f8667b71 | ||
|
|
b51339b69b | ||
|
|
38a73d7fbe | ||
|
|
e50689beed | ||
|
|
1bc138bd34 | ||
|
|
4b9066eda6 | ||
|
|
0c66e041b5 | ||
|
|
aa2be0163a | ||
|
|
ada2941e87 | ||
|
|
59c0013cd1 | ||
|
|
05737f6519 | ||
|
|
4f6a900fd4 | ||
|
|
63cfe60f1a | ||
|
|
e7acadb15d | ||
|
|
111e626a73 | ||
|
|
1a6d7b3ef6 | ||
|
|
2e1e4f3574 | ||
|
|
22d0a2120a | ||
|
|
68e15360c2 | ||
|
|
1b344a8851 | ||
|
|
d640544a40 | ||
|
|
e6aa6fc361 | ||
|
|
4c927624b0 | ||
|
|
0ea92b7280 | ||
|
|
2cde970c9e | ||
|
|
5061158bd6 | ||
|
|
9138056c01 | ||
|
|
0b1884b6bd | ||
|
|
1f6688e5c1 | ||
|
|
ae7f1aabdd | ||
|
|
b8664be2bb | ||
|
|
6e16a9647e | ||
|
|
bb0e76be47 | ||
|
|
27a20e1ed3 | ||
|
|
cbbbee0ace | ||
|
|
e9650d547b | ||
|
|
60160f56b8 | ||
|
|
05c2f313c7 | ||
|
|
f2a0f78288 | ||
|
|
3e96994b7b | ||
|
|
66c2a28e66 | ||
|
|
9672071b5d | ||
|
|
9581e8445a | ||
|
|
6ec8bc6655 | ||
|
|
d935c83a54 | ||
|
|
590d784800 | ||
|
|
784276b360 | ||
|
|
da80662b0f | ||
|
|
cfda972d50 | ||
|
|
6078bf1a04 | ||
|
|
ce638d26d9 | ||
|
|
422f401153 | ||
|
|
dfeef5e497 | ||
|
|
8c72136631 | ||
|
|
9d6c8f67f5 | ||
|
|
f70805ee60 | ||
|
|
a1466e1707 | ||
|
|
1b477bbef9 | ||
|
|
813625d995 | ||
|
|
15a2802f12 | ||
|
|
5d00dfb962 | ||
|
|
d9620bb072 | ||
|
|
d978563523 | ||
|
|
fb6d7e2fd2 | ||
|
|
2d60f0c65a | ||
|
|
5d4ae201d0 | ||
|
|
05007c86bb | ||
|
|
93584c6ca6 | ||
|
|
22bb7e95fd | ||
|
|
bebf6322ff | ||
|
|
36678f9023 | ||
|
|
90cdd61efc | ||
|
|
28166dedd6 | ||
|
|
0316b6e10e | ||
|
|
4cb68a034a | ||
|
|
847a396f1c | ||
|
|
c1babdf8b2 | ||
|
|
040c9e0954 | ||
|
|
1c85d39add | ||
|
|
4cd065f4f4 | ||
|
|
b9c97678bc | ||
|
|
5208def65a | ||
|
|
3b96dc1598 | ||
|
|
fa3f1bc19c | ||
|
|
8ed22eafdd | ||
|
|
05dd6bd743 | ||
|
|
9af1a42386 | ||
|
|
f3645e420e | ||
|
|
62abac0b7e | ||
|
|
6357e27418 | ||
|
|
1568c3be0e | ||
|
|
27e773fa1f | ||
|
|
d8e17be33e | ||
|
|
da5770ee2b | ||
|
|
731b3ebf6f | ||
|
|
1e0f94ba86 | ||
|
|
a987512c7b | ||
|
|
c1c7584de1 | ||
|
|
98b9a25cc7 | ||
|
|
a8305def3d | ||
|
|
d20d8324e7 | ||
|
|
c638fce31c | ||
|
|
34294702b0 | ||
|
|
4fad067a0e | ||
|
|
3f3c811e08 | ||
|
|
dbdbb68676 | ||
|
|
83772344b0 | ||
|
|
49367f1713 | ||
|
|
91b8effb24 | ||
|
|
4879d4dfcd | ||
|
|
b18479dd43 | ||
|
|
5cd9229986 | ||
|
|
3d38d36605 | ||
|
|
003adae51f | ||
|
|
5348375b99 | ||
|
|
5d7919a9f5 | ||
|
|
9b334b5428 | ||
|
|
685d14e662 | ||
|
|
edbf1a3b63 | ||
|
|
92145b56dc | ||
|
|
34eb3fc12e | ||
|
|
101304be53 | ||
|
|
f630bc735b | ||
|
|
ca3c687f1c | ||
|
|
1b51d0ce82 | ||
|
|
d9218e1551 | ||
|
|
9c448c64ef | ||
|
|
bc85eaa9b1 | ||
|
|
2a6f801978 | ||
|
|
8d567b5508 | ||
|
|
0dd2768d09 | ||
|
|
4324ddc024 | ||
|
|
557383fbbf | ||
|
|
b206dd28a3 | ||
|
|
453fa309b1 | ||
|
|
4d7dae9cea | ||
|
|
d228b9038d | ||
|
|
13477238a3 | ||
|
|
95a574e9e9 | ||
|
|
453100e0e2 | ||
|
|
d70e73ec66 | ||
|
|
300b124e42 | ||
|
|
3bad043413 | ||
|
|
23f34234d0 | ||
|
|
d71b3c841f | ||
|
|
24787a946b | ||
|
|
6e50c87dca | ||
|
|
e672b3f8e1 | ||
|
|
1c09db6d5d | ||
|
|
96acf1f5a6 | ||
|
|
97a171441d | ||
|
|
725e6056e1 | ||
|
|
1410f7dc20 | ||
|
|
8afe68f3f1 | ||
|
|
74c41e8c5e | ||
|
|
48f7e01158 | ||
|
|
f6f6ee5c8c | ||
|
|
b364c54940 | ||
|
|
e0e3f97c7c | ||
|
|
6a2d6786c6 | ||
|
|
18035bd4d4 | ||
|
|
f3b8fef34f | ||
|
|
6a4885ba64 | ||
|
|
f2cef2b963 | ||
|
|
bfd0869ee2 | ||
|
|
4e26e0407e | ||
|
|
d200ba4a7b | ||
|
|
ce7e2a2a9a | ||
|
|
c92400ead2 | ||
|
|
0b109c1954 | ||
|
|
d42979f705 | ||
|
|
29d81381c1 | ||
|
|
89f6c97097 | ||
|
|
ff6f109065 | ||
|
|
7da77302f4 | ||
|
|
76086fc717 | ||
|
|
555c4ecd1a | ||
|
|
630dfa0887 | ||
|
|
38cd7b7df0 | ||
|
|
9148f8df2a | ||
|
|
13f051d0e5 | ||
|
|
93b3f5030f | ||
|
|
b44e8f5c75 | ||
|
|
b9eb03e9a9 | ||
|
|
86b531406b | ||
|
|
47c49de94e | ||
|
|
50f16e2892 | ||
|
|
018ca82048 | ||
|
|
6976ba7e13 | ||
|
|
9b6e4c440c | ||
|
|
9eea311a4d | ||
|
|
86d70317bf | ||
|
|
6518eb10b3 | ||
|
|
0147d7a9d1 | ||
|
|
1b2b7647d6 | ||
|
|
af6d37c33d | ||
|
|
3da5c5f530 | ||
|
|
1694a92db0 | ||
|
|
c27e00b45c | ||
|
|
ed1c937998 | ||
|
|
db9a1f3e27 | ||
|
|
392a390a3f | ||
|
|
2a900e1795 | ||
|
|
0f5d8c6be3 | ||
|
|
f2caf9237a | ||
|
|
2f0e4e3ebf | ||
|
|
2c6b422f6b | ||
|
|
4d34998338 | ||
|
|
8be47b9c99 | ||
|
|
1d95e95cf8 | ||
|
|
3fa8c5940d | ||
|
|
c44edd7cac | ||
|
|
af05219b70 | ||
|
|
f366e1d936 | ||
|
|
6c94e4652e | ||
|
|
edfaa6d906 | ||
|
|
b6b96d9dad | ||
|
|
87800419f5 | ||
|
|
50a5fb7715 | ||
|
|
aa8f07d064 | ||
|
|
7868bdf660 | ||
|
|
46078e716d | ||
|
|
bb33a20bc8 | ||
|
|
5536473a08 | ||
|
|
323b35ed2d | ||
|
|
30958a91f7 | ||
|
|
b94b68a427 | ||
|
|
07145b210e | ||
|
|
321a20add6 | ||
|
|
65098d4737 | ||
|
|
35425f6164 | ||
|
|
a0060ff81b | ||
|
|
289a325757 | ||
|
|
3fbe0f87b7 | ||
|
|
ea98d210fd | ||
|
|
b9bc1fdcf8 | ||
|
|
6dc570bcd7 | ||
|
|
e21997f0d7 | ||
|
|
92c0b7c3c5 | ||
|
|
6d3ed98744 | ||
|
|
fb519fa547 | ||
|
|
e9501c3fb3 | ||
|
|
fd12659729 | ||
|
|
72ebbb9774 | ||
|
|
f1fdd55b38 | ||
|
|
58787746db | ||
|
|
ca88b69d24 | ||
|
|
6b1e15cab1 | ||
|
|
6f86e5bff8 | ||
|
|
3f492df74e | ||
|
|
5e7b1f6bfe | ||
|
|
e80a64fa67 | ||
|
|
95282edb78 | ||
|
|
7b82eda993 | ||
|
|
5d09cd0e7c | ||
|
|
1e717f9f5c | ||
|
|
c6e2b4a43a | ||
|
|
e567a0c718 | ||
|
|
52f060caae | ||
|
|
f486685e99 | ||
|
|
3ae874d75d | ||
|
|
c58eb13328 | ||
|
|
14ca39bc86 | ||
|
|
3ea8a2d4b6 | ||
|
|
6d2b9fd904 | ||
|
|
5451d96a81 | ||
|
|
69c2bad410 | ||
|
|
5383e29ce6 | ||
|
|
51472004a3 | ||
|
|
caf5b7b1f1 | ||
|
|
bef9aa55e6 | ||
|
|
d0a59b13a6 | ||
|
|
469e62067c | ||
|
|
a36d58aac9 | ||
|
|
aa5118c2aa | ||
|
|
974ba5c9aa | ||
|
|
ec1de4f48d | ||
|
|
bab72b7630 | ||
|
|
ac321fc146 | ||
|
|
ae2c76765c | ||
|
|
f21970c117 | ||
|
|
d0a58d1f2d | ||
|
|
3bbc90ec24 | ||
|
|
cef83efd4e | ||
|
|
cc09ab2aba | ||
|
|
f7a60cdc24 | ||
|
|
c3a49ece8d | ||
|
|
1a38eddffe | ||
|
|
5bcee4cf7c | ||
|
|
5c9fae7e62 | ||
|
|
ec3e02624c | ||
|
|
22b157bb6c | ||
|
|
095b603788 | ||
|
|
bc3c9484d1 | ||
|
|
162e9cef86 | ||
|
|
94ddb3380e | ||
|
|
16c61c6657 | ||
|
|
14bf2f33f7 | ||
|
|
305587aa81 | ||
|
|
2cdff97934 | ||
|
|
bbe1249ecb | ||
|
|
e62870e268 | ||
|
|
92b450eb11 | ||
|
|
d58cf7a12a | ||
|
|
036d803fbb | ||
|
|
c6ab11b14f | ||
|
|
9e20b1bbfe | ||
|
|
fadef0ccd9 | ||
|
|
4382ec0e0d | ||
|
|
db99addc64 | ||
|
|
97bf3856c1 | ||
|
|
ff6c6558dd | ||
|
|
5d4e7c84ee | ||
|
|
cb4fcf2c6c | ||
|
|
ee88abce14 | ||
|
|
ecc3653d44 | ||
|
|
ba8ac974aa | ||
|
|
50de01fb49 | ||
|
|
fabea4c448 | ||
|
|
6d9dfc08f9 | ||
|
|
252fabcc4b | ||
|
|
415c4c91fc | ||
|
|
0cc9d4ff8d | ||
|
|
8bc34defc4 | ||
|
|
8dd764679c | ||
|
|
9fe868ade9 | ||
|
|
4e48286838 | ||
|
|
ab01442d46 | ||
|
|
8694e38384 | ||
|
|
d5e550e79b | ||
|
|
affdab660e | ||
|
|
7d5858e83a | ||
|
|
815a6a6485 | ||
|
|
475d17e17d | ||
|
|
8472415472 | ||
|
|
faad6e27e3 |
@@ -1,3 +1,6 @@
|
||||
comment: false
|
||||
comment:
|
||||
layout: "flags, files"
|
||||
behavior: once
|
||||
require_changes: true
|
||||
ignore:
|
||||
- "tools"
|
||||
3
.github/FUNDING.yml
vendored
3
.github/FUNDING.yml
vendored
@@ -9,4 +9,5 @@ community_bridge: # Replace with a single Community Bridge project-name e.g., cl
|
||||
liberapay: # Replace with a single Liberapay username
|
||||
issuehunt: # Replace with a single IssueHunt username
|
||||
otechie: # Replace with a single Otechie username
|
||||
custom: https://gitee.com/kevwan/static/raw/master/images/sponsor.jpg
|
||||
custom: # https://gitee.com/kevwan/static/raw/master/images/sponsor.jpg
|
||||
ethereum: 0x5052b7f6B937B02563996D23feb69b38D06Ca150 | kevwan
|
||||
|
||||
11
.github/dependabot.yml
vendored
Normal file
11
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# To get started with Dependabot version updates, you'll need to specify which
|
||||
# package ecosystems to update and where the package manifests are located.
|
||||
# Please see the documentation for all configuration options:
|
||||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: "gomod" # See documentation for possible values
|
||||
directory: "/" # Location of package manifests
|
||||
schedule:
|
||||
interval: "daily"
|
||||
8
.github/workflows/codeql-analysis.yml
vendored
8
.github/workflows/codeql-analysis.yml
vendored
@@ -35,11 +35,11 @@ jobs:
|
||||
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
# Initializes the CodeQL tools for scanning.
|
||||
- name: Initialize CodeQL
|
||||
uses: github/codeql-action/init@v1
|
||||
uses: github/codeql-action/init@v2
|
||||
with:
|
||||
languages: ${{ matrix.language }}
|
||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||
@@ -50,7 +50,7 @@ jobs:
|
||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||
# If this step fails, then you should remove it and run the build manually (see below)
|
||||
- name: Autobuild
|
||||
uses: github/codeql-action/autobuild@v1
|
||||
uses: github/codeql-action/autobuild@v2
|
||||
|
||||
# ℹ️ Command-line programs to run using the OS shell.
|
||||
# 📚 https://git.io/JvXDl
|
||||
@@ -64,4 +64,4 @@ jobs:
|
||||
# make release
|
||||
|
||||
- name: Perform CodeQL Analysis
|
||||
uses: github/codeql-action/analyze@v1
|
||||
uses: github/codeql-action/analyze@v2
|
||||
|
||||
35
.github/workflows/go.yml
vendored
35
.github/workflows/go.yml
vendored
@@ -11,14 +11,16 @@ jobs:
|
||||
name: Linux
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ^1.15
|
||||
id: go
|
||||
|
||||
- name: Check out code into the Go module directory
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
go-version: ^1.16
|
||||
check-latest: true
|
||||
cache: true
|
||||
id: go
|
||||
|
||||
- name: Get dependencies
|
||||
run: |
|
||||
@@ -28,25 +30,28 @@ jobs:
|
||||
run: |
|
||||
go vet -stdmethods=false $(go list ./...)
|
||||
go install mvdan.cc/gofumpt@latest
|
||||
test -z "$(gofumpt -s -l -extra .)" || echo "Please run 'gofumpt -l -w -extra .'"
|
||||
test -z "$(gofumpt -l -extra .)" || echo "Please run 'gofumpt -l -w -extra .'"
|
||||
|
||||
- name: Test
|
||||
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./...
|
||||
|
||||
- name: Codecov
|
||||
uses: codecov/codecov-action@v2
|
||||
uses: codecov/codecov-action@v3
|
||||
|
||||
test-win:
|
||||
name: Windows
|
||||
runs-on: windows-latest
|
||||
steps:
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ^1.15
|
||||
|
||||
- name: Checkout codebase
|
||||
uses: actions/checkout@v2
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Go 1.x
|
||||
uses: actions/setup-go@v3
|
||||
with:
|
||||
# use 1.16 to guarantee Go 1.16 compatibility
|
||||
go-version: 1.16
|
||||
check-latest: true
|
||||
cache: true
|
||||
|
||||
- name: Test
|
||||
run: |
|
||||
|
||||
2
.github/workflows/issue-translator.yml
vendored
2
.github/workflows/issue-translator.yml
vendored
@@ -9,7 +9,7 @@ jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: tomsun28/issues-translate-action@v2.6
|
||||
- uses: usthe/issues-translate-action@v2.7
|
||||
with:
|
||||
IS_MODIFY_TITLE: true
|
||||
# not require, default false, . Decide whether to modify the issue title
|
||||
|
||||
6
.github/workflows/issues.yml
vendored
6
.github/workflows/issues.yml
vendored
@@ -7,10 +7,10 @@ jobs:
|
||||
close-issues:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/stale@v3
|
||||
- uses: actions/stale@v6
|
||||
with:
|
||||
days-before-issue-stale: 30
|
||||
days-before-issue-close: 14
|
||||
days-before-issue-stale: 365
|
||||
days-before-issue-close: 90
|
||||
stale-issue-label: "stale"
|
||||
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
|
||||
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
|
||||
|
||||
28
.github/workflows/release.yaml
vendored
Normal file
28
.github/workflows/release.yaml
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "tools/goctl/*"
|
||||
jobs:
|
||||
releases-matrix:
|
||||
name: Release goctl binary
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
# build and publish in parallel: linux/386, linux/amd64, linux/arm64,
|
||||
# windows/386, windows/amd64, windows/arm64, darwin/amd64, darwin/arm64
|
||||
goos: [ linux, windows, darwin ]
|
||||
goarch: [ "386", amd64, arm64 ]
|
||||
exclude:
|
||||
- goarch: "386"
|
||||
goos: darwin
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: zeromicro/go-zero-release-action@master
|
||||
with:
|
||||
github_token: ${{ secrets.GITHUB_TOKEN }}
|
||||
goos: ${{ matrix.goos }}
|
||||
goarch: ${{ matrix.goarch }}
|
||||
goversion: "https://dl.google.com/go/go1.17.5.linux-amd64.tar.gz"
|
||||
project_path: "tools/goctl"
|
||||
binary_name: "goctl"
|
||||
extra_files: tools/goctl/readme.md tools/goctl/readme-cn.md
|
||||
2
.github/workflows/reviewdog.yml
vendored
2
.github/workflows/reviewdog.yml
vendored
@@ -5,7 +5,7 @@ jobs:
|
||||
name: runner / staticcheck
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions/checkout@v3
|
||||
- uses: reviewdog/action-staticcheck@v1
|
||||
with:
|
||||
github_token: ${{ secrets.github_token }}
|
||||
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -17,10 +17,12 @@
|
||||
|
||||
# for test purpose
|
||||
**/adhoc
|
||||
**/testdata
|
||||
go.work
|
||||
go.work.sum
|
||||
|
||||
# gitlab ci
|
||||
.cache
|
||||
.golangci.yml
|
||||
|
||||
# vim auto backup file
|
||||
*~
|
||||
|
||||
28
ROADMAP.md
28
ROADMAP.md
@@ -1,28 +0,0 @@
|
||||
# go-zero Roadmap
|
||||
|
||||
This document defines a high level roadmap for go-zero development and upcoming releases.
|
||||
Community and contributor involvement is vital for successfully implementing all desired items for each release.
|
||||
We hope that the items listed below will inspire further engagement from the community to keep go-zero progressing and shipping exciting and valuable features.
|
||||
|
||||
## 2021 Q2
|
||||
- [x] Support service discovery through K8S client api
|
||||
- [x] Log full sql statements for easier sql problem solving
|
||||
|
||||
## 2021 Q3
|
||||
- [x] Support `goctl model pg` to support PostgreSQL code generation
|
||||
- [x] Adapt builtin tracing mechanism to opentracing solutions
|
||||
|
||||
## 2021 Q4
|
||||
- [x] Support `username/password` authentication in ETCD
|
||||
- [x] Support `SSL/TLS` in ETCD
|
||||
- [x] Support `SSL/TLS` in `zRPC`
|
||||
- [x] Support `TLS` in redis connections
|
||||
- [x] Support `goctl bug` to report bugs conveniently
|
||||
|
||||
## 2022
|
||||
- [x] Support `context` in redis related methods for timeout and tracing
|
||||
- [x] Support `context` in sql related methods for timeout and tracing
|
||||
- [ ] Support `context` in mongodb related methods for timeout and tracing
|
||||
- [x] Add `httpc.Do` with HTTP call governance, like circuit breaker etc.
|
||||
- [ ] Support `goctl doctor` command to report potential issues for given service
|
||||
- [ ] Support `goctl mock` command to start a mocking server with given `.api` file
|
||||
@@ -69,11 +69,8 @@ func (f *Filter) Exists(data []byte) (bool, error) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !isSet {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return isSet, nil
|
||||
}
|
||||
|
||||
func (f *Filter) getLocations(data []byte) []uint {
|
||||
|
||||
@@ -5,12 +5,12 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/mathx"
|
||||
"github.com/zeromicro/go-zero/core/proc"
|
||||
"github.com/zeromicro/go-zero/core/stat"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -198,7 +198,7 @@ type errorWindow struct {
|
||||
|
||||
func (ew *errorWindow) add(reason string) {
|
||||
ew.lock.Lock()
|
||||
ew.reasons[ew.index] = fmt.Sprintf("%s %s", timex.Time().Format(timeFormat), reason)
|
||||
ew.reasons[ew.index] = fmt.Sprintf("%s %s", time.Now().Format(timeFormat), reason)
|
||||
ew.index = (ew.index + 1) % numHistoryReasons
|
||||
ew.count = mathx.MinInt(ew.count+1, numHistoryReasons)
|
||||
ew.lock.Unlock()
|
||||
|
||||
@@ -20,16 +20,16 @@ func (b noOpBreaker) Do(req func() error) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
func (b noOpBreaker) DoWithAcceptable(req func() error, acceptable Acceptable) error {
|
||||
func (b noOpBreaker) DoWithAcceptable(req func() error, _ Acceptable) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
func (b noOpBreaker) DoWithFallback(req func() error, fallback func(err error) error) error {
|
||||
func (b noOpBreaker) DoWithFallback(req func() error, _ func(err error) error) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, fallback func(err error) error,
|
||||
acceptable Acceptable) error {
|
||||
func (b noOpBreaker) DoWithFallbackAcceptable(req func() error, _ func(err error) error,
|
||||
_ Acceptable) error {
|
||||
return req()
|
||||
}
|
||||
|
||||
@@ -38,5 +38,5 @@ type nopPromise struct{}
|
||||
func (p nopPromise) Accept() {
|
||||
}
|
||||
|
||||
func (p nopPromise) Reject(reason string) {
|
||||
func (p nopPromise) Reject(_ string) {
|
||||
}
|
||||
|
||||
@@ -32,9 +32,11 @@ func NewECBEncrypter(b cipher.Block) cipher.BlockMode {
|
||||
return (*ecbEncrypter)(newECB(b))
|
||||
}
|
||||
|
||||
// BlockSize returns the mode's block size.
|
||||
func (x *ecbEncrypter) BlockSize() int { return x.blockSize }
|
||||
|
||||
// why we don't return error is because cipher.BlockMode doesn't allow this
|
||||
// CryptBlocks encrypts a number of blocks. The length of src must be a multiple of
|
||||
// the block size. Dst and src must overlap entirely or not at all.
|
||||
func (x *ecbEncrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
logx.Error("crypto/cipher: input not full blocks")
|
||||
@@ -59,11 +61,13 @@ func NewECBDecrypter(b cipher.Block) cipher.BlockMode {
|
||||
return (*ecbDecrypter)(newECB(b))
|
||||
}
|
||||
|
||||
// BlockSize returns the mode's block size.
|
||||
func (x *ecbDecrypter) BlockSize() int {
|
||||
return x.blockSize
|
||||
}
|
||||
|
||||
// why we don't return error is because cipher.BlockMode doesn't allow this
|
||||
// CryptBlocks decrypts a number of blocks. The length of src must be a multiple of
|
||||
// the block size. Dst and src must overlap entirely or not at all.
|
||||
func (x *ecbDecrypter) CryptBlocks(dst, src []byte) {
|
||||
if len(src)%x.blockSize != 0 {
|
||||
logx.Error("crypto/cipher: input not full blocks")
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package codec
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
|
||||
@@ -10,7 +11,8 @@ import (
|
||||
func TestAesEcb(t *testing.T) {
|
||||
var (
|
||||
key = []byte("q4t7w!z%C*F-JaNdRgUjXn2r5u8x/A?D")
|
||||
val = []byte("hello")
|
||||
val = []byte("helloworld")
|
||||
valLong = []byte("helloworldlong..")
|
||||
badKey1 = []byte("aaaaaaaaa")
|
||||
// more than 32 chars
|
||||
badKey2 = []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
|
||||
@@ -31,6 +33,39 @@ func TestAesEcb(t *testing.T) {
|
||||
src, err := EcbDecrypt(key, dst)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, val, src)
|
||||
block, err := aes.NewCipher(key)
|
||||
assert.NoError(t, err)
|
||||
encrypter := NewECBEncrypter(block)
|
||||
assert.Equal(t, 16, encrypter.BlockSize())
|
||||
decrypter := NewECBDecrypter(block)
|
||||
assert.Equal(t, 16, decrypter.BlockSize())
|
||||
|
||||
dst = make([]byte, 8)
|
||||
encrypter.CryptBlocks(dst, val)
|
||||
for _, b := range dst {
|
||||
assert.Equal(t, byte(0), b)
|
||||
}
|
||||
|
||||
dst = make([]byte, 8)
|
||||
encrypter.CryptBlocks(dst, valLong)
|
||||
for _, b := range dst {
|
||||
assert.Equal(t, byte(0), b)
|
||||
}
|
||||
|
||||
dst = make([]byte, 8)
|
||||
decrypter.CryptBlocks(dst, val)
|
||||
for _, b := range dst {
|
||||
assert.Equal(t, byte(0), b)
|
||||
}
|
||||
|
||||
dst = make([]byte, 8)
|
||||
decrypter.CryptBlocks(dst, valLong)
|
||||
for _, b := range dst {
|
||||
assert.Equal(t, byte(0), b)
|
||||
}
|
||||
|
||||
_, err = EcbEncryptBase64("cTR0N3dDKkYtSmFOZFJnVWpYbjJyNXU4eC9BP0QK", "aGVsbG93b3JsZGxvbmcuLgo=")
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
func TestAesEcbBase64(t *testing.T) {
|
||||
|
||||
@@ -80,3 +80,17 @@ func TestKeyBytes(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, len(key.Bytes()) > 0)
|
||||
}
|
||||
|
||||
func TestDHOnErrors(t *testing.T) {
|
||||
key, err := GenerateKey()
|
||||
assert.Nil(t, err)
|
||||
assert.NotEmpty(t, key.Bytes())
|
||||
_, err = ComputeKey(key.PubKey, key.PriKey)
|
||||
assert.NoError(t, err)
|
||||
_, err = ComputeKey(nil, key.PriKey)
|
||||
assert.Error(t, err)
|
||||
_, err = ComputeKey(key.PubKey, nil)
|
||||
assert.Error(t, err)
|
||||
|
||||
assert.NotNil(t, NewPublicKey([]byte("")))
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -48,7 +48,7 @@ type (
|
||||
|
||||
// NewRsaDecrypter returns a RsaDecrypter with the given file.
|
||||
func NewRsaDecrypter(file string) (RsaDecrypter, error) {
|
||||
content, err := ioutil.ReadFile(file)
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ type (
|
||||
// CacheOption defines the method to customize a Cache.
|
||||
CacheOption func(cache *Cache)
|
||||
|
||||
// A Cache object is a in-memory cache.
|
||||
// A Cache object is an in-memory cache.
|
||||
Cache struct {
|
||||
name string
|
||||
lock sync.Mutex
|
||||
@@ -98,13 +98,18 @@ func (c *Cache) Get(key string) (interface{}, bool) {
|
||||
|
||||
// Set sets value into c with key.
|
||||
func (c *Cache) Set(key string, value interface{}) {
|
||||
c.SetWithExpire(key, value, c.expire)
|
||||
}
|
||||
|
||||
// SetWithExpire sets value into c with key and expire with the given value.
|
||||
func (c *Cache) SetWithExpire(key string, value interface{}, expire time.Duration) {
|
||||
c.lock.Lock()
|
||||
_, ok := c.data[key]
|
||||
c.data[key] = value
|
||||
c.lruCache.add(key)
|
||||
c.lock.Unlock()
|
||||
|
||||
expiry := c.unstableExpiry.AroundDuration(c.expire)
|
||||
expiry := c.unstableExpiry.AroundDuration(expire)
|
||||
if ok {
|
||||
c.timingWheel.MoveTimer(key, expiry)
|
||||
} else {
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestCacheSet(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
|
||||
cache.Set("first", "first element")
|
||||
cache.Set("second", "second element")
|
||||
cache.SetWithExpire("second", "second element", time.Second*3)
|
||||
|
||||
value, ok := cache.Get("first")
|
||||
assert.True(t, ok)
|
||||
|
||||
@@ -6,7 +6,7 @@ import "sync"
|
||||
type Ring struct {
|
||||
elements []interface{}
|
||||
index int
|
||||
lock sync.Mutex
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
// NewRing returns a Ring object with the given size n.
|
||||
@@ -31,8 +31,8 @@ func (r *Ring) Add(v interface{}) {
|
||||
|
||||
// Take takes all items from r.
|
||||
func (r *Ring) Take() []interface{} {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
|
||||
var size int
|
||||
var start int
|
||||
|
||||
@@ -68,6 +68,24 @@ func (m *SafeMap) Get(key interface{}) (interface{}, bool) {
|
||||
return val, ok
|
||||
}
|
||||
|
||||
// Range calls f sequentially for each key and value present in the map.
|
||||
// If f returns false, range stops the iteration.
|
||||
func (m *SafeMap) Range(f func(key, val interface{}) bool) {
|
||||
m.lock.RLock()
|
||||
defer m.lock.RUnlock()
|
||||
|
||||
for k, v := range m.dirtyOld {
|
||||
if !f(k, v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
for k, v := range m.dirtyNew {
|
||||
if !f(k, v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set sets the value into m with the given key.
|
||||
func (m *SafeMap) Set(key, value interface{}) {
|
||||
m.lock.Lock()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package collection
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -107,3 +108,42 @@ func testSafeMapWithParameters(t *testing.T, size, exception int) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSafeMap_Range(t *testing.T) {
|
||||
const (
|
||||
size = 100000
|
||||
exception1 = 5
|
||||
exception2 = 500
|
||||
)
|
||||
|
||||
m := NewSafeMap()
|
||||
newMap := NewSafeMap()
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
m.Set(i, i)
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
if i%exception1 == 0 {
|
||||
m.Del(i)
|
||||
}
|
||||
}
|
||||
|
||||
for i := size; i < size<<1; i++ {
|
||||
m.Set(i, i)
|
||||
}
|
||||
for i := size; i < size<<1; i++ {
|
||||
if i%exception2 != 0 {
|
||||
m.Del(i)
|
||||
}
|
||||
}
|
||||
|
||||
var count int32
|
||||
m.Range(func(k, v interface{}) bool {
|
||||
atomic.AddInt32(&count, 1)
|
||||
newMap.Set(k, v)
|
||||
return true
|
||||
})
|
||||
assert.Equal(t, int(atomic.LoadInt32(&count)), m.Size())
|
||||
assert.Equal(t, m.dirtyNew, newMap.dirtyNew)
|
||||
assert.Equal(t, m.dirtyOld, newMap.dirtyOld)
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ func NewSet() *Set {
|
||||
}
|
||||
}
|
||||
|
||||
// NewUnmanagedSet returns a unmanaged Set, which can put values with different types.
|
||||
// NewUnmanagedSet returns an unmanaged Set, which can put values with different types.
|
||||
func NewUnmanagedSet() *Set {
|
||||
return &Set{
|
||||
data: make(map[interface{}]lang.PlaceholderType),
|
||||
|
||||
@@ -2,6 +2,7 @@ package collection
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
@@ -12,6 +13,11 @@ import (
|
||||
|
||||
const drainWorkers = 8
|
||||
|
||||
var (
|
||||
ErrClosed = errors.New("TimingWheel is closed already")
|
||||
ErrArgument = errors.New("incorrect task argument")
|
||||
)
|
||||
|
||||
type (
|
||||
// Execute defines the method to execute the task.
|
||||
Execute func(key, value interface{})
|
||||
@@ -59,14 +65,16 @@ type (
|
||||
// NewTimingWheel returns a TimingWheel.
|
||||
func NewTimingWheel(interval time.Duration, numSlots int, execute Execute) (*TimingWheel, error) {
|
||||
if interval <= 0 || numSlots <= 0 || execute == nil {
|
||||
return nil, fmt.Errorf("interval: %v, slots: %d, execute: %p", interval, numSlots, execute)
|
||||
return nil, fmt.Errorf("interval: %v, slots: %d, execute: %p",
|
||||
interval, numSlots, execute)
|
||||
}
|
||||
|
||||
return newTimingWheelWithClock(interval, numSlots, execute, timex.NewTicker(interval))
|
||||
return NewTimingWheelWithTicker(interval, numSlots, execute, timex.NewTicker(interval))
|
||||
}
|
||||
|
||||
func newTimingWheelWithClock(interval time.Duration, numSlots int, execute Execute, ticker timex.Ticker) (
|
||||
*TimingWheel, error) {
|
||||
// NewTimingWheelWithTicker returns a TimingWheel with the given ticker.
|
||||
func NewTimingWheelWithTicker(interval time.Duration, numSlots int, execute Execute,
|
||||
ticker timex.Ticker) (*TimingWheel, error) {
|
||||
tw := &TimingWheel{
|
||||
interval: interval,
|
||||
ticker: ticker,
|
||||
@@ -89,47 +97,67 @@ func newTimingWheelWithClock(interval time.Duration, numSlots int, execute Execu
|
||||
}
|
||||
|
||||
// Drain drains all items and executes them.
|
||||
func (tw *TimingWheel) Drain(fn func(key, value interface{})) {
|
||||
tw.drainChannel <- fn
|
||||
func (tw *TimingWheel) Drain(fn func(key, value interface{})) error {
|
||||
select {
|
||||
case tw.drainChannel <- fn:
|
||||
return nil
|
||||
case <-tw.stopChannel:
|
||||
return ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
// MoveTimer moves the task with the given key to the given delay.
|
||||
func (tw *TimingWheel) MoveTimer(key interface{}, delay time.Duration) {
|
||||
func (tw *TimingWheel) MoveTimer(key interface{}, delay time.Duration) error {
|
||||
if delay <= 0 || key == nil {
|
||||
return
|
||||
return ErrArgument
|
||||
}
|
||||
|
||||
tw.moveChannel <- baseEntry{
|
||||
select {
|
||||
case tw.moveChannel <- baseEntry{
|
||||
delay: delay,
|
||||
key: key,
|
||||
}:
|
||||
return nil
|
||||
case <-tw.stopChannel:
|
||||
return ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveTimer removes the task with the given key.
|
||||
func (tw *TimingWheel) RemoveTimer(key interface{}) {
|
||||
func (tw *TimingWheel) RemoveTimer(key interface{}) error {
|
||||
if key == nil {
|
||||
return
|
||||
return ErrArgument
|
||||
}
|
||||
|
||||
tw.removeChannel <- key
|
||||
select {
|
||||
case tw.removeChannel <- key:
|
||||
return nil
|
||||
case <-tw.stopChannel:
|
||||
return ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
// SetTimer sets the task value with the given key to the delay.
|
||||
func (tw *TimingWheel) SetTimer(key, value interface{}, delay time.Duration) {
|
||||
func (tw *TimingWheel) SetTimer(key, value interface{}, delay time.Duration) error {
|
||||
if delay <= 0 || key == nil {
|
||||
return
|
||||
return ErrArgument
|
||||
}
|
||||
|
||||
tw.setChannel <- timingEntry{
|
||||
select {
|
||||
case tw.setChannel <- timingEntry{
|
||||
baseEntry: baseEntry{
|
||||
delay: delay,
|
||||
key: key,
|
||||
},
|
||||
value: value,
|
||||
}:
|
||||
return nil
|
||||
case <-tw.stopChannel:
|
||||
return ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops tw.
|
||||
// Stop stops tw. No more actions after stopping a TimingWheel.
|
||||
func (tw *TimingWheel) Stop() {
|
||||
close(tw.stopChannel)
|
||||
}
|
||||
|
||||
@@ -26,9 +26,8 @@ func TestNewTimingWheel(t *testing.T) {
|
||||
|
||||
func TestTimingWheel_Drain(t *testing.T) {
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 10, func(k, v interface{}) {
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {
|
||||
}, ticker)
|
||||
defer tw.Stop()
|
||||
tw.SetTimer("first", 3, testStep*4)
|
||||
tw.SetTimer("second", 5, testStep*7)
|
||||
tw.SetTimer("third", 7, testStep*7)
|
||||
@@ -56,12 +55,14 @@ func TestTimingWheel_Drain(t *testing.T) {
|
||||
})
|
||||
time.Sleep(time.Millisecond * 100)
|
||||
assert.Equal(t, 0, count)
|
||||
tw.Stop()
|
||||
assert.Equal(t, ErrClosed, tw.Drain(func(key, value interface{}) {}))
|
||||
}
|
||||
|
||||
func TestTimingWheel_SetTimerSoon(t *testing.T) {
|
||||
run := syncx.NewAtomicBool()
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 10, func(k, v interface{}) {
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {
|
||||
assert.True(t, run.CompareAndSwap(false, true))
|
||||
assert.Equal(t, "any", k)
|
||||
assert.Equal(t, 3, v.(int))
|
||||
@@ -77,7 +78,7 @@ func TestTimingWheel_SetTimerSoon(t *testing.T) {
|
||||
func TestTimingWheel_SetTimerTwice(t *testing.T) {
|
||||
run := syncx.NewAtomicBool()
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 10, func(k, v interface{}) {
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {
|
||||
assert.True(t, run.CompareAndSwap(false, true))
|
||||
assert.Equal(t, "any", k)
|
||||
assert.Equal(t, 5, v.(int))
|
||||
@@ -95,23 +96,29 @@ func TestTimingWheel_SetTimerTwice(t *testing.T) {
|
||||
|
||||
func TestTimingWheel_SetTimerWrongDelay(t *testing.T) {
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 10, func(k, v interface{}) {}, ticker)
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {}, ticker)
|
||||
defer tw.Stop()
|
||||
assert.NotPanics(t, func() {
|
||||
tw.SetTimer("any", 3, -testStep)
|
||||
})
|
||||
}
|
||||
|
||||
func TestTimingWheel_SetTimerAfterClose(t *testing.T) {
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {}, ticker)
|
||||
tw.Stop()
|
||||
assert.Equal(t, ErrClosed, tw.SetTimer("any", 3, testStep))
|
||||
}
|
||||
|
||||
func TestTimingWheel_MoveTimer(t *testing.T) {
|
||||
run := syncx.NewAtomicBool()
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 3, func(k, v interface{}) {
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 3, func(k, v interface{}) {
|
||||
assert.True(t, run.CompareAndSwap(false, true))
|
||||
assert.Equal(t, "any", k)
|
||||
assert.Equal(t, 3, v.(int))
|
||||
ticker.Done()
|
||||
}, ticker)
|
||||
defer tw.Stop()
|
||||
tw.SetTimer("any", 3, testStep*4)
|
||||
tw.MoveTimer("any", testStep*7)
|
||||
tw.MoveTimer("any", -testStep)
|
||||
@@ -125,12 +132,14 @@ func TestTimingWheel_MoveTimer(t *testing.T) {
|
||||
}
|
||||
assert.Nil(t, ticker.Wait(waitTime))
|
||||
assert.True(t, run.True())
|
||||
tw.Stop()
|
||||
assert.Equal(t, ErrClosed, tw.MoveTimer("any", time.Millisecond))
|
||||
}
|
||||
|
||||
func TestTimingWheel_MoveTimerSoon(t *testing.T) {
|
||||
run := syncx.NewAtomicBool()
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 3, func(k, v interface{}) {
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 3, func(k, v interface{}) {
|
||||
assert.True(t, run.CompareAndSwap(false, true))
|
||||
assert.Equal(t, "any", k)
|
||||
assert.Equal(t, 3, v.(int))
|
||||
@@ -146,7 +155,7 @@ func TestTimingWheel_MoveTimerSoon(t *testing.T) {
|
||||
func TestTimingWheel_MoveTimerEarlier(t *testing.T) {
|
||||
run := syncx.NewAtomicBool()
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 10, func(k, v interface{}) {
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {
|
||||
assert.True(t, run.CompareAndSwap(false, true))
|
||||
assert.Equal(t, "any", k)
|
||||
assert.Equal(t, 3, v.(int))
|
||||
@@ -164,7 +173,7 @@ func TestTimingWheel_MoveTimerEarlier(t *testing.T) {
|
||||
|
||||
func TestTimingWheel_RemoveTimer(t *testing.T) {
|
||||
ticker := timex.NewFakeTicker()
|
||||
tw, _ := newTimingWheelWithClock(testStep, 10, func(k, v interface{}) {}, ticker)
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {}, ticker)
|
||||
tw.SetTimer("any", 3, testStep)
|
||||
assert.NotPanics(t, func() {
|
||||
tw.RemoveTimer("any")
|
||||
@@ -175,6 +184,7 @@ func TestTimingWheel_RemoveTimer(t *testing.T) {
|
||||
ticker.Tick()
|
||||
}
|
||||
tw.Stop()
|
||||
assert.Equal(t, ErrClosed, tw.RemoveTimer("any"))
|
||||
}
|
||||
|
||||
func TestTimingWheel_SetTimer(t *testing.T) {
|
||||
@@ -226,7 +236,7 @@ func TestTimingWheel_SetTimer(t *testing.T) {
|
||||
}
|
||||
var actual int32
|
||||
done := make(chan lang.PlaceholderType)
|
||||
tw, err := newTimingWheelWithClock(testStep, test.slots, func(key, value interface{}) {
|
||||
tw, err := NewTimingWheelWithTicker(testStep, test.slots, func(key, value interface{}) {
|
||||
assert.Equal(t, 1, key.(int))
|
||||
assert.Equal(t, 2, value.(int))
|
||||
actual = atomic.LoadInt32(&count)
|
||||
@@ -307,7 +317,7 @@ func TestTimingWheel_SetAndMoveThenStart(t *testing.T) {
|
||||
}
|
||||
var actual int32
|
||||
done := make(chan lang.PlaceholderType)
|
||||
tw, err := newTimingWheelWithClock(testStep, test.slots, func(key, value interface{}) {
|
||||
tw, err := NewTimingWheelWithTicker(testStep, test.slots, func(key, value interface{}) {
|
||||
actual = atomic.LoadInt32(&count)
|
||||
close(done)
|
||||
}, ticker)
|
||||
@@ -395,7 +405,7 @@ func TestTimingWheel_SetAndMoveTwice(t *testing.T) {
|
||||
}
|
||||
var actual int32
|
||||
done := make(chan lang.PlaceholderType)
|
||||
tw, err := newTimingWheelWithClock(testStep, test.slots, func(key, value interface{}) {
|
||||
tw, err := NewTimingWheelWithTicker(testStep, test.slots, func(key, value interface{}) {
|
||||
actual = atomic.LoadInt32(&count)
|
||||
close(done)
|
||||
}, ticker)
|
||||
@@ -476,7 +486,7 @@ func TestTimingWheel_ElapsedAndSet(t *testing.T) {
|
||||
}
|
||||
var actual int32
|
||||
done := make(chan lang.PlaceholderType)
|
||||
tw, err := newTimingWheelWithClock(testStep, test.slots, func(key, value interface{}) {
|
||||
tw, err := NewTimingWheelWithTicker(testStep, test.slots, func(key, value interface{}) {
|
||||
actual = atomic.LoadInt32(&count)
|
||||
close(done)
|
||||
}, ticker)
|
||||
@@ -567,7 +577,7 @@ func TestTimingWheel_ElapsedAndSetThenMove(t *testing.T) {
|
||||
}
|
||||
var actual int32
|
||||
done := make(chan lang.PlaceholderType)
|
||||
tw, err := newTimingWheelWithClock(testStep, test.slots, func(key, value interface{}) {
|
||||
tw, err := NewTimingWheelWithTicker(testStep, test.slots, func(key, value interface{}) {
|
||||
actual = atomic.LoadInt32(&count)
|
||||
close(done)
|
||||
}, ticker)
|
||||
@@ -602,7 +612,7 @@ func TestMoveAndRemoveTask(t *testing.T) {
|
||||
}
|
||||
}
|
||||
var keys []int
|
||||
tw, _ := newTimingWheelWithClock(testStep, 10, func(k, v interface{}) {
|
||||
tw, _ := NewTimingWheelWithTicker(testStep, 10, func(k, v interface{}) {
|
||||
assert.Equal(t, "any", k)
|
||||
assert.Equal(t, 3, v.(int))
|
||||
keys = append(keys, v.(int))
|
||||
|
||||
73
core/color/color.go
Normal file
73
core/color/color.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package color
|
||||
|
||||
import "github.com/fatih/color"
|
||||
|
||||
const (
|
||||
// NoColor is no color for both foreground and background.
|
||||
NoColor Color = iota
|
||||
// FgBlack is the foreground color black.
|
||||
FgBlack
|
||||
// FgRed is the foreground color red.
|
||||
FgRed
|
||||
// FgGreen is the foreground color green.
|
||||
FgGreen
|
||||
// FgYellow is the foreground color yellow.
|
||||
FgYellow
|
||||
// FgBlue is the foreground color blue.
|
||||
FgBlue
|
||||
// FgMagenta is the foreground color magenta.
|
||||
FgMagenta
|
||||
// FgCyan is the foreground color cyan.
|
||||
FgCyan
|
||||
// FgWhite is the foreground color white.
|
||||
FgWhite
|
||||
|
||||
// BgBlack is the background color black.
|
||||
BgBlack
|
||||
// BgRed is the background color red.
|
||||
BgRed
|
||||
// BgGreen is the background color green.
|
||||
BgGreen
|
||||
// BgYellow is the background color yellow.
|
||||
BgYellow
|
||||
// BgBlue is the background color blue.
|
||||
BgBlue
|
||||
// BgMagenta is the background color magenta.
|
||||
BgMagenta
|
||||
// BgCyan is the background color cyan.
|
||||
BgCyan
|
||||
// BgWhite is the background color white.
|
||||
BgWhite
|
||||
)
|
||||
|
||||
var colors = map[Color][]color.Attribute{
|
||||
FgBlack: {color.FgBlack, color.Bold},
|
||||
FgRed: {color.FgRed, color.Bold},
|
||||
FgGreen: {color.FgGreen, color.Bold},
|
||||
FgYellow: {color.FgYellow, color.Bold},
|
||||
FgBlue: {color.FgBlue, color.Bold},
|
||||
FgMagenta: {color.FgMagenta, color.Bold},
|
||||
FgCyan: {color.FgCyan, color.Bold},
|
||||
FgWhite: {color.FgWhite, color.Bold},
|
||||
BgBlack: {color.BgBlack, color.FgHiWhite, color.Bold},
|
||||
BgRed: {color.BgRed, color.FgHiWhite, color.Bold},
|
||||
BgGreen: {color.BgGreen, color.FgHiWhite, color.Bold},
|
||||
BgYellow: {color.BgHiYellow, color.FgHiBlack, color.Bold},
|
||||
BgBlue: {color.BgBlue, color.FgHiWhite, color.Bold},
|
||||
BgMagenta: {color.BgMagenta, color.FgHiWhite, color.Bold},
|
||||
BgCyan: {color.BgCyan, color.FgHiWhite, color.Bold},
|
||||
BgWhite: {color.BgHiWhite, color.FgHiBlack, color.Bold},
|
||||
}
|
||||
|
||||
type Color uint32
|
||||
|
||||
// WithColor returns a string with the given color applied.
|
||||
func WithColor(text string, colour Color) string {
|
||||
c := color.New(colors[colour]...)
|
||||
return c.Sprint(text)
|
||||
}
|
||||
|
||||
// WithColorPadding returns a string with the given color applied with leading and trailing spaces.
|
||||
func WithColorPadding(text string, colour Color) string {
|
||||
return WithColor(" "+text+" ", colour)
|
||||
}
|
||||
17
core/color/color_test.go
Normal file
17
core/color/color_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithColor(t *testing.T) {
|
||||
output := WithColor("Hello", BgRed)
|
||||
assert.Equal(t, "Hello", output)
|
||||
}
|
||||
|
||||
func TestWithColorPadding(t *testing.T) {
|
||||
output := WithColorPadding("Hello", BgRed)
|
||||
assert.Equal(t, " Hello ", output)
|
||||
}
|
||||
@@ -2,28 +2,38 @@ package conf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/jsonx"
|
||||
"github.com/zeromicro/go-zero/core/mapping"
|
||||
"github.com/zeromicro/go-zero/internal/encoding"
|
||||
)
|
||||
|
||||
var loaders = map[string]func([]byte, interface{}) error{
|
||||
".json": LoadConfigFromJsonBytes,
|
||||
".yaml": LoadConfigFromYamlBytes,
|
||||
".yml": LoadConfigFromYamlBytes,
|
||||
".json": LoadFromJsonBytes,
|
||||
".toml": LoadFromTomlBytes,
|
||||
".yaml": LoadFromYamlBytes,
|
||||
".yml": LoadFromYamlBytes,
|
||||
}
|
||||
|
||||
// LoadConfig loads config into v from file, .json, .yaml and .yml are acceptable.
|
||||
func LoadConfig(file string, v interface{}, opts ...Option) error {
|
||||
content, err := ioutil.ReadFile(file)
|
||||
type fieldInfo struct {
|
||||
name string
|
||||
kind reflect.Kind
|
||||
children map[string]fieldInfo
|
||||
}
|
||||
|
||||
// Load loads config into v from file, .json, .yaml and .yml are acceptable.
|
||||
func Load(file string, v interface{}, opts ...Option) error {
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
loader, ok := loaders[path.Ext(file)]
|
||||
loader, ok := loaders[strings.ToLower(path.Ext(file))]
|
||||
if !ok {
|
||||
return fmt.Errorf("unrecognized file type: %s", file)
|
||||
}
|
||||
@@ -40,19 +50,158 @@ func LoadConfig(file string, v interface{}, opts ...Option) error {
|
||||
return loader(content, v)
|
||||
}
|
||||
|
||||
// LoadConfig loads config into v from file, .json, .yaml and .yml are acceptable.
|
||||
// Deprecated: use Load instead.
|
||||
func LoadConfig(file string, v interface{}, opts ...Option) error {
|
||||
return Load(file, v, opts...)
|
||||
}
|
||||
|
||||
// LoadFromJsonBytes loads config into v from content json bytes.
|
||||
func LoadFromJsonBytes(content []byte, v interface{}) error {
|
||||
var m map[string]interface{}
|
||||
if err := jsonx.Unmarshal(content, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
finfo := buildFieldsInfo(reflect.TypeOf(v))
|
||||
lowerCaseKeyMap := toLowerCaseKeyMap(m, finfo)
|
||||
|
||||
return mapping.UnmarshalJsonMap(lowerCaseKeyMap, v, mapping.WithCanonicalKeyFunc(toLowerCase))
|
||||
}
|
||||
|
||||
// LoadConfigFromJsonBytes loads config into v from content json bytes.
|
||||
// Deprecated: use LoadFromJsonBytes instead.
|
||||
func LoadConfigFromJsonBytes(content []byte, v interface{}) error {
|
||||
return mapping.UnmarshalJsonBytes(content, v)
|
||||
return LoadFromJsonBytes(content, v)
|
||||
}
|
||||
|
||||
// LoadFromTomlBytes loads config into v from content toml bytes.
|
||||
func LoadFromTomlBytes(content []byte, v interface{}) error {
|
||||
b, err := encoding.TomlToJson(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return LoadFromJsonBytes(b, v)
|
||||
}
|
||||
|
||||
// LoadFromYamlBytes loads config into v from content yaml bytes.
|
||||
func LoadFromYamlBytes(content []byte, v interface{}) error {
|
||||
b, err := encoding.YamlToJson(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return LoadFromJsonBytes(b, v)
|
||||
}
|
||||
|
||||
// LoadConfigFromYamlBytes loads config into v from content yaml bytes.
|
||||
// Deprecated: use LoadFromYamlBytes instead.
|
||||
func LoadConfigFromYamlBytes(content []byte, v interface{}) error {
|
||||
return mapping.UnmarshalYamlBytes(content, v)
|
||||
return LoadFromYamlBytes(content, v)
|
||||
}
|
||||
|
||||
// MustLoad loads config into v from path, exits on error.
|
||||
func MustLoad(path string, v interface{}, opts ...Option) {
|
||||
if err := LoadConfig(path, v, opts...); err != nil {
|
||||
if err := Load(path, v, opts...); err != nil {
|
||||
log.Fatalf("error: config file %s, %s", path, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func buildFieldsInfo(tp reflect.Type) map[string]fieldInfo {
|
||||
tp = mapping.Deref(tp)
|
||||
|
||||
switch tp.Kind() {
|
||||
case reflect.Struct:
|
||||
return buildStructFieldsInfo(tp)
|
||||
case reflect.Array, reflect.Slice:
|
||||
return buildFieldsInfo(mapping.Deref(tp.Elem()))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func buildStructFieldsInfo(tp reflect.Type) map[string]fieldInfo {
|
||||
info := make(map[string]fieldInfo)
|
||||
|
||||
for i := 0; i < tp.NumField(); i++ {
|
||||
field := tp.Field(i)
|
||||
name := field.Name
|
||||
lowerCaseName := toLowerCase(name)
|
||||
ft := mapping.Deref(field.Type)
|
||||
|
||||
// flatten anonymous fields
|
||||
if field.Anonymous {
|
||||
if ft.Kind() == reflect.Struct {
|
||||
fields := buildFieldsInfo(ft)
|
||||
for k, v := range fields {
|
||||
info[k] = v
|
||||
}
|
||||
} else {
|
||||
info[lowerCaseName] = fieldInfo{
|
||||
name: name,
|
||||
kind: ft.Kind(),
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
var fields map[string]fieldInfo
|
||||
switch ft.Kind() {
|
||||
case reflect.Struct:
|
||||
fields = buildFieldsInfo(ft)
|
||||
case reflect.Array, reflect.Slice:
|
||||
fields = buildFieldsInfo(ft.Elem())
|
||||
case reflect.Map:
|
||||
fields = buildFieldsInfo(ft.Elem())
|
||||
}
|
||||
|
||||
info[lowerCaseName] = fieldInfo{
|
||||
name: name,
|
||||
kind: ft.Kind(),
|
||||
children: fields,
|
||||
}
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
func toLowerCase(s string) string {
|
||||
return strings.ToLower(s)
|
||||
}
|
||||
|
||||
func toLowerCaseInterface(v interface{}, info map[string]fieldInfo) interface{} {
|
||||
switch vv := v.(type) {
|
||||
case map[string]interface{}:
|
||||
return toLowerCaseKeyMap(vv, info)
|
||||
case []interface{}:
|
||||
var arr []interface{}
|
||||
for _, vvv := range vv {
|
||||
arr = append(arr, toLowerCaseInterface(vvv, info))
|
||||
}
|
||||
return arr
|
||||
default:
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
func toLowerCaseKeyMap(m map[string]interface{}, info map[string]fieldInfo) map[string]interface{} {
|
||||
res := make(map[string]interface{})
|
||||
|
||||
for k, v := range m {
|
||||
ti, ok := info[k]
|
||||
if ok {
|
||||
res[k] = toLowerCaseInterface(v, ti.children)
|
||||
continue
|
||||
}
|
||||
|
||||
lk := toLowerCase(k)
|
||||
if ti, ok = info[lk]; ok {
|
||||
res[lk] = toLowerCaseInterface(v, ti.children)
|
||||
} else {
|
||||
res[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -11,14 +10,14 @@ import (
|
||||
)
|
||||
|
||||
func TestLoadConfig_notExists(t *testing.T) {
|
||||
assert.NotNil(t, LoadConfig("not_a_file", nil))
|
||||
assert.NotNil(t, Load("not_a_file", nil))
|
||||
}
|
||||
|
||||
func TestLoadConfig_notRecogFile(t *testing.T) {
|
||||
filename, err := fs.TempFilenameWithText("hello")
|
||||
assert.Nil(t, err)
|
||||
defer os.Remove(filename)
|
||||
assert.NotNil(t, LoadConfig(filename, nil))
|
||||
assert.NotNil(t, Load(filename, nil))
|
||||
}
|
||||
|
||||
func TestConfigJson(t *testing.T) {
|
||||
@@ -57,6 +56,156 @@ func TestConfigJson(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromJsonBytesArray(t *testing.T) {
|
||||
input := []byte(`{"users": [{"name": "foo"}, {"Name": "bar"}]}`)
|
||||
var val struct {
|
||||
Users []struct {
|
||||
Name string
|
||||
}
|
||||
}
|
||||
|
||||
assert.NoError(t, LoadFromJsonBytes(input, &val))
|
||||
var expect []string
|
||||
for _, user := range val.Users {
|
||||
expect = append(expect, user.Name)
|
||||
}
|
||||
assert.EqualValues(t, []string{"foo", "bar"}, expect)
|
||||
}
|
||||
|
||||
func TestConfigToml(t *testing.T) {
|
||||
text := `a = "foo"
|
||||
b = 1
|
||||
c = "${FOO}"
|
||||
d = "abcd!@#$112"
|
||||
`
|
||||
os.Setenv("FOO", "2")
|
||||
defer os.Unsetenv("FOO")
|
||||
tmpfile, err := createTempFile(".toml", text)
|
||||
assert.Nil(t, err)
|
||||
defer os.Remove(tmpfile)
|
||||
|
||||
var val struct {
|
||||
A string `json:"a"`
|
||||
B int `json:"b"`
|
||||
C string `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
MustLoad(tmpfile, &val)
|
||||
assert.Equal(t, "foo", val.A)
|
||||
assert.Equal(t, 1, val.B)
|
||||
assert.Equal(t, "${FOO}", val.C)
|
||||
assert.Equal(t, "abcd!@#$112", val.D)
|
||||
}
|
||||
|
||||
func TestConfigOptional(t *testing.T) {
|
||||
text := `a = "foo"
|
||||
b = 1
|
||||
c = "FOO"
|
||||
d = "abcd"
|
||||
`
|
||||
tmpfile, err := createTempFile(".toml", text)
|
||||
assert.Nil(t, err)
|
||||
defer os.Remove(tmpfile)
|
||||
|
||||
var val struct {
|
||||
A string `json:"a"`
|
||||
B int `json:"b,optional"`
|
||||
C string `json:"c,optional=B"`
|
||||
D string `json:"d,optional=b"`
|
||||
}
|
||||
if assert.NoError(t, Load(tmpfile, &val)) {
|
||||
assert.Equal(t, "foo", val.A)
|
||||
assert.Equal(t, 1, val.B)
|
||||
assert.Equal(t, "FOO", val.C)
|
||||
assert.Equal(t, "abcd", val.D)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigJsonCanonical(t *testing.T) {
|
||||
text := []byte(`{"a": "foo", "B": "bar"}`)
|
||||
|
||||
var val1 struct {
|
||||
A string `json:"a"`
|
||||
B string `json:"b"`
|
||||
}
|
||||
var val2 struct {
|
||||
A string
|
||||
B string
|
||||
}
|
||||
assert.NoError(t, LoadFromJsonBytes(text, &val1))
|
||||
assert.Equal(t, "foo", val1.A)
|
||||
assert.Equal(t, "bar", val1.B)
|
||||
assert.NoError(t, LoadFromJsonBytes(text, &val2))
|
||||
assert.Equal(t, "foo", val2.A)
|
||||
assert.Equal(t, "bar", val2.B)
|
||||
}
|
||||
|
||||
func TestConfigTomlCanonical(t *testing.T) {
|
||||
text := []byte(`a = "foo"
|
||||
B = "bar"`)
|
||||
|
||||
var val1 struct {
|
||||
A string `json:"a"`
|
||||
B string `json:"b"`
|
||||
}
|
||||
var val2 struct {
|
||||
A string
|
||||
B string
|
||||
}
|
||||
assert.NoError(t, LoadFromTomlBytes(text, &val1))
|
||||
assert.Equal(t, "foo", val1.A)
|
||||
assert.Equal(t, "bar", val1.B)
|
||||
assert.NoError(t, LoadFromTomlBytes(text, &val2))
|
||||
assert.Equal(t, "foo", val2.A)
|
||||
assert.Equal(t, "bar", val2.B)
|
||||
}
|
||||
|
||||
func TestConfigYamlCanonical(t *testing.T) {
|
||||
text := []byte(`a: foo
|
||||
B: bar`)
|
||||
|
||||
var val1 struct {
|
||||
A string `json:"a"`
|
||||
B string `json:"b"`
|
||||
}
|
||||
var val2 struct {
|
||||
A string
|
||||
B string
|
||||
}
|
||||
assert.NoError(t, LoadFromYamlBytes(text, &val1))
|
||||
assert.Equal(t, "foo", val1.A)
|
||||
assert.Equal(t, "bar", val1.B)
|
||||
assert.NoError(t, LoadFromYamlBytes(text, &val2))
|
||||
assert.Equal(t, "foo", val2.A)
|
||||
assert.Equal(t, "bar", val2.B)
|
||||
}
|
||||
|
||||
func TestConfigTomlEnv(t *testing.T) {
|
||||
text := `a = "foo"
|
||||
b = 1
|
||||
c = "${FOO}"
|
||||
d = "abcd!@#112"
|
||||
`
|
||||
os.Setenv("FOO", "2")
|
||||
defer os.Unsetenv("FOO")
|
||||
tmpfile, err := createTempFile(".toml", text)
|
||||
assert.Nil(t, err)
|
||||
defer os.Remove(tmpfile)
|
||||
|
||||
var val struct {
|
||||
A string `json:"a"`
|
||||
B int `json:"b"`
|
||||
C string `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
|
||||
MustLoad(tmpfile, &val, UseEnv())
|
||||
assert.Equal(t, "foo", val.A)
|
||||
assert.Equal(t, 1, val.B)
|
||||
assert.Equal(t, "2", val.C)
|
||||
assert.Equal(t, "abcd!@#112", val.D)
|
||||
}
|
||||
|
||||
func TestConfigJsonEnv(t *testing.T) {
|
||||
tests := []string{
|
||||
".json",
|
||||
@@ -93,13 +242,221 @@ func TestConfigJsonEnv(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestToCamelCase(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
input: "",
|
||||
expect: "",
|
||||
},
|
||||
{
|
||||
input: "A",
|
||||
expect: "a",
|
||||
},
|
||||
{
|
||||
input: "a",
|
||||
expect: "a",
|
||||
},
|
||||
{
|
||||
input: "hello_world",
|
||||
expect: "hello_world",
|
||||
},
|
||||
{
|
||||
input: "Hello_world",
|
||||
expect: "hello_world",
|
||||
},
|
||||
{
|
||||
input: "hello_World",
|
||||
expect: "hello_world",
|
||||
},
|
||||
{
|
||||
input: "helloWorld",
|
||||
expect: "helloworld",
|
||||
},
|
||||
{
|
||||
input: "HelloWorld",
|
||||
expect: "helloworld",
|
||||
},
|
||||
{
|
||||
input: "hello World",
|
||||
expect: "hello world",
|
||||
},
|
||||
{
|
||||
input: "Hello World",
|
||||
expect: "hello world",
|
||||
},
|
||||
{
|
||||
input: "Hello World",
|
||||
expect: "hello world",
|
||||
},
|
||||
{
|
||||
input: "Hello World foo_bar",
|
||||
expect: "hello world foo_bar",
|
||||
},
|
||||
{
|
||||
input: "Hello World foo_Bar",
|
||||
expect: "hello world foo_bar",
|
||||
},
|
||||
{
|
||||
input: "Hello World Foo_bar",
|
||||
expect: "hello world foo_bar",
|
||||
},
|
||||
{
|
||||
input: "Hello World Foo_Bar",
|
||||
expect: "hello world foo_bar",
|
||||
},
|
||||
{
|
||||
input: "Hello.World Foo_Bar",
|
||||
expect: "hello.world foo_bar",
|
||||
},
|
||||
{
|
||||
input: "你好 World Foo_Bar",
|
||||
expect: "你好 world foo_bar",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.input, func(t *testing.T) {
|
||||
assert.Equal(t, test.expect, toLowerCase(test.input))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadFromJsonBytesError(t *testing.T) {
|
||||
var val struct{}
|
||||
assert.Error(t, LoadFromJsonBytes([]byte(`hello`), &val))
|
||||
}
|
||||
|
||||
func TestLoadFromTomlBytesError(t *testing.T) {
|
||||
var val struct{}
|
||||
assert.Error(t, LoadFromTomlBytes([]byte(`hello`), &val))
|
||||
}
|
||||
|
||||
func TestLoadFromYamlBytesError(t *testing.T) {
|
||||
var val struct{}
|
||||
assert.Error(t, LoadFromYamlBytes([]byte(`':hello`), &val))
|
||||
}
|
||||
|
||||
func TestLoadFromYamlBytes(t *testing.T) {
|
||||
input := []byte(`layer1:
|
||||
layer2:
|
||||
layer3: foo`)
|
||||
var val struct {
|
||||
Layer1 struct {
|
||||
Layer2 struct {
|
||||
Layer3 string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert.NoError(t, LoadFromYamlBytes(input, &val))
|
||||
assert.Equal(t, "foo", val.Layer1.Layer2.Layer3)
|
||||
}
|
||||
|
||||
func TestLoadFromYamlBytesTerm(t *testing.T) {
|
||||
input := []byte(`layer1:
|
||||
layer2:
|
||||
tls_conf: foo`)
|
||||
var val struct {
|
||||
Layer1 struct {
|
||||
Layer2 struct {
|
||||
Layer3 string `json:"tls_conf"`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert.NoError(t, LoadFromYamlBytes(input, &val))
|
||||
assert.Equal(t, "foo", val.Layer1.Layer2.Layer3)
|
||||
}
|
||||
|
||||
func TestLoadFromYamlBytesLayers(t *testing.T) {
|
||||
input := []byte(`layer1:
|
||||
layer2:
|
||||
layer3: foo`)
|
||||
var val struct {
|
||||
Value string `json:"Layer1.Layer2.Layer3"`
|
||||
}
|
||||
|
||||
assert.NoError(t, LoadFromYamlBytes(input, &val))
|
||||
assert.Equal(t, "foo", val.Value)
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonBytesMap(t *testing.T) {
|
||||
input := []byte(`{"foo":{"/mtproto.RPCTos": "bff.bff","bar":"baz"}}`)
|
||||
|
||||
var val struct {
|
||||
Foo map[string]string
|
||||
}
|
||||
|
||||
assert.NoError(t, LoadFromJsonBytes(input, &val))
|
||||
assert.Equal(t, "bff.bff", val.Foo["/mtproto.RPCTos"])
|
||||
assert.Equal(t, "baz", val.Foo["bar"])
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonBytesMapWithSliceElements(t *testing.T) {
|
||||
input := []byte(`{"foo":{"/mtproto.RPCTos": ["bff.bff", "any"],"bar":["baz", "qux"]}}`)
|
||||
|
||||
var val struct {
|
||||
Foo map[string][]string
|
||||
}
|
||||
|
||||
assert.NoError(t, LoadFromJsonBytes(input, &val))
|
||||
assert.EqualValues(t, []string{"bff.bff", "any"}, val.Foo["/mtproto.RPCTos"])
|
||||
assert.EqualValues(t, []string{"baz", "qux"}, val.Foo["bar"])
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonBytesMapWithSliceOfStructs(t *testing.T) {
|
||||
input := []byte(`{"foo":{
|
||||
"/mtproto.RPCTos": [{"bar": "any"}],
|
||||
"bar":[{"bar": "qux"}, {"bar": "ever"}]}}`)
|
||||
|
||||
var val struct {
|
||||
Foo map[string][]struct {
|
||||
Bar string
|
||||
}
|
||||
}
|
||||
|
||||
assert.NoError(t, LoadFromJsonBytes(input, &val))
|
||||
assert.Equal(t, 1, len(val.Foo["/mtproto.RPCTos"]))
|
||||
assert.Equal(t, "any", val.Foo["/mtproto.RPCTos"][0].Bar)
|
||||
assert.Equal(t, 2, len(val.Foo["bar"]))
|
||||
assert.Equal(t, "qux", val.Foo["bar"][0].Bar)
|
||||
assert.Equal(t, "ever", val.Foo["bar"][1].Bar)
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonBytesWithAnonymousField(t *testing.T) {
|
||||
type (
|
||||
Int int
|
||||
|
||||
InnerConf struct {
|
||||
Name string
|
||||
}
|
||||
|
||||
Conf struct {
|
||||
Int
|
||||
InnerConf
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
input = []byte(`{"Name": "hello", "int": 3}`)
|
||||
c Conf
|
||||
)
|
||||
assert.NoError(t, LoadFromJsonBytes(input, &c))
|
||||
assert.Equal(t, "hello", c.Name)
|
||||
assert.Equal(t, Int(3), c.Int)
|
||||
}
|
||||
|
||||
func createTempFile(ext, text string) (string, error) {
|
||||
tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext)
|
||||
tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text))+"*"+ext)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
||||
if err := os.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
|
||||
57
core/conf/readme.md
Normal file
57
core/conf/readme.md
Normal file
@@ -0,0 +1,57 @@
|
||||
## How to use
|
||||
|
||||
1. Define a config structure, like below:
|
||||
|
||||
```go
|
||||
type RestfulConf struct {
|
||||
Host string `json:",default=0.0.0.0"`
|
||||
Port int
|
||||
LogMode string `json:",options=[file,console]"`
|
||||
Verbose bool `json:",optional"`
|
||||
MaxConns int `json:",default=10000"`
|
||||
MaxBytes int64 `json:",default=1048576"`
|
||||
Timeout time.Duration `json:",default=3s"`
|
||||
CpuThreshold int64 `json:",default=900,range=[0:1000]"`
|
||||
}
|
||||
```
|
||||
|
||||
2. Write the yaml, toml or json config file:
|
||||
|
||||
- yaml example
|
||||
|
||||
```yaml
|
||||
# most fields are optional or have default values
|
||||
Port: 8080
|
||||
LogMode: console
|
||||
# you can use env settings
|
||||
MaxBytes: ${MAX_BYTES}
|
||||
```
|
||||
|
||||
- toml example
|
||||
|
||||
```toml
|
||||
# most fields are optional or have default values
|
||||
Port = 8_080
|
||||
LogMode = "console"
|
||||
# you can use env settings
|
||||
MaxBytes = "${MAX_BYTES}"
|
||||
```
|
||||
|
||||
3. Load the config from a file:
|
||||
|
||||
```go
|
||||
// exit on error
|
||||
var config RestfulConf
|
||||
conf.MustLoad(configFile, &config)
|
||||
|
||||
// or handle the error on your own
|
||||
var config RestfulConf
|
||||
if err := conf.Load(configFile, &config); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// enable reading from environments
|
||||
var config RestfulConf
|
||||
conf.MustLoad(configFile, &config, conf.UseEnv())
|
||||
```
|
||||
|
||||
@@ -3,7 +3,7 @@ package internal
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -37,7 +37,7 @@ func AddTLS(endpoints []string, certFile, certKeyFile, caFile string, insecureSk
|
||||
return err
|
||||
}
|
||||
|
||||
caData, err := ioutil.ReadFile(caFile)
|
||||
caData, err := os.ReadFile(caFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -191,9 +191,11 @@ func (c *cluster) handleWatchEvents(key string, events []*clientv3.Event) {
|
||||
})
|
||||
}
|
||||
case clientv3.EventTypeDelete:
|
||||
c.lock.Lock()
|
||||
if vals, ok := c.values[key]; ok {
|
||||
delete(vals, string(ev.Kv.Key))
|
||||
}
|
||||
c.lock.Unlock()
|
||||
for _, l := range listeners {
|
||||
l.OnDelete(KV{
|
||||
Key: string(ev.Kv.Key),
|
||||
@@ -206,7 +208,7 @@ func (c *cluster) handleWatchEvents(key string, events []*clientv3.Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cluster) load(cli EtcdClient, key string) {
|
||||
func (c *cluster) load(cli EtcdClient, key string) int64 {
|
||||
var resp *clientv3.GetResponse
|
||||
for {
|
||||
var err error
|
||||
@@ -230,6 +232,8 @@ func (c *cluster) load(cli EtcdClient, key string) {
|
||||
}
|
||||
|
||||
c.handleChanges(key, kvs)
|
||||
|
||||
return resp.Header.Revision
|
||||
}
|
||||
|
||||
func (c *cluster) monitor(key string, l UpdateListener) error {
|
||||
@@ -242,9 +246,9 @@ func (c *cluster) monitor(key string, l UpdateListener) error {
|
||||
return err
|
||||
}
|
||||
|
||||
c.load(cli, key)
|
||||
rev := c.load(cli, key)
|
||||
c.watchGroup.Run(func() {
|
||||
c.watch(cli, key)
|
||||
c.watch(cli, key, rev)
|
||||
})
|
||||
|
||||
return nil
|
||||
@@ -276,22 +280,29 @@ func (c *cluster) reload(cli EtcdClient) {
|
||||
for _, key := range keys {
|
||||
k := key
|
||||
c.watchGroup.Run(func() {
|
||||
c.load(cli, k)
|
||||
c.watch(cli, k)
|
||||
rev := c.load(cli, k)
|
||||
c.watch(cli, k, rev)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cluster) watch(cli EtcdClient, key string) {
|
||||
func (c *cluster) watch(cli EtcdClient, key string, rev int64) {
|
||||
for {
|
||||
if c.watchStream(cli, key) {
|
||||
if c.watchStream(cli, key, rev) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *cluster) watchStream(cli EtcdClient, key string) bool {
|
||||
rch := cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix())
|
||||
func (c *cluster) watchStream(cli EtcdClient, key string, rev int64) bool {
|
||||
var rch clientv3.WatchChan
|
||||
if rev != 0 {
|
||||
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix(),
|
||||
clientv3.WithRev(rev+1))
|
||||
} else {
|
||||
rch = cli.Watch(clientv3.WithRequireLeader(c.context(cli)), makeKeyPrefix(key), clientv3.WithPrefix())
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case wresp, ok := <-rch:
|
||||
@@ -332,6 +343,7 @@ func DialClient(endpoints []string) (EtcdClient, error) {
|
||||
DialKeepAliveTime: dialKeepAliveTime,
|
||||
DialKeepAliveTimeout: DialTimeout,
|
||||
RejectOldCluster: true,
|
||||
PermitWithoutStream: true,
|
||||
}
|
||||
if account, ok := GetAccount(endpoints); ok {
|
||||
cfg.Username = account.User
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
"go.etcd.io/etcd/api/v3/etcdserverpb"
|
||||
"go.etcd.io/etcd/api/v3/mvccpb"
|
||||
clientv3 "go.etcd.io/etcd/client/v3"
|
||||
)
|
||||
@@ -112,6 +113,7 @@ func TestCluster_Load(t *testing.T) {
|
||||
restore := setMockClient(cli)
|
||||
defer restore()
|
||||
cli.EXPECT().Get(gomock.Any(), "any/", gomock.Any()).Return(&clientv3.GetResponse{
|
||||
Header: &etcdserverpb.ResponseHeader{},
|
||||
Kvs: []*mvccpb.KeyValue{
|
||||
{
|
||||
Key: []byte("hello"),
|
||||
@@ -168,7 +170,7 @@ func TestCluster_Watch(t *testing.T) {
|
||||
listener.EXPECT().OnDelete(gomock.Any()).Do(func(_ interface{}) {
|
||||
wg.Done()
|
||||
}).MaxTimes(1)
|
||||
go c.watch(cli, "any")
|
||||
go c.watch(cli, "any", 0)
|
||||
ch <- clientv3.WatchResponse{
|
||||
Events: []*clientv3.Event{
|
||||
{
|
||||
@@ -212,7 +214,7 @@ func TestClusterWatch_RespFailures(t *testing.T) {
|
||||
ch <- resp
|
||||
close(c.done)
|
||||
}()
|
||||
c.watch(cli, "any")
|
||||
c.watch(cli, "any", 0)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -232,7 +234,7 @@ func TestClusterWatch_CloseChan(t *testing.T) {
|
||||
close(ch)
|
||||
close(c.done)
|
||||
}()
|
||||
c.watch(cli, "any")
|
||||
c.watch(cli, "any", 0)
|
||||
}
|
||||
|
||||
func TestValueOnlyContext(t *testing.T) {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package discov
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/discov/internal"
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -51,12 +53,7 @@ func NewPublisher(endpoints []string, key, value string, opts ...PubOption) *Pub
|
||||
|
||||
// KeepAlive keeps key:value alive.
|
||||
func (p *Publisher) KeepAlive() error {
|
||||
cli, err := internal.GetRegistry().GetConn(p.endpoints)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.lease, err = p.register(cli)
|
||||
cli, err := p.doRegister()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -83,6 +80,43 @@ func (p *Publisher) Stop() {
|
||||
p.quit.Close()
|
||||
}
|
||||
|
||||
func (p *Publisher) doKeepAlive() error {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
select {
|
||||
case <-p.quit.Done():
|
||||
return nil
|
||||
default:
|
||||
cli, err := p.doRegister()
|
||||
if err != nil {
|
||||
logx.Errorf("etcd publisher doRegister: %s", err.Error())
|
||||
break
|
||||
}
|
||||
|
||||
if err := p.keepAliveAsync(cli); err != nil {
|
||||
logx.Errorf("etcd publisher keepAliveAsync: %s", err.Error())
|
||||
break
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Publisher) doRegister() (internal.EtcdClient, error) {
|
||||
cli, err := internal.GetRegistry().GetConn(p.endpoints)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p.lease, err = p.register(cli)
|
||||
return cli, err
|
||||
}
|
||||
|
||||
func (p *Publisher) keepAliveAsync(cli internal.EtcdClient) error {
|
||||
ch, err := cli.KeepAlive(cli.Ctx(), p.lease)
|
||||
if err != nil {
|
||||
@@ -95,8 +129,8 @@ func (p *Publisher) keepAliveAsync(cli internal.EtcdClient) error {
|
||||
case _, ok := <-ch:
|
||||
if !ok {
|
||||
p.revoke(cli)
|
||||
if err := p.KeepAlive(); err != nil {
|
||||
logx.Errorf("KeepAlive: %s", err.Error())
|
||||
if err := p.doKeepAlive(); err != nil {
|
||||
logx.Errorf("etcd publisher KeepAlive: %s", err.Error())
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -105,8 +139,8 @@ func (p *Publisher) keepAliveAsync(cli internal.EtcdClient) error {
|
||||
p.revoke(cli)
|
||||
select {
|
||||
case <-p.resumeChan:
|
||||
if err := p.KeepAlive(); err != nil {
|
||||
logx.Errorf("KeepAlive: %s", err.Error())
|
||||
if err := p.doKeepAlive(); err != nil {
|
||||
logx.Errorf("etcd publisher KeepAlive: %s", err.Error())
|
||||
}
|
||||
return
|
||||
case <-p.quit.Done():
|
||||
@@ -141,7 +175,7 @@ func (p *Publisher) register(client internal.EtcdClient) (clientv3.LeaseID, erro
|
||||
|
||||
func (p *Publisher) revoke(cli internal.EtcdClient) {
|
||||
if _, err := cli.Revoke(cli.Ctx(), p.lease); err != nil {
|
||||
logx.Error(err)
|
||||
logx.Errorf("etcd publisher revoke: %s", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ type (
|
||||
// SubOption defines the method to customize a Subscriber.
|
||||
SubOption func(sub *Subscriber)
|
||||
|
||||
// A Subscriber is used to subscribe the given key on a etcd cluster.
|
||||
// A Subscriber is used to subscribe the given key on an etcd cluster.
|
||||
Subscriber struct {
|
||||
endpoints []string
|
||||
exclusive bool
|
||||
|
||||
21
core/errorx/wrap.go
Normal file
21
core/errorx/wrap.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package errorx
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Wrap returns an error that wraps err with given message.
|
||||
func Wrap(err error, message string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("%s: %w", message, err)
|
||||
}
|
||||
|
||||
// Wrapf returns an error that wraps err with given format and args.
|
||||
func Wrapf(err error, format string, args ...interface{}) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), err)
|
||||
}
|
||||
24
core/errorx/wrap_test.go
Normal file
24
core/errorx/wrap_test.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package errorx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWrap(t *testing.T) {
|
||||
assert.Nil(t, Wrap(nil, "test"))
|
||||
assert.Equal(t, "foo: bar", Wrap(errors.New("bar"), "foo").Error())
|
||||
|
||||
err := errors.New("foo")
|
||||
assert.True(t, errors.Is(Wrap(err, "bar"), err))
|
||||
}
|
||||
|
||||
func TestWrapf(t *testing.T) {
|
||||
assert.Nil(t, Wrapf(nil, "%s", "test"))
|
||||
assert.Equal(t, "foo bar: quz", Wrapf(errors.New("quz"), "foo %s", "bar").Error())
|
||||
|
||||
err := errors.New("foo")
|
||||
assert.True(t, errors.Is(Wrapf(err, "foo %s", "bar"), err))
|
||||
}
|
||||
15
core/fs/files_test.go
Normal file
15
core/fs/files_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCloseOnExec(t *testing.T) {
|
||||
file := os.NewFile(0, os.DevNull)
|
||||
assert.NotPanics(t, func() {
|
||||
CloseOnExec(file)
|
||||
})
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/hash"
|
||||
@@ -12,12 +11,12 @@ import (
|
||||
// The file is kept as open, the caller should close the file handle,
|
||||
// and remove the file by name.
|
||||
func TempFileWithText(text string) (*os.File, error) {
|
||||
tmpfile, err := ioutil.TempFile(os.TempDir(), hash.Md5Hex([]byte(text)))
|
||||
tmpfile, err := os.CreateTemp(os.TempDir(), hash.Md5Hex([]byte(text)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
||||
if err := os.WriteFile(tmpfile.Name(), []byte(text), os.ModeTemporary); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
||||
49
core/fs/temps_test.go
Normal file
49
core/fs/temps_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package fs
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestTempFileWithText(t *testing.T) {
|
||||
f, err := TempFileWithText("test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if f == nil {
|
||||
t.Error("TempFileWithText returned nil")
|
||||
}
|
||||
if f.Name() == "" {
|
||||
t.Error("TempFileWithText returned empty file name")
|
||||
}
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
bs, err := io.ReadAll(f)
|
||||
assert.Nil(t, err)
|
||||
if len(bs) != 4 {
|
||||
t.Error("TempFileWithText returned wrong file size")
|
||||
}
|
||||
if f.Close() != nil {
|
||||
t.Error("TempFileWithText returned error on close")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTempFilenameWithText(t *testing.T) {
|
||||
f, err := TempFilenameWithText("test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if f == "" {
|
||||
t.Error("TempFilenameWithText returned empty file name")
|
||||
}
|
||||
defer os.Remove(f)
|
||||
|
||||
bs, err := os.ReadFile(f)
|
||||
assert.Nil(t, err)
|
||||
if len(bs) != 4 {
|
||||
t.Error("TempFilenameWithText returned wrong file size")
|
||||
}
|
||||
}
|
||||
@@ -328,7 +328,7 @@ func (s Stream) Parallel(fn ParallelFunc, opts ...Option) {
|
||||
}, opts...).Done()
|
||||
}
|
||||
|
||||
// Reduce is a utility method to let the caller deal with the underlying channel.
|
||||
// Reduce is an utility method to let the caller deal with the underlying channel.
|
||||
func (s Stream) Reduce(fn ReduceFunc) (interface{}, error) {
|
||||
return fn(s.source)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package fx
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
@@ -238,7 +238,7 @@ func TestLast(t *testing.T) {
|
||||
|
||||
func TestMap(t *testing.T) {
|
||||
runCheckedTest(t, func(t *testing.T) {
|
||||
log.SetOutput(ioutil.Discard)
|
||||
log.SetOutput(io.Discard)
|
||||
|
||||
tests := []struct {
|
||||
mapper MapFunc
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/mapping"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -183,5 +182,5 @@ func innerRepr(node interface{}) string {
|
||||
}
|
||||
|
||||
func repr(node interface{}) string {
|
||||
return mapping.Repr(node)
|
||||
return lang.Repr(node)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ func (nopCloser) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// NopCloser returns a io.WriteCloser that does nothing on calling Close.
|
||||
// NopCloser returns an io.WriteCloser that does nothing on calling Close.
|
||||
func NopCloser(w io.Writer) io.WriteCloser {
|
||||
return nopCloser{w}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
@@ -26,7 +25,7 @@ type (
|
||||
func DupReadCloser(reader io.ReadCloser) (io.ReadCloser, io.ReadCloser) {
|
||||
var buf bytes.Buffer
|
||||
tee := io.TeeReader(reader, &buf)
|
||||
return ioutil.NopCloser(tee), ioutil.NopCloser(&buf)
|
||||
return io.NopCloser(tee), io.NopCloser(&buf)
|
||||
}
|
||||
|
||||
// KeepSpace customizes the reading functions to keep leading and tailing spaces.
|
||||
@@ -54,7 +53,7 @@ func ReadBytes(reader io.Reader, buf []byte) error {
|
||||
|
||||
// ReadText reads content from the given file with leading and tailing spaces trimmed.
|
||||
func ReadText(filename string) (string, error) {
|
||||
content, err := ioutil.ReadFile(filename)
|
||||
content, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package iox
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -97,10 +96,10 @@ func TestReadTextLines(t *testing.T) {
|
||||
|
||||
func TestDupReadCloser(t *testing.T) {
|
||||
input := "hello"
|
||||
reader := ioutil.NopCloser(bytes.NewBufferString(input))
|
||||
reader := io.NopCloser(bytes.NewBufferString(input))
|
||||
r1, r2 := DupReadCloser(reader)
|
||||
verify := func(r io.Reader) {
|
||||
output, err := ioutil.ReadAll(r)
|
||||
output, err := io.ReadAll(r)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, input, string(output))
|
||||
}
|
||||
@@ -110,7 +109,7 @@ func TestDupReadCloser(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadBytes(t *testing.T) {
|
||||
reader := ioutil.NopCloser(bytes.NewBufferString("helloworld"))
|
||||
reader := io.NopCloser(bytes.NewBufferString("helloworld"))
|
||||
buf := make([]byte, 5)
|
||||
err := ReadBytes(reader, buf)
|
||||
assert.Nil(t, err)
|
||||
@@ -118,7 +117,7 @@ func TestReadBytes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestReadBytesNotEnough(t *testing.T) {
|
||||
reader := ioutil.NopCloser(bytes.NewBufferString("hell"))
|
||||
reader := io.NopCloser(bytes.NewBufferString("hell"))
|
||||
buf := make([]byte, 5)
|
||||
err := ReadBytes(reader, buf)
|
||||
assert.Equal(t, io.EOF, err)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package iox
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
@@ -13,7 +12,7 @@ func TestCountLines(t *testing.T) {
|
||||
2
|
||||
3
|
||||
4`
|
||||
file, err := ioutil.TempFile(os.TempDir(), "test-")
|
||||
file, err := os.CreateTemp(os.TempDir(), "test-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package jsontype
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// MilliTime represents time.Time that works better with mongodb.
|
||||
type MilliTime struct {
|
||||
time.Time
|
||||
}
|
||||
|
||||
// MarshalJSON marshals mt to json bytes.
|
||||
func (mt MilliTime) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(mt.Milli())
|
||||
}
|
||||
|
||||
// UnmarshalJSON unmarshals data into mt.
|
||||
func (mt *MilliTime) UnmarshalJSON(data []byte) error {
|
||||
var milli int64
|
||||
if err := json.Unmarshal(data, &milli); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mt.Time = time.Unix(0, milli*int64(time.Millisecond))
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetBSON returns BSON base on mt.
|
||||
func (mt MilliTime) GetBSON() (interface{}, error) {
|
||||
return mt.Time, nil
|
||||
}
|
||||
|
||||
// SetBSON sets raw into mt.
|
||||
func (mt *MilliTime) SetBSON(raw bson.Raw) error {
|
||||
return raw.Unmarshal(&mt.Time)
|
||||
}
|
||||
|
||||
// Milli returns milliseconds for mt.
|
||||
func (mt MilliTime) Milli() int64 {
|
||||
return mt.UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
@@ -1,126 +0,0 @@
|
||||
package jsontype
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/globalsign/mgo/bson"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMilliTime_GetBSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tm time.Time
|
||||
}{
|
||||
{
|
||||
name: "now",
|
||||
tm: time.Now(),
|
||||
},
|
||||
{
|
||||
name: "future",
|
||||
tm: time.Now().Add(time.Hour),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := MilliTime{test.tm}.GetBSON()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, test.tm, got)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMilliTime_MarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tm time.Time
|
||||
}{
|
||||
{
|
||||
name: "now",
|
||||
tm: time.Now(),
|
||||
},
|
||||
{
|
||||
name: "future",
|
||||
tm: time.Now().Add(time.Hour),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
b, err := MilliTime{test.tm}.MarshalJSON()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, strconv.FormatInt(test.tm.UnixNano()/1e6, 10), string(b))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMilliTime_Milli(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tm time.Time
|
||||
}{
|
||||
{
|
||||
name: "now",
|
||||
tm: time.Now(),
|
||||
},
|
||||
{
|
||||
name: "future",
|
||||
tm: time.Now().Add(time.Hour),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
n := MilliTime{test.tm}.Milli()
|
||||
assert.Equal(t, test.tm.UnixNano()/1e6, n)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMilliTime_UnmarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
tm time.Time
|
||||
}{
|
||||
{
|
||||
name: "now",
|
||||
tm: time.Now(),
|
||||
},
|
||||
{
|
||||
name: "future",
|
||||
tm: time.Now().Add(time.Hour),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var mt MilliTime
|
||||
s := strconv.FormatInt(test.tm.UnixNano()/1e6, 10)
|
||||
err := mt.UnmarshalJSON([]byte(s))
|
||||
assert.Nil(t, err)
|
||||
s1, err := mt.MarshalJSON()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, s, string(s1))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnmarshalWithError(t *testing.T) {
|
||||
var mt MilliTime
|
||||
assert.NotNil(t, mt.UnmarshalJSON([]byte("hello")))
|
||||
}
|
||||
|
||||
func TestSetBSON(t *testing.T) {
|
||||
data, err := bson.Marshal(time.Now())
|
||||
assert.Nil(t, err)
|
||||
|
||||
var raw bson.Raw
|
||||
assert.Nil(t, bson.Unmarshal(data, &raw))
|
||||
|
||||
var mt MilliTime
|
||||
assert.Nil(t, mt.SetBSON(raw))
|
||||
assert.NotNil(t, mt.SetBSON(bson.Raw{}))
|
||||
}
|
||||
@@ -13,6 +13,16 @@ func Marshal(v interface{}) ([]byte, error) {
|
||||
return json.Marshal(v)
|
||||
}
|
||||
|
||||
// MarshalToString marshals v into a string.
|
||||
func MarshalToString(v interface{}) (string, error) {
|
||||
data, err := Marshal(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// Unmarshal unmarshals data bytes into v.
|
||||
func Unmarshal(data []byte, v interface{}) error {
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
|
||||
103
core/jsonx/json_test.go
Normal file
103
core/jsonx/json_test.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package jsonx
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
var v = struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}{
|
||||
Name: "John",
|
||||
Age: 30,
|
||||
}
|
||||
bs, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, `{"name":"John","age":30}`, string(bs))
|
||||
}
|
||||
|
||||
func TestMarshalToString(t *testing.T) {
|
||||
var v = struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}{
|
||||
Name: "John",
|
||||
Age: 30,
|
||||
}
|
||||
toString, err := MarshalToString(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, `{"name":"John","age":30}`, toString)
|
||||
|
||||
_, err = MarshalToString(make(chan int))
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestUnmarshal(t *testing.T) {
|
||||
const s = `{"name":"John","age":30}`
|
||||
var v struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
err := Unmarshal([]byte(s), &v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "John", v.Name)
|
||||
assert.Equal(t, 30, v.Age)
|
||||
}
|
||||
|
||||
func TestUnmarshalError(t *testing.T) {
|
||||
const s = `{"name":"John","age":30`
|
||||
var v struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
err := Unmarshal([]byte(s), &v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestUnmarshalFromString(t *testing.T) {
|
||||
const s = `{"name":"John","age":30}`
|
||||
var v struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
err := UnmarshalFromString(s, &v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "John", v.Name)
|
||||
assert.Equal(t, 30, v.Age)
|
||||
}
|
||||
|
||||
func TestUnmarshalFromStringError(t *testing.T) {
|
||||
const s = `{"name":"John","age":30`
|
||||
var v struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
err := UnmarshalFromString(s, &v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestUnmarshalFromRead(t *testing.T) {
|
||||
const s = `{"name":"John","age":30}`
|
||||
var v struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
err := UnmarshalFromReader(strings.NewReader(s), &v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "John", v.Name)
|
||||
assert.Equal(t, 30, v.Age)
|
||||
}
|
||||
|
||||
func TestUnmarshalFromReaderError(t *testing.T) {
|
||||
const s = `{"name":"John","age":30`
|
||||
var v struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
err := UnmarshalFromReader(strings.NewReader(s), &v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
package lang
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Placeholder is a placeholder object that can be used globally.
|
||||
var Placeholder PlaceholderType
|
||||
|
||||
@@ -9,3 +15,64 @@ type (
|
||||
// PlaceholderType represents a placeholder type.
|
||||
PlaceholderType = struct{}
|
||||
)
|
||||
|
||||
// Repr returns the string representation of v.
|
||||
func Repr(v interface{}) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// if func (v *Type) String() string, we can't use Elem()
|
||||
switch vt := v.(type) {
|
||||
case fmt.Stringer:
|
||||
return vt.String()
|
||||
}
|
||||
|
||||
val := reflect.ValueOf(v)
|
||||
for val.Kind() == reflect.Ptr && !val.IsNil() {
|
||||
val = val.Elem()
|
||||
}
|
||||
|
||||
return reprOfValue(val)
|
||||
}
|
||||
|
||||
func reprOfValue(val reflect.Value) string {
|
||||
switch vt := val.Interface().(type) {
|
||||
case bool:
|
||||
return strconv.FormatBool(vt)
|
||||
case error:
|
||||
return vt.Error()
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(vt), 'f', -1, 32)
|
||||
case float64:
|
||||
return strconv.FormatFloat(vt, 'f', -1, 64)
|
||||
case fmt.Stringer:
|
||||
return vt.String()
|
||||
case int:
|
||||
return strconv.Itoa(vt)
|
||||
case int8:
|
||||
return strconv.Itoa(int(vt))
|
||||
case int16:
|
||||
return strconv.Itoa(int(vt))
|
||||
case int32:
|
||||
return strconv.Itoa(int(vt))
|
||||
case int64:
|
||||
return strconv.FormatInt(vt, 10)
|
||||
case string:
|
||||
return vt
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint64:
|
||||
return strconv.FormatUint(vt, 10)
|
||||
case []byte:
|
||||
return string(vt)
|
||||
default:
|
||||
return fmt.Sprint(val.Interface())
|
||||
}
|
||||
}
|
||||
|
||||
156
core/lang/lang_test.go
Normal file
156
core/lang/lang_test.go
Normal file
@@ -0,0 +1,156 @@
|
||||
package lang
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRepr(t *testing.T) {
|
||||
var (
|
||||
f32 float32 = 1.1
|
||||
f64 = 2.2
|
||||
i8 int8 = 1
|
||||
i16 int16 = 2
|
||||
i32 int32 = 3
|
||||
i64 int64 = 4
|
||||
u8 uint8 = 5
|
||||
u16 uint16 = 6
|
||||
u32 uint32 = 7
|
||||
u64 uint64 = 8
|
||||
)
|
||||
tests := []struct {
|
||||
v interface{}
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
nil,
|
||||
"",
|
||||
},
|
||||
{
|
||||
mockStringable{},
|
||||
"mocked",
|
||||
},
|
||||
{
|
||||
new(mockStringable),
|
||||
"mocked",
|
||||
},
|
||||
{
|
||||
newMockPtr(),
|
||||
"mockptr",
|
||||
},
|
||||
{
|
||||
&mockOpacity{
|
||||
val: 1,
|
||||
},
|
||||
"{1}",
|
||||
},
|
||||
{
|
||||
true,
|
||||
"true",
|
||||
},
|
||||
{
|
||||
false,
|
||||
"false",
|
||||
},
|
||||
{
|
||||
f32,
|
||||
"1.1",
|
||||
},
|
||||
{
|
||||
f64,
|
||||
"2.2",
|
||||
},
|
||||
{
|
||||
i8,
|
||||
"1",
|
||||
},
|
||||
{
|
||||
i16,
|
||||
"2",
|
||||
},
|
||||
{
|
||||
i32,
|
||||
"3",
|
||||
},
|
||||
{
|
||||
i64,
|
||||
"4",
|
||||
},
|
||||
{
|
||||
u8,
|
||||
"5",
|
||||
},
|
||||
{
|
||||
u16,
|
||||
"6",
|
||||
},
|
||||
{
|
||||
u32,
|
||||
"7",
|
||||
},
|
||||
{
|
||||
u64,
|
||||
"8",
|
||||
},
|
||||
{
|
||||
[]byte(`abcd`),
|
||||
"abcd",
|
||||
},
|
||||
{
|
||||
mockOpacity{val: 1},
|
||||
"{1}",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.expect, func(t *testing.T) {
|
||||
assert.Equal(t, test.expect, Repr(test.v))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestReprOfValue(t *testing.T) {
|
||||
t.Run("error", func(t *testing.T) {
|
||||
assert.Equal(t, "error", reprOfValue(reflect.ValueOf(errors.New("error"))))
|
||||
})
|
||||
|
||||
t.Run("stringer", func(t *testing.T) {
|
||||
assert.Equal(t, "1.23", reprOfValue(reflect.ValueOf(json.Number("1.23"))))
|
||||
})
|
||||
|
||||
t.Run("int", func(t *testing.T) {
|
||||
assert.Equal(t, "1", reprOfValue(reflect.ValueOf(1)))
|
||||
})
|
||||
|
||||
t.Run("int", func(t *testing.T) {
|
||||
assert.Equal(t, "1", reprOfValue(reflect.ValueOf("1")))
|
||||
})
|
||||
|
||||
t.Run("int", func(t *testing.T) {
|
||||
assert.Equal(t, "1", reprOfValue(reflect.ValueOf(uint(1))))
|
||||
})
|
||||
}
|
||||
|
||||
type mockStringable struct{}
|
||||
|
||||
func (m mockStringable) String() string {
|
||||
return "mocked"
|
||||
}
|
||||
|
||||
type mockPtr struct{}
|
||||
|
||||
func newMockPtr() *mockPtr {
|
||||
return new(mockPtr)
|
||||
}
|
||||
|
||||
func (m *mockPtr) String() string {
|
||||
return "mockptr"
|
||||
}
|
||||
|
||||
type mockOpacity struct {
|
||||
val int
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -74,7 +75,12 @@ func NewPeriodLimit(period, quota int, limitStore *redis.Redis, keyPrefix string
|
||||
|
||||
// Take requests a permit, it returns the permit state.
|
||||
func (h *PeriodLimit) Take(key string) (int, error) {
|
||||
resp, err := h.limitStore.Eval(periodScript, []string{h.keyPrefix + key}, []string{
|
||||
return h.TakeCtx(context.Background(), key)
|
||||
}
|
||||
|
||||
// TakeCtx requests a permit with context, it returns the permit state.
|
||||
func (h *PeriodLimit) TakeCtx(ctx context.Context, key string) (int, error) {
|
||||
resp, err := h.limitStore.EvalCtx(ctx, periodScript, []string{h.keyPrefix + key}, []string{
|
||||
strconv.Itoa(h.quota),
|
||||
strconv.Itoa(h.calcExpireSeconds()),
|
||||
})
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"sync"
|
||||
@@ -58,8 +60,8 @@ type TokenLimiter struct {
|
||||
timestampKey string
|
||||
rescueLock sync.Mutex
|
||||
redisAlive uint32
|
||||
rescueLimiter *xrate.Limiter
|
||||
monitorStarted bool
|
||||
rescueLimiter *xrate.Limiter
|
||||
}
|
||||
|
||||
// NewTokenLimiter returns a new TokenLimiter that allows events up to rate and permits
|
||||
@@ -84,19 +86,31 @@ func (lim *TokenLimiter) Allow() bool {
|
||||
return lim.AllowN(time.Now(), 1)
|
||||
}
|
||||
|
||||
// AllowCtx is shorthand for AllowNCtx(ctx,time.Now(), 1) with incoming context.
|
||||
func (lim *TokenLimiter) AllowCtx(ctx context.Context) bool {
|
||||
return lim.AllowNCtx(ctx, time.Now(), 1)
|
||||
}
|
||||
|
||||
// AllowN reports whether n events may happen at time now.
|
||||
// Use this method if you intend to drop / skip events that exceed the rate.
|
||||
// Otherwise, use Reserve or Wait.
|
||||
func (lim *TokenLimiter) AllowN(now time.Time, n int) bool {
|
||||
return lim.reserveN(now, n)
|
||||
return lim.reserveN(context.Background(), now, n)
|
||||
}
|
||||
|
||||
func (lim *TokenLimiter) reserveN(now time.Time, n int) bool {
|
||||
// AllowNCtx reports whether n events may happen at time now with incoming context.
|
||||
// Use this method if you intend to drop / skip events that exceed the rate.
|
||||
// Otherwise, use Reserve or Wait.
|
||||
func (lim *TokenLimiter) AllowNCtx(ctx context.Context, now time.Time, n int) bool {
|
||||
return lim.reserveN(ctx, now, n)
|
||||
}
|
||||
|
||||
func (lim *TokenLimiter) reserveN(ctx context.Context, now time.Time, n int) bool {
|
||||
if atomic.LoadUint32(&lim.redisAlive) == 0 {
|
||||
return lim.rescueLimiter.AllowN(now, n)
|
||||
}
|
||||
|
||||
resp, err := lim.store.Eval(
|
||||
resp, err := lim.store.EvalCtx(ctx,
|
||||
script,
|
||||
[]string{
|
||||
lim.tokenKey,
|
||||
@@ -113,6 +127,10 @@ func (lim *TokenLimiter) reserveN(now time.Time, n int) bool {
|
||||
if err == redis.Nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) {
|
||||
logx.Errorf("fail to use rate limiter: %s", err)
|
||||
return false
|
||||
}
|
||||
if err != nil {
|
||||
logx.Errorf("fail to use rate limiter: %s, use in-process limiter for rescue", err)
|
||||
lim.startMonitor()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package limit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -15,6 +16,30 @@ func init() {
|
||||
logx.Disable()
|
||||
}
|
||||
|
||||
func TestTokenLimit_WithCtx(t *testing.T) {
|
||||
s, err := miniredis.Run()
|
||||
assert.Nil(t, err)
|
||||
|
||||
const (
|
||||
total = 100
|
||||
rate = 5
|
||||
burst = 10
|
||||
)
|
||||
l := NewTokenLimiter(rate, burst, redis.New(s.Addr()), "tokenlimit")
|
||||
defer s.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
ok := l.AllowCtx(ctx)
|
||||
assert.True(t, ok)
|
||||
|
||||
cancel()
|
||||
for i := 0; i < total; i++ {
|
||||
ok := l.AllowCtx(ctx)
|
||||
assert.False(t, ok)
|
||||
assert.False(t, l.monitorStarted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenLimit_Rescue(t *testing.T) {
|
||||
s, err := miniredis.Run()
|
||||
assert.Nil(t, err)
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
const (
|
||||
defaultBuckets = 50
|
||||
defaultWindow = time.Second * 5
|
||||
// using 1000m notation, 900m is like 80%, keep it as var for unit test
|
||||
// using 1000m notation, 900m is like 90%, keep it as var for unit test
|
||||
defaultCpuThreshold = 900
|
||||
defaultMinRt = float64(time.Second / time.Millisecond)
|
||||
// moving average hyperparameter beta for calculating requests on the fly
|
||||
@@ -70,7 +70,7 @@ type (
|
||||
flying int64
|
||||
avgFlying float64
|
||||
avgFlyingLock syncx.SpinLock
|
||||
dropTime *syncx.AtomicDuration
|
||||
overloadTime *syncx.AtomicDuration
|
||||
droppedRecently *syncx.AtomicBool
|
||||
passCounter *collection.RollingWindow
|
||||
rtCounter *collection.RollingWindow
|
||||
@@ -106,7 +106,7 @@ func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
|
||||
return &adaptiveShedder{
|
||||
cpuThreshold: options.cpuThreshold,
|
||||
windows: int64(time.Second / bucketDuration),
|
||||
dropTime: syncx.NewAtomicDuration(),
|
||||
overloadTime: syncx.NewAtomicDuration(),
|
||||
droppedRecently: syncx.NewAtomicBool(),
|
||||
passCounter: collection.NewRollingWindow(options.buckets, bucketDuration,
|
||||
collection.IgnoreCurrentBucket()),
|
||||
@@ -118,7 +118,6 @@ func NewAdaptiveShedder(opts ...ShedderOption) Shedder {
|
||||
// Allow implements Shedder.Allow.
|
||||
func (as *adaptiveShedder) Allow() (Promise, error) {
|
||||
if as.shouldDrop() {
|
||||
as.dropTime.Set(timex.Now())
|
||||
as.droppedRecently.Set(true)
|
||||
|
||||
return nil, ErrServiceOverloaded
|
||||
@@ -215,21 +214,26 @@ func (as *adaptiveShedder) stillHot() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
dropTime := as.dropTime.Load()
|
||||
if dropTime == 0 {
|
||||
overloadTime := as.overloadTime.Load()
|
||||
if overloadTime == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
hot := timex.Since(dropTime) < coolOffDuration
|
||||
if !hot {
|
||||
as.droppedRecently.Set(false)
|
||||
if timex.Since(overloadTime) < coolOffDuration {
|
||||
return true
|
||||
}
|
||||
|
||||
return hot
|
||||
as.droppedRecently.Set(false)
|
||||
return false
|
||||
}
|
||||
|
||||
func (as *adaptiveShedder) systemOverloaded() bool {
|
||||
return systemOverloadChecker(as.cpuThreshold)
|
||||
if !systemOverloadChecker(as.cpuThreshold) {
|
||||
return false
|
||||
}
|
||||
|
||||
as.overloadTime.Set(timex.Now())
|
||||
return true
|
||||
}
|
||||
|
||||
// WithBuckets customizes the Shedder with given number of buckets.
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/zeromicro/go-zero/core/mathx"
|
||||
"github.com/zeromicro/go-zero/core/stat"
|
||||
"github.com/zeromicro/go-zero/core/syncx"
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -136,7 +137,7 @@ func TestAdaptiveShedderShouldDrop(t *testing.T) {
|
||||
passCounter: passCounter,
|
||||
rtCounter: rtCounter,
|
||||
windows: buckets,
|
||||
dropTime: syncx.NewAtomicDuration(),
|
||||
overloadTime: syncx.NewAtomicDuration(),
|
||||
droppedRecently: syncx.NewAtomicBool(),
|
||||
}
|
||||
// cpu >= 800, inflight < maxPass
|
||||
@@ -190,12 +191,15 @@ func TestAdaptiveShedderStillHot(t *testing.T) {
|
||||
passCounter: passCounter,
|
||||
rtCounter: rtCounter,
|
||||
windows: buckets,
|
||||
dropTime: syncx.NewAtomicDuration(),
|
||||
overloadTime: syncx.NewAtomicDuration(),
|
||||
droppedRecently: syncx.ForAtomicBool(true),
|
||||
}
|
||||
assert.False(t, shedder.stillHot())
|
||||
shedder.dropTime.Set(-coolOffDuration * 2)
|
||||
shedder.overloadTime.Set(-coolOffDuration * 2)
|
||||
assert.False(t, shedder.stillHot())
|
||||
shedder.droppedRecently.Set(true)
|
||||
shedder.overloadTime.Set(timex.Now())
|
||||
assert.True(t, shedder.stillHot())
|
||||
}
|
||||
|
||||
func BenchmarkAdaptiveShedder_Allow(b *testing.B) {
|
||||
|
||||
122
core/logc/logs.go
Normal file
122
core/logc/logs.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package logc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type (
|
||||
LogConf = logx.LogConf
|
||||
LogField = logx.LogField
|
||||
)
|
||||
|
||||
// AddGlobalFields adds global fields.
|
||||
func AddGlobalFields(fields ...LogField) {
|
||||
logx.AddGlobalFields(fields...)
|
||||
}
|
||||
|
||||
// Alert alerts v in alert level, and the message is written to error log.
|
||||
func Alert(_ context.Context, v string) {
|
||||
logx.Alert(v)
|
||||
}
|
||||
|
||||
// Close closes the logging.
|
||||
func Close() error {
|
||||
return logx.Close()
|
||||
}
|
||||
|
||||
// Error writes v into error log.
|
||||
func Error(ctx context.Context, v ...interface{}) {
|
||||
getLogger(ctx).Error(v...)
|
||||
}
|
||||
|
||||
// Errorf writes v with format into error log.
|
||||
func Errorf(ctx context.Context, format string, v ...interface{}) {
|
||||
getLogger(ctx).Errorf(fmt.Errorf(format, v...).Error())
|
||||
}
|
||||
|
||||
// Errorv writes v into error log with json content.
|
||||
// No call stack attached, because not elegant to pack the messages.
|
||||
func Errorv(ctx context.Context, v interface{}) {
|
||||
getLogger(ctx).Errorv(v)
|
||||
}
|
||||
|
||||
// Errorw writes msg along with fields into error log.
|
||||
func Errorw(ctx context.Context, msg string, fields ...LogField) {
|
||||
getLogger(ctx).Errorw(msg, fields...)
|
||||
}
|
||||
|
||||
// Field returns a LogField for the given key and value.
|
||||
func Field(key string, value interface{}) LogField {
|
||||
return logx.Field(key, value)
|
||||
}
|
||||
|
||||
// Info writes v into access log.
|
||||
func Info(ctx context.Context, v ...interface{}) {
|
||||
getLogger(ctx).Info(v...)
|
||||
}
|
||||
|
||||
// Infof writes v with format into access log.
|
||||
func Infof(ctx context.Context, format string, v ...interface{}) {
|
||||
getLogger(ctx).Infof(format, v...)
|
||||
}
|
||||
|
||||
// Infov writes v into access log with json content.
|
||||
func Infov(ctx context.Context, v interface{}) {
|
||||
getLogger(ctx).Infov(v)
|
||||
}
|
||||
|
||||
// Infow writes msg along with fields into access log.
|
||||
func Infow(ctx context.Context, msg string, fields ...LogField) {
|
||||
getLogger(ctx).Infow(msg, fields...)
|
||||
}
|
||||
|
||||
// Must checks if err is nil, otherwise logs the error and exits.
|
||||
func Must(err error) {
|
||||
logx.Must(err)
|
||||
}
|
||||
|
||||
// MustSetup sets up logging with given config c. It exits on error.
|
||||
func MustSetup(c logx.LogConf) {
|
||||
logx.MustSetup(c)
|
||||
}
|
||||
|
||||
// SetLevel sets the logging level. It can be used to suppress some logs.
|
||||
func SetLevel(level uint32) {
|
||||
logx.SetLevel(level)
|
||||
}
|
||||
|
||||
// SetUp sets up the logx. If already set up, just return nil.
|
||||
// we allow SetUp to be called multiple times, because for example
|
||||
// we need to allow different service frameworks to initialize logx respectively.
|
||||
// the same logic for SetUp
|
||||
func SetUp(c LogConf) error {
|
||||
return logx.SetUp(c)
|
||||
}
|
||||
|
||||
// Slow writes v into slow log.
|
||||
func Slow(ctx context.Context, v ...interface{}) {
|
||||
getLogger(ctx).Slow(v...)
|
||||
}
|
||||
|
||||
// Slowf writes v with format into slow log.
|
||||
func Slowf(ctx context.Context, format string, v ...interface{}) {
|
||||
getLogger(ctx).Slowf(format, v...)
|
||||
}
|
||||
|
||||
// Slowv writes v into slow log with json content.
|
||||
func Slowv(ctx context.Context, v interface{}) {
|
||||
getLogger(ctx).Slowv(v)
|
||||
}
|
||||
|
||||
// Sloww writes msg along with fields into slow log.
|
||||
func Sloww(ctx context.Context, msg string, fields ...LogField) {
|
||||
getLogger(ctx).Sloww(msg, fields...)
|
||||
}
|
||||
|
||||
// getLogger returns the logx.Logger with the given ctx and correct caller.
|
||||
func getLogger(ctx context.Context) logx.Logger {
|
||||
return logx.WithContext(ctx).WithCallerSkip(1)
|
||||
}
|
||||
218
core/logc/logs_test.go
Normal file
218
core/logc/logs_test.go
Normal file
@@ -0,0 +1,218 @@
|
||||
package logc
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
func TestAddGlobalFields(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
Info(context.Background(), "hello")
|
||||
buf.Reset()
|
||||
|
||||
AddGlobalFields(Field("a", "1"), Field("b", "2"))
|
||||
AddGlobalFields(Field("c", "3"))
|
||||
Info(context.Background(), "world")
|
||||
var m map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
|
||||
assert.Equal(t, "1", m["a"])
|
||||
assert.Equal(t, "2", m["b"])
|
||||
assert.Equal(t, "3", m["c"])
|
||||
}
|
||||
|
||||
func TestAlert(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
Alert(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), "foo"), buf.String())
|
||||
}
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Error(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestErrorf(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Errorf(context.Background(), "foo %s", "bar")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestErrorv(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Errorv(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestErrorw(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Errorw(context.Background(), "foo", Field("a", "b"))
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfo(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Info(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfof(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Infof(context.Background(), "foo %s", "bar")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfov(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Infov(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestInfow(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Infow(context.Background(), "foo", Field("a", "b"))
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestMust(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
Must(nil)
|
||||
})
|
||||
assert.NotPanics(t, func() {
|
||||
MustSetup(LogConf{})
|
||||
})
|
||||
}
|
||||
|
||||
func TestMisc(t *testing.T) {
|
||||
SetLevel(logx.DebugLevel)
|
||||
assert.NoError(t, SetUp(LogConf{}))
|
||||
assert.NoError(t, Close())
|
||||
}
|
||||
|
||||
func TestSlow(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Slow(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func TestSlowf(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Slowf(context.Background(), "foo %s", "bar")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func TestSlowv(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Slowv(context.Background(), "foo")
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func TestSloww(t *testing.T) {
|
||||
var buf strings.Builder
|
||||
writer := logx.NewWriter(&buf)
|
||||
old := logx.Reset()
|
||||
logx.SetWriter(writer)
|
||||
defer logx.SetWriter(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Sloww(context.Background(), "foo", Field("a", "b"))
|
||||
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
|
||||
}
|
||||
|
||||
func getFileLine() (string, int) {
|
||||
_, file, line, _ := runtime.Caller(1)
|
||||
short := file
|
||||
|
||||
for i := len(file) - 1; i > 0; i-- {
|
||||
if file[i] == '/' {
|
||||
short = file[i+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return short, line
|
||||
}
|
||||
26
core/logx/color.go
Normal file
26
core/logx/color.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
)
|
||||
|
||||
// WithColor is a helper function to add color to a string, only in plain encoding.
|
||||
func WithColor(text string, colour color.Color) string {
|
||||
if atomic.LoadUint32(&encoding) == plainEncodingType {
|
||||
return color.WithColor(text, colour)
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
|
||||
// WithColorPadding is a helper function to add color to a string with leading and trailing spaces,
|
||||
// only in plain encoding.
|
||||
func WithColorPadding(text string, colour color.Color) string {
|
||||
if atomic.LoadUint32(&encoding) == plainEncodingType {
|
||||
return color.WithColorPadding(text, colour)
|
||||
}
|
||||
|
||||
return text
|
||||
}
|
||||
33
core/logx/color_test.go
Normal file
33
core/logx/color_test.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
)
|
||||
|
||||
func TestWithColor(t *testing.T) {
|
||||
old := atomic.SwapUint32(&encoding, plainEncodingType)
|
||||
defer atomic.StoreUint32(&encoding, old)
|
||||
|
||||
output := WithColor("hello", color.BgBlue)
|
||||
assert.Equal(t, "hello", output)
|
||||
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
output = WithColor("hello", color.BgBlue)
|
||||
assert.Equal(t, "hello", output)
|
||||
}
|
||||
|
||||
func TestWithColorPadding(t *testing.T) {
|
||||
old := atomic.SwapUint32(&encoding, plainEncodingType)
|
||||
defer atomic.StoreUint32(&encoding, old)
|
||||
|
||||
output := WithColorPadding("hello", color.BgBlue)
|
||||
assert.Equal(t, " hello ", output)
|
||||
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
output = WithColorPadding("hello", color.BgBlue)
|
||||
assert.Equal(t, "hello", output)
|
||||
}
|
||||
@@ -7,8 +7,22 @@ type LogConf struct {
|
||||
Encoding string `json:",default=json,options=[json,plain]"`
|
||||
TimeFormat string `json:",optional"`
|
||||
Path string `json:",default=logs"`
|
||||
Level string `json:",default=info,options=[info,error,severe]"`
|
||||
Level string `json:",default=info,options=[debug,info,error,severe]"`
|
||||
MaxContentLength uint32 `json:",optional"`
|
||||
Compress bool `json:",optional"`
|
||||
Stat bool `json:",default=true"`
|
||||
KeepDays int `json:",optional"`
|
||||
StackCooldownMillis int `json:",default=100"`
|
||||
// MaxBackups represents how many backup log files will be kept. 0 means all files will be kept forever.
|
||||
// Only take effect when RotationRuleType is `size`.
|
||||
// Even thougth `MaxBackups` sets 0, log files will still be removed
|
||||
// if the `KeepDays` limitation is reached.
|
||||
MaxBackups int `json:",default=0"`
|
||||
// MaxSize represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`.
|
||||
// Only take effect when RotationRuleType is `size`
|
||||
MaxSize int `json:",default=0"`
|
||||
// RotationRuleType represents the type of log rotation rule. Default is `daily`.
|
||||
// daily: daily rotation.
|
||||
// size: size limited rotation.
|
||||
Rotation string `json:",default=daily,options=[daily,size]"`
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
)
|
||||
|
||||
const durationCallerDepth = 3
|
||||
|
||||
type durationLogger logEntry
|
||||
|
||||
// WithDuration returns a Logger which logs the given duration.
|
||||
func WithDuration(d time.Duration) Logger {
|
||||
return &durationLogger{
|
||||
Duration: timex.ReprOfDuration(d),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Error(v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Errorf(format string, v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Errorv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Info(v ...interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Infof(format string, v ...interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Infov(v interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Slow(v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Slowf(format string, v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) Slowv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *durationLogger) WithDuration(duration time.Duration) Logger {
|
||||
l.Duration = timex.ReprOfDuration(duration)
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *durationLogger) write(writer io.Writer, level string, val interface{}) {
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
writePlainAny(writer, level, val, l.Duration)
|
||||
default:
|
||||
outputJson(writer, &durationLogger{
|
||||
Timestamp: getTimestamp(),
|
||||
Level: level,
|
||||
Content: val,
|
||||
Duration: l.Duration,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestWithDurationError(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Error("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationErrorf(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Errorf("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationErrorv(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Errorv("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationInfo(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Info("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationInfoConsole(t *testing.T) {
|
||||
old := atomic.LoadUint32(&encoding)
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
defer func() {
|
||||
atomic.StoreUint32(&encoding, old)
|
||||
}()
|
||||
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Info("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "ms"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationInfof(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Infof("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationInfov(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Infov("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationSlow(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).Slow("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationSlowf(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).WithDuration(time.Hour).Slowf("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
|
||||
func TestWithDurationSlowv(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
WithDuration(time.Second).WithDuration(time.Hour).Slowv("foo")
|
||||
assert.True(t, strings.Contains(builder.String(), "duration"), builder.String())
|
||||
}
|
||||
48
core/logx/fields.go
Normal file
48
core/logx/fields.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
fieldsContextKey contextKey
|
||||
globalFields atomic.Value
|
||||
globalFieldsLock sync.Mutex
|
||||
)
|
||||
|
||||
type contextKey struct{}
|
||||
|
||||
// AddGlobalFields adds global fields.
|
||||
func AddGlobalFields(fields ...LogField) {
|
||||
globalFieldsLock.Lock()
|
||||
defer globalFieldsLock.Unlock()
|
||||
|
||||
old := globalFields.Load()
|
||||
if old == nil {
|
||||
globalFields.Store(append([]LogField(nil), fields...))
|
||||
} else {
|
||||
globalFields.Store(append(old.([]LogField), fields...))
|
||||
}
|
||||
}
|
||||
|
||||
// ContextWithFields returns a new context with the given fields.
|
||||
func ContextWithFields(ctx context.Context, fields ...LogField) context.Context {
|
||||
if val := ctx.Value(fieldsContextKey); val != nil {
|
||||
if arr, ok := val.([]LogField); ok {
|
||||
allFields := make([]LogField, 0, len(arr)+len(fields))
|
||||
allFields = append(allFields, arr...)
|
||||
allFields = append(allFields, fields...)
|
||||
return context.WithValue(ctx, fieldsContextKey, allFields)
|
||||
}
|
||||
}
|
||||
|
||||
return context.WithValue(ctx, fieldsContextKey, fields)
|
||||
}
|
||||
|
||||
// WithFields returns a new logger with the given fields.
|
||||
// deprecated: use ContextWithFields instead.
|
||||
func WithFields(ctx context.Context, fields ...LogField) context.Context {
|
||||
return ContextWithFields(ctx, fields...)
|
||||
}
|
||||
121
core/logx/fields_test.go
Normal file
121
core/logx/fields_test.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAddGlobalFields(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
writer := NewWriter(&buf)
|
||||
old := Reset()
|
||||
SetWriter(writer)
|
||||
defer SetWriter(old)
|
||||
|
||||
Info("hello")
|
||||
buf.Reset()
|
||||
|
||||
AddGlobalFields(Field("a", "1"), Field("b", "2"))
|
||||
AddGlobalFields(Field("c", "3"))
|
||||
Info("world")
|
||||
var m map[string]interface{}
|
||||
assert.NoError(t, json.Unmarshal(buf.Bytes(), &m))
|
||||
assert.Equal(t, "1", m["a"])
|
||||
assert.Equal(t, "2", m["b"])
|
||||
assert.Equal(t, "3", m["c"])
|
||||
}
|
||||
|
||||
func TestContextWithFields(t *testing.T) {
|
||||
ctx := ContextWithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||||
vals := ctx.Value(fieldsContextKey)
|
||||
assert.NotNil(t, vals)
|
||||
fields, ok := vals.([]LogField)
|
||||
assert.True(t, ok)
|
||||
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
|
||||
}
|
||||
|
||||
func TestWithFields(t *testing.T) {
|
||||
ctx := WithFields(context.Background(), Field("a", 1), Field("b", 2))
|
||||
vals := ctx.Value(fieldsContextKey)
|
||||
assert.NotNil(t, vals)
|
||||
fields, ok := vals.([]LogField)
|
||||
assert.True(t, ok)
|
||||
assert.EqualValues(t, []LogField{Field("a", 1), Field("b", 2)}, fields)
|
||||
}
|
||||
|
||||
func TestWithFieldsAppend(t *testing.T) {
|
||||
var dummyKey struct{}
|
||||
ctx := context.WithValue(context.Background(), dummyKey, "dummy")
|
||||
ctx = ContextWithFields(ctx, Field("a", 1), Field("b", 2))
|
||||
ctx = ContextWithFields(ctx, Field("c", 3), Field("d", 4))
|
||||
vals := ctx.Value(fieldsContextKey)
|
||||
assert.NotNil(t, vals)
|
||||
fields, ok := vals.([]LogField)
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "dummy", ctx.Value(dummyKey))
|
||||
assert.EqualValues(t, []LogField{
|
||||
Field("a", 1),
|
||||
Field("b", 2),
|
||||
Field("c", 3),
|
||||
Field("d", 4),
|
||||
}, fields)
|
||||
}
|
||||
|
||||
func TestWithFieldsAppendCopy(t *testing.T) {
|
||||
const count = 10
|
||||
ctx := context.Background()
|
||||
for i := 0; i < count; i++ {
|
||||
ctx = ContextWithFields(ctx, Field(strconv.Itoa(i), 1))
|
||||
}
|
||||
|
||||
af := Field("foo", 1)
|
||||
bf := Field("bar", 2)
|
||||
ctxa := ContextWithFields(ctx, af)
|
||||
ctxb := ContextWithFields(ctx, bf)
|
||||
|
||||
assert.EqualValues(t, af, ctxa.Value(fieldsContextKey).([]LogField)[count])
|
||||
assert.EqualValues(t, bf, ctxb.Value(fieldsContextKey).([]LogField)[count])
|
||||
}
|
||||
|
||||
func BenchmarkAtomicValue(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
var container atomic.Value
|
||||
vals := []LogField{
|
||||
Field("a", "b"),
|
||||
Field("c", "d"),
|
||||
Field("e", "f"),
|
||||
}
|
||||
container.Store(&vals)
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
val := container.Load()
|
||||
if val != nil {
|
||||
_ = *val.(*[]LogField)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRWMutex(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
var lock sync.RWMutex
|
||||
vals := []LogField{
|
||||
Field("a", "b"),
|
||||
Field("c", "d"),
|
||||
Field("e", "f"),
|
||||
}
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
lock.RLock()
|
||||
_ = vals
|
||||
lock.RUnlock()
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -9,23 +8,27 @@ import (
|
||||
)
|
||||
|
||||
func TestLessLogger_Error(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
l := NewLessLogger(500)
|
||||
for i := 0; i < 100; i++ {
|
||||
l.Error("hello")
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, strings.Count(builder.String(), "\n"))
|
||||
assert.Equal(t, 1, strings.Count(w.String(), "\n"))
|
||||
}
|
||||
|
||||
func TestLessLogger_Errorf(t *testing.T) {
|
||||
var builder strings.Builder
|
||||
log.SetOutput(&builder)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
l := NewLessLogger(500)
|
||||
for i := 0; i < 100; i++ {
|
||||
l.Errorf("hello")
|
||||
}
|
||||
|
||||
assert.Equal(t, 1, strings.Count(builder.String(), "\n"))
|
||||
assert.Equal(t, 1, strings.Count(w.String(), "\n"))
|
||||
}
|
||||
|
||||
50
core/logx/logger.go
Normal file
50
core/logx/logger.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A Logger represents a logger.
|
||||
type Logger interface {
|
||||
// Debug logs a message at info level.
|
||||
Debug(...interface{})
|
||||
// Debugf logs a message at info level.
|
||||
Debugf(string, ...interface{})
|
||||
// Debugv logs a message at info level.
|
||||
Debugv(interface{})
|
||||
// Debugw logs a message at info level.
|
||||
Debugw(string, ...LogField)
|
||||
// Error logs a message at error level.
|
||||
Error(...interface{})
|
||||
// Errorf logs a message at error level.
|
||||
Errorf(string, ...interface{})
|
||||
// Errorv logs a message at error level.
|
||||
Errorv(interface{})
|
||||
// Errorw logs a message at error level.
|
||||
Errorw(string, ...LogField)
|
||||
// Info logs a message at info level.
|
||||
Info(...interface{})
|
||||
// Infof logs a message at info level.
|
||||
Infof(string, ...interface{})
|
||||
// Infov logs a message at info level.
|
||||
Infov(interface{})
|
||||
// Infow logs a message at info level.
|
||||
Infow(string, ...LogField)
|
||||
// Slow logs a message at slow level.
|
||||
Slow(...interface{})
|
||||
// Slowf logs a message at slow level.
|
||||
Slowf(string, ...interface{})
|
||||
// Slowv logs a message at slow level.
|
||||
Slowv(interface{})
|
||||
// Sloww logs a message at slow level.
|
||||
Sloww(string, ...LogField)
|
||||
// WithCallerSkip returns a new logger with the given caller skip.
|
||||
WithCallerSkip(skip int) Logger
|
||||
// WithContext returns a new logger with the given context.
|
||||
WithContext(ctx context.Context) Logger
|
||||
// WithDuration returns a new logger with the given duration.
|
||||
WithDuration(d time.Duration) Logger
|
||||
// WithFields returns a new logger with the given fields.
|
||||
WithFields(fields ...LogField) Logger
|
||||
}
|
||||
@@ -1,220 +1,95 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/iox"
|
||||
"github.com/zeromicro/go-zero/core/sysx"
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
)
|
||||
|
||||
const (
|
||||
// InfoLevel logs everything
|
||||
InfoLevel = iota
|
||||
// ErrorLevel includes errors, slows, stacks
|
||||
ErrorLevel
|
||||
// SevereLevel only log severe messages
|
||||
SevereLevel
|
||||
)
|
||||
|
||||
const (
|
||||
jsonEncodingType = iota
|
||||
plainEncodingType
|
||||
|
||||
jsonEncoding = "json"
|
||||
plainEncoding = "plain"
|
||||
plainEncodingSep = '\t'
|
||||
)
|
||||
|
||||
const (
|
||||
accessFilename = "access.log"
|
||||
errorFilename = "error.log"
|
||||
severeFilename = "severe.log"
|
||||
slowFilename = "slow.log"
|
||||
statFilename = "stat.log"
|
||||
|
||||
consoleMode = "console"
|
||||
volumeMode = "volume"
|
||||
|
||||
levelAlert = "alert"
|
||||
levelInfo = "info"
|
||||
levelError = "error"
|
||||
levelSevere = "severe"
|
||||
levelFatal = "fatal"
|
||||
levelSlow = "slow"
|
||||
levelStat = "stat"
|
||||
|
||||
backupFileDelimiter = "-"
|
||||
callerInnerDepth = 5
|
||||
flags = 0x0
|
||||
)
|
||||
const callerDepth = 4
|
||||
|
||||
var (
|
||||
// ErrLogPathNotSet is an error that indicates the log path is not set.
|
||||
ErrLogPathNotSet = errors.New("log path must be set")
|
||||
// ErrLogNotInitialized is an error that log is not initialized.
|
||||
ErrLogNotInitialized = errors.New("log not initialized")
|
||||
// ErrLogServiceNameNotSet is an error that indicates that the service name is not set.
|
||||
ErrLogServiceNameNotSet = errors.New("log service name must be set")
|
||||
|
||||
timeFormat = "2006-01-02T15:04:05.000Z07:00"
|
||||
writeConsole bool
|
||||
logLevel uint32
|
||||
encoding uint32 = jsonEncodingType
|
||||
timeFormat = "2006-01-02T15:04:05.000Z07:00"
|
||||
logLevel uint32
|
||||
encoding uint32 = jsonEncodingType
|
||||
// maxContentLength is used to truncate the log content, 0 for not truncating.
|
||||
maxContentLength uint32
|
||||
// use uint32 for atomic operations
|
||||
disableLog uint32
|
||||
disableStat uint32
|
||||
infoLog io.WriteCloser
|
||||
errorLog io.WriteCloser
|
||||
severeLog io.WriteCloser
|
||||
slowLog io.WriteCloser
|
||||
statLog io.WriteCloser
|
||||
stackLog io.Writer
|
||||
|
||||
once sync.Once
|
||||
initialized uint32
|
||||
options logOptions
|
||||
writer = new(atomicWriter)
|
||||
setupOnce sync.Once
|
||||
)
|
||||
|
||||
type (
|
||||
logEntry struct {
|
||||
Timestamp string `json:"@timestamp"`
|
||||
Level string `json:"level"`
|
||||
Duration string `json:"duration,omitempty"`
|
||||
Content interface{} `json:"content"`
|
||||
}
|
||||
|
||||
logOptions struct {
|
||||
gzipEnabled bool
|
||||
logStackCooldownMills int
|
||||
keepDays int
|
||||
// LogField is a key-value pair that will be added to the log entry.
|
||||
LogField struct {
|
||||
Key string
|
||||
Value interface{}
|
||||
}
|
||||
|
||||
// LogOption defines the method to customize the logging.
|
||||
LogOption func(options *logOptions)
|
||||
|
||||
// A Logger represents a logger.
|
||||
Logger interface {
|
||||
Error(...interface{})
|
||||
Errorf(string, ...interface{})
|
||||
Errorv(interface{})
|
||||
Info(...interface{})
|
||||
Infof(string, ...interface{})
|
||||
Infov(interface{})
|
||||
Slow(...interface{})
|
||||
Slowf(string, ...interface{})
|
||||
Slowv(interface{})
|
||||
WithDuration(time.Duration) Logger
|
||||
logEntry map[string]interface{}
|
||||
|
||||
logOptions struct {
|
||||
gzipEnabled bool
|
||||
logStackCooldownMills int
|
||||
keepDays int
|
||||
maxBackups int
|
||||
maxSize int
|
||||
rotationRule string
|
||||
}
|
||||
)
|
||||
|
||||
// MustSetup sets up logging with given config c. It exits on error.
|
||||
func MustSetup(c LogConf) {
|
||||
Must(SetUp(c))
|
||||
}
|
||||
|
||||
// SetUp sets up the logx. If already set up, just return nil.
|
||||
// we allow SetUp to be called multiple times, because for example
|
||||
// we need to allow different service frameworks to initialize logx respectively.
|
||||
// the same logic for SetUp
|
||||
func SetUp(c LogConf) error {
|
||||
if len(c.TimeFormat) > 0 {
|
||||
timeFormat = c.TimeFormat
|
||||
}
|
||||
switch c.Encoding {
|
||||
case plainEncoding:
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
default:
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
}
|
||||
|
||||
switch c.Mode {
|
||||
case consoleMode:
|
||||
setupWithConsole(c)
|
||||
return nil
|
||||
case volumeMode:
|
||||
return setupWithVolume(c)
|
||||
default:
|
||||
return setupWithFiles(c)
|
||||
}
|
||||
}
|
||||
|
||||
// Alert alerts v in alert level, and the message is written to error log.
|
||||
func Alert(v string) {
|
||||
outputText(errorLog, levelAlert, v)
|
||||
getWriter().Alert(v)
|
||||
}
|
||||
|
||||
// Close closes the logging.
|
||||
func Close() error {
|
||||
if writeConsole {
|
||||
return nil
|
||||
}
|
||||
|
||||
if atomic.LoadUint32(&initialized) == 0 {
|
||||
return ErrLogNotInitialized
|
||||
}
|
||||
|
||||
atomic.StoreUint32(&initialized, 0)
|
||||
|
||||
if infoLog != nil {
|
||||
if err := infoLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if errorLog != nil {
|
||||
if err := errorLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if severeLog != nil {
|
||||
if err := severeLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if slowLog != nil {
|
||||
if err := slowLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if statLog != nil {
|
||||
if err := statLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if w := writer.Swap(nil); w != nil {
|
||||
return w.(io.Closer).Close()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Debug writes v into access log.
|
||||
func Debug(v ...interface{}) {
|
||||
writeDebug(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Debugf writes v with format into access log.
|
||||
func Debugf(format string, v ...interface{}) {
|
||||
writeDebug(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Debugv writes v into access log with json content.
|
||||
func Debugv(v interface{}) {
|
||||
writeDebug(v)
|
||||
}
|
||||
|
||||
// Debugw writes msg along with fields into access log.
|
||||
func Debugw(msg string, fields ...LogField) {
|
||||
writeDebug(msg, fields...)
|
||||
}
|
||||
|
||||
// Disable disables the logging.
|
||||
func Disable() {
|
||||
once.Do(func() {
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
|
||||
infoLog = iox.NopCloser(ioutil.Discard)
|
||||
errorLog = iox.NopCloser(ioutil.Discard)
|
||||
severeLog = iox.NopCloser(ioutil.Discard)
|
||||
slowLog = iox.NopCloser(ioutil.Discard)
|
||||
statLog = iox.NopCloser(ioutil.Discard)
|
||||
stackLog = ioutil.Discard
|
||||
})
|
||||
atomic.StoreUint32(&disableLog, 1)
|
||||
writer.Store(nopWriter{})
|
||||
}
|
||||
|
||||
// DisableStat disables the stat logs.
|
||||
@@ -224,65 +99,115 @@ func DisableStat() {
|
||||
|
||||
// Error writes v into error log.
|
||||
func Error(v ...interface{}) {
|
||||
ErrorCaller(1, v...)
|
||||
}
|
||||
|
||||
// ErrorCaller writes v with context into error log.
|
||||
func ErrorCaller(callDepth int, v ...interface{}) {
|
||||
errorTextSync(fmt.Sprint(v...), callDepth+callerInnerDepth)
|
||||
}
|
||||
|
||||
// ErrorCallerf writes v with context in format into error log.
|
||||
func ErrorCallerf(callDepth int, format string, v ...interface{}) {
|
||||
errorTextSync(fmt.Errorf(format, v...).Error(), callDepth+callerInnerDepth)
|
||||
writeError(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Errorf writes v with format into error log.
|
||||
func Errorf(format string, v ...interface{}) {
|
||||
ErrorCallerf(1, format, v...)
|
||||
writeError(fmt.Errorf(format, v...).Error())
|
||||
}
|
||||
|
||||
// ErrorStack writes v along with call stack into error log.
|
||||
func ErrorStack(v ...interface{}) {
|
||||
// there is newline in stack string
|
||||
stackSync(fmt.Sprint(v...))
|
||||
writeStack(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// ErrorStackf writes v along with call stack in format into error log.
|
||||
func ErrorStackf(format string, v ...interface{}) {
|
||||
// there is newline in stack string
|
||||
stackSync(fmt.Sprintf(format, v...))
|
||||
writeStack(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Errorv writes v into error log with json content.
|
||||
// No call stack attached, because not elegant to pack the messages.
|
||||
func Errorv(v interface{}) {
|
||||
errorAnySync(v)
|
||||
writeError(v)
|
||||
}
|
||||
|
||||
// Errorw writes msg along with fields into error log.
|
||||
func Errorw(msg string, fields ...LogField) {
|
||||
writeError(msg, fields...)
|
||||
}
|
||||
|
||||
// Field returns a LogField for the given key and value.
|
||||
func Field(key string, value interface{}) LogField {
|
||||
switch val := value.(type) {
|
||||
case error:
|
||||
return LogField{Key: key, Value: val.Error()}
|
||||
case []error:
|
||||
var errs []string
|
||||
for _, err := range val {
|
||||
errs = append(errs, err.Error())
|
||||
}
|
||||
return LogField{Key: key, Value: errs}
|
||||
case time.Duration:
|
||||
return LogField{Key: key, Value: fmt.Sprint(val)}
|
||||
case []time.Duration:
|
||||
var durs []string
|
||||
for _, dur := range val {
|
||||
durs = append(durs, fmt.Sprint(dur))
|
||||
}
|
||||
return LogField{Key: key, Value: durs}
|
||||
case []time.Time:
|
||||
var times []string
|
||||
for _, t := range val {
|
||||
times = append(times, fmt.Sprint(t))
|
||||
}
|
||||
return LogField{Key: key, Value: times}
|
||||
case fmt.Stringer:
|
||||
return LogField{Key: key, Value: val.String()}
|
||||
case []fmt.Stringer:
|
||||
var strs []string
|
||||
for _, str := range val {
|
||||
strs = append(strs, str.String())
|
||||
}
|
||||
return LogField{Key: key, Value: strs}
|
||||
default:
|
||||
return LogField{Key: key, Value: val}
|
||||
}
|
||||
}
|
||||
|
||||
// Info writes v into access log.
|
||||
func Info(v ...interface{}) {
|
||||
infoTextSync(fmt.Sprint(v...))
|
||||
writeInfo(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Infof writes v with format into access log.
|
||||
func Infof(format string, v ...interface{}) {
|
||||
infoTextSync(fmt.Sprintf(format, v...))
|
||||
writeInfo(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Infov writes v into access log with json content.
|
||||
func Infov(v interface{}) {
|
||||
infoAnySync(v)
|
||||
writeInfo(v)
|
||||
}
|
||||
|
||||
// Must checks if err is nil, otherwise logs the err and exits.
|
||||
// Infow writes msg along with fields into access log.
|
||||
func Infow(msg string, fields ...LogField) {
|
||||
writeInfo(msg, fields...)
|
||||
}
|
||||
|
||||
// Must checks if err is nil, otherwise logs the error and exits.
|
||||
func Must(err error) {
|
||||
if err != nil {
|
||||
msg := formatWithCaller(err.Error(), 3)
|
||||
log.Print(msg)
|
||||
outputText(severeLog, levelFatal, msg)
|
||||
os.Exit(1)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
msg := err.Error()
|
||||
log.Print(msg)
|
||||
getWriter().Severe(msg)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// MustSetup sets up logging with given config c. It exits on error.
|
||||
func MustSetup(c LogConf) {
|
||||
Must(SetUp(c))
|
||||
}
|
||||
|
||||
// Reset clears the writer and resets the log level.
|
||||
func Reset() Writer {
|
||||
return writer.Swap(nil)
|
||||
}
|
||||
|
||||
// SetLevel sets the logging level. It can be used to suppress some logs.
|
||||
@@ -290,39 +215,91 @@ func SetLevel(level uint32) {
|
||||
atomic.StoreUint32(&logLevel, level)
|
||||
}
|
||||
|
||||
// SetWriter sets the logging writer. It can be used to customize the logging.
|
||||
func SetWriter(w Writer) {
|
||||
if atomic.LoadUint32(&disableLog) == 0 {
|
||||
writer.Store(w)
|
||||
}
|
||||
}
|
||||
|
||||
// SetUp sets up the logx. If already set up, just return nil.
|
||||
// we allow SetUp to be called multiple times, because for example
|
||||
// we need to allow different service frameworks to initialize logx respectively.
|
||||
func SetUp(c LogConf) (err error) {
|
||||
// Just ignore the subsequent SetUp calls.
|
||||
// Because multiple services in one process might call SetUp respectively.
|
||||
// Need to wait for the first caller to complete the execution.
|
||||
setupOnce.Do(func() {
|
||||
setupLogLevel(c)
|
||||
|
||||
if !c.Stat {
|
||||
DisableStat()
|
||||
}
|
||||
|
||||
if len(c.TimeFormat) > 0 {
|
||||
timeFormat = c.TimeFormat
|
||||
}
|
||||
|
||||
atomic.StoreUint32(&maxContentLength, c.MaxContentLength)
|
||||
|
||||
switch c.Encoding {
|
||||
case plainEncoding:
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
default:
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
}
|
||||
|
||||
switch c.Mode {
|
||||
case fileMode:
|
||||
err = setupWithFiles(c)
|
||||
case volumeMode:
|
||||
err = setupWithVolume(c)
|
||||
default:
|
||||
setupWithConsole()
|
||||
}
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Severe writes v into severe log.
|
||||
func Severe(v ...interface{}) {
|
||||
severeSync(fmt.Sprint(v...))
|
||||
writeSevere(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Severef writes v with format into severe log.
|
||||
func Severef(format string, v ...interface{}) {
|
||||
severeSync(fmt.Sprintf(format, v...))
|
||||
writeSevere(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Slow writes v into slow log.
|
||||
func Slow(v ...interface{}) {
|
||||
slowTextSync(fmt.Sprint(v...))
|
||||
writeSlow(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Slowf writes v with format into slow log.
|
||||
func Slowf(format string, v ...interface{}) {
|
||||
slowTextSync(fmt.Sprintf(format, v...))
|
||||
writeSlow(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// Slowv writes v into slow log with json content.
|
||||
func Slowv(v interface{}) {
|
||||
slowAnySync(v)
|
||||
writeSlow(v)
|
||||
}
|
||||
|
||||
// Sloww writes msg along with fields into slow log.
|
||||
func Sloww(msg string, fields ...LogField) {
|
||||
writeSlow(msg, fields...)
|
||||
}
|
||||
|
||||
// Stat writes v into stat log.
|
||||
func Stat(v ...interface{}) {
|
||||
statSync(fmt.Sprint(v...))
|
||||
writeStat(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
// Statf writes v with format into stat log.
|
||||
func Statf(format string, v ...interface{}) {
|
||||
statSync(fmt.Sprintf(format, v...))
|
||||
writeStat(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
// WithCooldownMillis customizes logging on writing call stack interval.
|
||||
@@ -346,63 +323,53 @@ func WithGzip() LogOption {
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxBackups customizes how many log files backups will be kept.
|
||||
func WithMaxBackups(count int) LogOption {
|
||||
return func(opts *logOptions) {
|
||||
opts.maxBackups = count
|
||||
}
|
||||
}
|
||||
|
||||
// WithMaxSize customizes how much space the writing log file can take up.
|
||||
func WithMaxSize(size int) LogOption {
|
||||
return func(opts *logOptions) {
|
||||
opts.maxSize = size
|
||||
}
|
||||
}
|
||||
|
||||
// WithRotation customizes which log rotation rule to use.
|
||||
func WithRotation(r string) LogOption {
|
||||
return func(opts *logOptions) {
|
||||
opts.rotationRule = r
|
||||
}
|
||||
}
|
||||
|
||||
func addCaller(fields ...LogField) []LogField {
|
||||
return append(fields, Field(callerKey, getCaller(callerDepth)))
|
||||
}
|
||||
|
||||
func createOutput(path string) (io.WriteCloser, error) {
|
||||
if len(path) == 0 {
|
||||
return nil, ErrLogPathNotSet
|
||||
}
|
||||
|
||||
return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays,
|
||||
options.gzipEnabled), options.gzipEnabled)
|
||||
}
|
||||
|
||||
func errorAnySync(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputAny(errorLog, levelError, v)
|
||||
switch options.rotationRule {
|
||||
case sizeRotationRule:
|
||||
return NewLogger(path, NewSizeLimitRotateRule(path, backupFileDelimiter, options.keepDays,
|
||||
options.maxSize, options.maxBackups, options.gzipEnabled), options.gzipEnabled)
|
||||
default:
|
||||
return NewLogger(path, DefaultRotateRule(path, backupFileDelimiter, options.keepDays,
|
||||
options.gzipEnabled), options.gzipEnabled)
|
||||
}
|
||||
}
|
||||
|
||||
func errorTextSync(msg string, callDepth int) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputError(errorLog, msg, callDepth)
|
||||
}
|
||||
}
|
||||
|
||||
func formatWithCaller(msg string, callDepth int) string {
|
||||
var buf strings.Builder
|
||||
|
||||
caller := getCaller(callDepth)
|
||||
if len(caller) > 0 {
|
||||
buf.WriteString(caller)
|
||||
buf.WriteByte(' ')
|
||||
func getWriter() Writer {
|
||||
w := writer.Load()
|
||||
if w == nil {
|
||||
w = writer.StoreIfNil(newConsoleWriter())
|
||||
}
|
||||
|
||||
buf.WriteString(msg)
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func getCaller(callDepth int) string {
|
||||
var buf strings.Builder
|
||||
|
||||
_, file, line, ok := runtime.Caller(callDepth)
|
||||
if ok {
|
||||
short := file
|
||||
for i := len(file) - 1; i > 0; i-- {
|
||||
if file[i] == '/' {
|
||||
short = file[i+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
buf.WriteString(short)
|
||||
buf.WriteByte(':')
|
||||
buf.WriteString(strconv.Itoa(line))
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func getTimestamp() string {
|
||||
return timex.Time().Format(timeFormat)
|
||||
return w
|
||||
}
|
||||
|
||||
func handleOptions(opts []LogOption) {
|
||||
@@ -411,63 +378,10 @@ func handleOptions(opts []LogOption) {
|
||||
}
|
||||
}
|
||||
|
||||
func infoAnySync(val interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
outputAny(infoLog, levelInfo, val)
|
||||
}
|
||||
}
|
||||
|
||||
func infoTextSync(msg string) {
|
||||
if shallLog(InfoLevel) {
|
||||
outputText(infoLog, levelInfo, msg)
|
||||
}
|
||||
}
|
||||
|
||||
func outputAny(writer io.Writer, level string, val interface{}) {
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
writePlainAny(writer, level, val)
|
||||
default:
|
||||
info := logEntry{
|
||||
Timestamp: getTimestamp(),
|
||||
Level: level,
|
||||
Content: val,
|
||||
}
|
||||
outputJson(writer, info)
|
||||
}
|
||||
}
|
||||
|
||||
func outputText(writer io.Writer, level, msg string) {
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
writePlainText(writer, level, msg)
|
||||
default:
|
||||
info := logEntry{
|
||||
Timestamp: getTimestamp(),
|
||||
Level: level,
|
||||
Content: msg,
|
||||
}
|
||||
outputJson(writer, info)
|
||||
}
|
||||
}
|
||||
|
||||
func outputError(writer io.Writer, msg string, callDepth int) {
|
||||
content := formatWithCaller(msg, callDepth)
|
||||
outputText(writer, levelError, content)
|
||||
}
|
||||
|
||||
func outputJson(writer io.Writer, info interface{}) {
|
||||
if content, err := json.Marshal(info); err != nil {
|
||||
log.Println(err.Error())
|
||||
} else if atomic.LoadUint32(&initialized) == 0 || writer == nil {
|
||||
log.Println(string(content))
|
||||
} else {
|
||||
writer.Write(append(content, '\n'))
|
||||
}
|
||||
}
|
||||
|
||||
func setupLogLevel(c LogConf) {
|
||||
switch c.Level {
|
||||
case levelDebug:
|
||||
SetLevel(DebugLevel)
|
||||
case levelInfo:
|
||||
SetLevel(InfoLevel)
|
||||
case levelError:
|
||||
@@ -477,72 +391,18 @@ func setupLogLevel(c LogConf) {
|
||||
}
|
||||
}
|
||||
|
||||
func setupWithConsole(c LogConf) {
|
||||
once.Do(func() {
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
writeConsole = true
|
||||
setupLogLevel(c)
|
||||
|
||||
infoLog = newLogWriter(log.New(os.Stdout, "", flags))
|
||||
errorLog = newLogWriter(log.New(os.Stderr, "", flags))
|
||||
severeLog = newLogWriter(log.New(os.Stderr, "", flags))
|
||||
slowLog = newLogWriter(log.New(os.Stderr, "", flags))
|
||||
stackLog = newLessWriter(errorLog, options.logStackCooldownMills)
|
||||
statLog = infoLog
|
||||
})
|
||||
func setupWithConsole() {
|
||||
SetWriter(newConsoleWriter())
|
||||
}
|
||||
|
||||
func setupWithFiles(c LogConf) error {
|
||||
var opts []LogOption
|
||||
var err error
|
||||
|
||||
if len(c.Path) == 0 {
|
||||
return ErrLogPathNotSet
|
||||
w, err := newFileWriter(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
opts = append(opts, WithCooldownMillis(c.StackCooldownMillis))
|
||||
if c.Compress {
|
||||
opts = append(opts, WithGzip())
|
||||
}
|
||||
if c.KeepDays > 0 {
|
||||
opts = append(opts, WithKeepDays(c.KeepDays))
|
||||
}
|
||||
|
||||
accessFile := path.Join(c.Path, accessFilename)
|
||||
errorFile := path.Join(c.Path, errorFilename)
|
||||
severeFile := path.Join(c.Path, severeFilename)
|
||||
slowFile := path.Join(c.Path, slowFilename)
|
||||
statFile := path.Join(c.Path, statFilename)
|
||||
|
||||
once.Do(func() {
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
handleOptions(opts)
|
||||
setupLogLevel(c)
|
||||
|
||||
if infoLog, err = createOutput(accessFile); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if errorLog, err = createOutput(errorFile); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if severeLog, err = createOutput(severeFile); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if slowLog, err = createOutput(slowFile); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if statLog, err = createOutput(statFile); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stackLog = newLessWriter(errorLog, options.logStackCooldownMills)
|
||||
})
|
||||
|
||||
return err
|
||||
SetWriter(w)
|
||||
return nil
|
||||
}
|
||||
|
||||
func setupWithVolume(c LogConf) error {
|
||||
@@ -554,12 +414,6 @@ func setupWithVolume(c LogConf) error {
|
||||
return setupWithFiles(c)
|
||||
}
|
||||
|
||||
func severeSync(msg string) {
|
||||
if shallLog(SevereLevel) {
|
||||
outputText(severeLog, levelSevere, fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
}
|
||||
}
|
||||
|
||||
func shallLog(level uint32) bool {
|
||||
return atomic.LoadUint32(&logLevel) <= level
|
||||
}
|
||||
@@ -568,101 +422,44 @@ func shallLogStat() bool {
|
||||
return atomic.LoadUint32(&disableStat) == 0
|
||||
}
|
||||
|
||||
func slowAnySync(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputAny(slowLog, levelSlow, v)
|
||||
func writeDebug(val interface{}, fields ...LogField) {
|
||||
if shallLog(DebugLevel) {
|
||||
getWriter().Debug(val, addCaller(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func slowTextSync(msg string) {
|
||||
func writeError(val interface{}, fields ...LogField) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputText(slowLog, levelSlow, msg)
|
||||
getWriter().Error(val, addCaller(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func stackSync(msg string) {
|
||||
if shallLog(ErrorLevel) {
|
||||
outputText(stackLog, levelError, fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
func writeInfo(val interface{}, fields ...LogField) {
|
||||
if shallLog(InfoLevel) {
|
||||
getWriter().Info(val, addCaller(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func statSync(msg string) {
|
||||
func writeSevere(msg string) {
|
||||
if shallLog(SevereLevel) {
|
||||
getWriter().Severe(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
}
|
||||
}
|
||||
|
||||
func writeSlow(val interface{}, fields ...LogField) {
|
||||
if shallLog(ErrorLevel) {
|
||||
getWriter().Slow(val, addCaller(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func writeStack(msg string) {
|
||||
if shallLog(ErrorLevel) {
|
||||
getWriter().Stack(fmt.Sprintf("%s\n%s", msg, string(debug.Stack())))
|
||||
}
|
||||
}
|
||||
|
||||
func writeStat(msg string) {
|
||||
if shallLogStat() && shallLog(InfoLevel) {
|
||||
outputText(statLog, levelStat, msg)
|
||||
getWriter().Stat(msg, addCaller()...)
|
||||
}
|
||||
}
|
||||
|
||||
func writePlainAny(writer io.Writer, level string, val interface{}, fields ...string) {
|
||||
switch v := val.(type) {
|
||||
case string:
|
||||
writePlainText(writer, level, v, fields...)
|
||||
case error:
|
||||
writePlainText(writer, level, v.Error(), fields...)
|
||||
case fmt.Stringer:
|
||||
writePlainText(writer, level, v.String(), fields...)
|
||||
default:
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(getTimestamp())
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(level)
|
||||
for _, item := range fields {
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(item)
|
||||
}
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
||||
log.Println(err.Error())
|
||||
return
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
if atomic.LoadUint32(&initialized) == 0 || writer == nil {
|
||||
log.Println(buf.String())
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(getTimestamp())
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(level)
|
||||
for _, item := range fields {
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(item)
|
||||
}
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(msg)
|
||||
buf.WriteByte('\n')
|
||||
if atomic.LoadUint32(&initialized) == 0 || writer == nil {
|
||||
log.Println(buf.String())
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
type logWriter struct {
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func newLogWriter(logger *log.Logger) logWriter {
|
||||
return logWriter{
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (lw logWriter) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lw logWriter) Write(data []byte) (int, error) {
|
||||
lw.logger.Print(string(data))
|
||||
return len(data), nil
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -19,8 +19,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
s = []byte("Sending #11 notification (id: 1451875113812010473) in #1 connection")
|
||||
pool = make(chan []byte, 1)
|
||||
s = []byte("Sending #11 notification (id: 1451875113812010473) in #1 connection")
|
||||
pool = make(chan []byte, 1)
|
||||
_ Writer = (*mockWriter)(nil)
|
||||
)
|
||||
|
||||
type mockWriter struct {
|
||||
@@ -28,10 +29,52 @@ type mockWriter struct {
|
||||
builder strings.Builder
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Write(data []byte) (int, error) {
|
||||
func (mw *mockWriter) Alert(v interface{}) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
return mw.builder.Write(data)
|
||||
output(&mw.builder, levelAlert, v)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Debug(v interface{}, fields ...LogField) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelDebug, v, fields...)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Error(v interface{}, fields ...LogField) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelError, v, fields...)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Info(v interface{}, fields ...LogField) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelInfo, v, fields...)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Severe(v interface{}) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelSevere, v)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Slow(v interface{}, fields ...LogField) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelSlow, v, fields...)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Stack(v interface{}) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelError, v)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Stat(v interface{}, fields ...LogField) {
|
||||
mw.lock.Lock()
|
||||
defer mw.lock.Unlock()
|
||||
output(&mw.builder, levelStat, v, fields...)
|
||||
}
|
||||
|
||||
func (mw *mockWriter) Close() error {
|
||||
@@ -56,95 +99,251 @@ func (mw *mockWriter) String() string {
|
||||
return mw.builder.String()
|
||||
}
|
||||
|
||||
func TestField(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
f LogField
|
||||
want map[string]interface{}
|
||||
}{
|
||||
{
|
||||
name: "error",
|
||||
f: Field("foo", errors.New("bar")),
|
||||
want: map[string]interface{}{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "errors",
|
||||
f: Field("foo", []error{errors.New("bar"), errors.New("baz")}),
|
||||
want: map[string]interface{}{
|
||||
"foo": []interface{}{"bar", "baz"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "strings",
|
||||
f: Field("foo", []string{"bar", "baz"}),
|
||||
want: map[string]interface{}{
|
||||
"foo": []interface{}{"bar", "baz"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "duration",
|
||||
f: Field("foo", time.Second),
|
||||
want: map[string]interface{}{
|
||||
"foo": "1s",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "durations",
|
||||
f: Field("foo", []time.Duration{time.Second, 2 * time.Second}),
|
||||
want: map[string]interface{}{
|
||||
"foo": []interface{}{"1s", "2s"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "times",
|
||||
f: Field("foo", []time.Time{
|
||||
time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC),
|
||||
time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC),
|
||||
}),
|
||||
want: map[string]interface{}{
|
||||
"foo": []interface{}{"2020-01-01 00:00:00 +0000 UTC", "2020-01-02 00:00:00 +0000 UTC"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "stringer",
|
||||
f: Field("foo", ValStringer{val: "bar"}),
|
||||
want: map[string]interface{}{
|
||||
"foo": "bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "stringers",
|
||||
f: Field("foo", []fmt.Stringer{ValStringer{val: "bar"}, ValStringer{val: "baz"}}),
|
||||
want: map[string]interface{}{
|
||||
"foo": []interface{}{"bar", "baz"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
Infow("foo", test.f)
|
||||
validateFields(t, w.String(), test.want)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFileLineFileMode(t *testing.T) {
|
||||
writer := new(mockWriter)
|
||||
errorLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Error("anything")
|
||||
assert.True(t, writer.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
|
||||
writer.Reset()
|
||||
file, line = getFileLine()
|
||||
Errorf("anything %s", "format")
|
||||
assert.True(t, writer.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestFileLineConsoleMode(t *testing.T) {
|
||||
writer := new(mockWriter)
|
||||
writeConsole = true
|
||||
errorLog = newLogWriter(log.New(writer, "[ERROR] ", flags))
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
file, line := getFileLine()
|
||||
Error("anything")
|
||||
assert.True(t, writer.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
|
||||
writer.Reset()
|
||||
w.Reset()
|
||||
file, line = getFileLine()
|
||||
Errorf("anything %s", "format")
|
||||
assert.True(t, writer.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestStructedLogAlert(t *testing.T) {
|
||||
doTestStructedLog(t, levelAlert, func(writer io.WriteCloser) {
|
||||
errorLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelAlert, w, func(v ...interface{}) {
|
||||
Alert(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebug(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debug(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebugf(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debugf(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebugv(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debugv(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogDebugw(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelDebug, w, func(v ...interface{}) {
|
||||
Debugw(fmt.Sprint(v...), Field("foo", time.Second))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogError(t *testing.T) {
|
||||
doTestStructedLog(t, levelError, func(writer io.WriteCloser) {
|
||||
errorLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelError, w, func(v ...interface{}) {
|
||||
Error(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogErrorf(t *testing.T) {
|
||||
doTestStructedLog(t, levelError, func(writer io.WriteCloser) {
|
||||
errorLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelError, w, func(v ...interface{}) {
|
||||
Errorf("%s", fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogErrorv(t *testing.T) {
|
||||
doTestStructedLog(t, levelError, func(writer io.WriteCloser) {
|
||||
errorLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelError, w, func(v ...interface{}) {
|
||||
Errorv(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogErrorw(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelError, w, func(v ...interface{}) {
|
||||
Errorw(fmt.Sprint(v...), Field("foo", "bar"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfo(t *testing.T) {
|
||||
doTestStructedLog(t, levelInfo, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelInfo, w, func(v ...interface{}) {
|
||||
Info(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfof(t *testing.T) {
|
||||
doTestStructedLog(t, levelInfo, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelInfo, w, func(v ...interface{}) {
|
||||
Infof("%s", fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfov(t *testing.T) {
|
||||
doTestStructedLog(t, levelInfo, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelInfo, w, func(v ...interface{}) {
|
||||
Infov(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfow(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelInfo, w, func(v ...interface{}) {
|
||||
Infow(fmt.Sprint(v...), Field("foo", "bar"))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogInfoConsoleAny(t *testing.T) {
|
||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLogConsole(t, w, func(v ...interface{}) {
|
||||
old := atomic.LoadUint32(&encoding)
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
defer func() {
|
||||
@@ -156,9 +355,11 @@ func TestStructedLogInfoConsoleAny(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStructedLogInfoConsoleAnyString(t *testing.T) {
|
||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLogConsole(t, w, func(v ...interface{}) {
|
||||
old := atomic.LoadUint32(&encoding)
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
defer func() {
|
||||
@@ -170,9 +371,11 @@ func TestStructedLogInfoConsoleAnyString(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStructedLogInfoConsoleAnyError(t *testing.T) {
|
||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLogConsole(t, w, func(v ...interface{}) {
|
||||
old := atomic.LoadUint32(&encoding)
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
defer func() {
|
||||
@@ -184,9 +387,11 @@ func TestStructedLogInfoConsoleAnyError(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStructedLogInfoConsoleAnyStringer(t *testing.T) {
|
||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLogConsole(t, w, func(v ...interface{}) {
|
||||
old := atomic.LoadUint32(&encoding)
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
defer func() {
|
||||
@@ -200,9 +405,11 @@ func TestStructedLogInfoConsoleAnyStringer(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStructedLogInfoConsoleText(t *testing.T) {
|
||||
doTestStructedLogConsole(t, func(writer io.WriteCloser) {
|
||||
infoLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLogConsole(t, w, func(v ...interface{}) {
|
||||
old := atomic.LoadUint32(&encoding)
|
||||
atomic.StoreUint32(&encoding, plainEncodingType)
|
||||
defer func() {
|
||||
@@ -214,115 +421,147 @@ func TestStructedLogInfoConsoleText(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStructedLogSlow(t *testing.T) {
|
||||
doTestStructedLog(t, levelSlow, func(writer io.WriteCloser) {
|
||||
slowLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelSlow, w, func(v ...interface{}) {
|
||||
Slow(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogSlowf(t *testing.T) {
|
||||
doTestStructedLog(t, levelSlow, func(writer io.WriteCloser) {
|
||||
slowLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelSlow, w, func(v ...interface{}) {
|
||||
Slowf(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogSlowv(t *testing.T) {
|
||||
doTestStructedLog(t, levelSlow, func(writer io.WriteCloser) {
|
||||
slowLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelSlow, w, func(v ...interface{}) {
|
||||
Slowv(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogSloww(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelSlow, w, func(v ...interface{}) {
|
||||
Sloww(fmt.Sprint(v...), Field("foo", time.Second))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogStat(t *testing.T) {
|
||||
doTestStructedLog(t, levelStat, func(writer io.WriteCloser) {
|
||||
statLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelStat, w, func(v ...interface{}) {
|
||||
Stat(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogStatf(t *testing.T) {
|
||||
doTestStructedLog(t, levelStat, func(writer io.WriteCloser) {
|
||||
statLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelStat, w, func(v ...interface{}) {
|
||||
Statf(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogSevere(t *testing.T) {
|
||||
doTestStructedLog(t, levelSevere, func(writer io.WriteCloser) {
|
||||
severeLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelSevere, w, func(v ...interface{}) {
|
||||
Severe(v...)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogSeveref(t *testing.T) {
|
||||
doTestStructedLog(t, levelSevere, func(writer io.WriteCloser) {
|
||||
severeLog = writer
|
||||
}, func(v ...interface{}) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
doTestStructedLog(t, levelSevere, w, func(v ...interface{}) {
|
||||
Severef(fmt.Sprint(v...))
|
||||
})
|
||||
}
|
||||
|
||||
func TestStructedLogWithDuration(t *testing.T) {
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
infoLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
WithDuration(time.Second).Info(message)
|
||||
var entry logEntry
|
||||
if err := json.Unmarshal([]byte(writer.builder.String()), &entry); err != nil {
|
||||
var entry map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(w.String()), &entry); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, levelInfo, entry.Level)
|
||||
assert.Equal(t, message, entry.Content)
|
||||
assert.Equal(t, "1000.0ms", entry.Duration)
|
||||
assert.Equal(t, levelInfo, entry[levelKey])
|
||||
assert.Equal(t, message, entry[contentKey])
|
||||
assert.Equal(t, "1000.0ms", entry[durationKey])
|
||||
}
|
||||
|
||||
func TestSetLevel(t *testing.T) {
|
||||
SetLevel(ErrorLevel)
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
infoLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
Info(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
assert.Equal(t, 0, w.builder.Len())
|
||||
}
|
||||
|
||||
func TestSetLevelTwiceWithMode(t *testing.T) {
|
||||
testModes := []string{
|
||||
"mode",
|
||||
"console",
|
||||
"volumn",
|
||||
"mode",
|
||||
}
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
for _, mode := range testModes {
|
||||
testSetLevelTwiceWithMode(t, mode)
|
||||
testSetLevelTwiceWithMode(t, mode, w)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetLevelWithDuration(t *testing.T) {
|
||||
SetLevel(ErrorLevel)
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
infoLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
WithDuration(time.Second).Info(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
assert.Equal(t, 0, w.builder.Len())
|
||||
}
|
||||
|
||||
func TestErrorfWithWrappedError(t *testing.T) {
|
||||
SetLevel(ErrorLevel)
|
||||
const message = "there"
|
||||
writer := new(mockWriter)
|
||||
errorLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
Errorf("hello %w", errors.New(message))
|
||||
assert.True(t, strings.Contains(writer.builder.String(), "hello there"))
|
||||
assert.True(t, strings.Contains(w.String(), "hello there"))
|
||||
}
|
||||
|
||||
func TestMustNil(t *testing.T) {
|
||||
@@ -330,9 +569,15 @@ func TestMustNil(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSetup(t *testing.T) {
|
||||
defer func() {
|
||||
SetLevel(InfoLevel)
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
}()
|
||||
|
||||
MustSetup(LogConf{
|
||||
ServiceName: "any",
|
||||
Mode: "console",
|
||||
TimeFormat: timeFormat,
|
||||
})
|
||||
MustSetup(LogConf{
|
||||
ServiceName: "any",
|
||||
@@ -344,13 +589,34 @@ func TestSetup(t *testing.T) {
|
||||
Mode: "volume",
|
||||
Path: os.TempDir(),
|
||||
})
|
||||
MustSetup(LogConf{
|
||||
ServiceName: "any",
|
||||
Mode: "console",
|
||||
TimeFormat: timeFormat,
|
||||
})
|
||||
MustSetup(LogConf{
|
||||
ServiceName: "any",
|
||||
Mode: "console",
|
||||
Encoding: plainEncoding,
|
||||
})
|
||||
|
||||
defer os.RemoveAll("CD01CB7D-2705-4F3F-889E-86219BF56F10")
|
||||
assert.NotNil(t, setupWithVolume(LogConf{}))
|
||||
assert.Nil(t, setupWithVolume(LogConf{
|
||||
ServiceName: "CD01CB7D-2705-4F3F-889E-86219BF56F10",
|
||||
}))
|
||||
assert.Nil(t, setupWithVolume(LogConf{
|
||||
ServiceName: "CD01CB7D-2705-4F3F-889E-86219BF56F10",
|
||||
Rotation: sizeRotationRule,
|
||||
}))
|
||||
assert.NotNil(t, setupWithFiles(LogConf{}))
|
||||
assert.Nil(t, setupWithFiles(LogConf{
|
||||
ServiceName: "any",
|
||||
Path: os.TempDir(),
|
||||
Compress: true,
|
||||
KeepDays: 1,
|
||||
MaxBackups: 3,
|
||||
MaxSize: 1024 * 1024,
|
||||
}))
|
||||
setupLogLevel(LogConf{
|
||||
Level: levelInfo,
|
||||
@@ -364,6 +630,8 @@ func TestSetup(t *testing.T) {
|
||||
_, err := createOutput("")
|
||||
assert.NotNil(t, err)
|
||||
Disable()
|
||||
SetLevel(InfoLevel)
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
}
|
||||
|
||||
func TestDisable(t *testing.T) {
|
||||
@@ -372,8 +640,9 @@ func TestDisable(t *testing.T) {
|
||||
var opt logOptions
|
||||
WithKeepDays(1)(&opt)
|
||||
WithGzip()(&opt)
|
||||
WithMaxBackups(1)(&opt)
|
||||
WithMaxSize(1024)(&opt)
|
||||
assert.Nil(t, Close())
|
||||
writeConsole = false
|
||||
assert.Nil(t, Close())
|
||||
}
|
||||
|
||||
@@ -381,11 +650,22 @@ func TestDisableStat(t *testing.T) {
|
||||
DisableStat()
|
||||
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
statLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
Stat(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
assert.Equal(t, 0, w.builder.Len())
|
||||
}
|
||||
|
||||
func TestSetWriter(t *testing.T) {
|
||||
atomic.StoreUint32(&disableLog, 0)
|
||||
Reset()
|
||||
SetWriter(nopWriter{})
|
||||
assert.NotNil(t, writer.Load())
|
||||
assert.True(t, writer.Load() == nopWriter{})
|
||||
mocked := new(mockWriter)
|
||||
SetWriter(mocked)
|
||||
assert.Equal(t, mocked, writer.Load())
|
||||
}
|
||||
|
||||
func TestWithGzip(t *testing.T) {
|
||||
@@ -428,7 +708,7 @@ func BenchmarkCopyByteSlice(b *testing.B) {
|
||||
buf = make([]byte, len(s))
|
||||
copy(buf, s)
|
||||
}
|
||||
fmt.Fprint(ioutil.Discard, buf)
|
||||
fmt.Fprint(io.Discard, buf)
|
||||
}
|
||||
|
||||
func BenchmarkCopyOnWriteByteSlice(b *testing.B) {
|
||||
@@ -437,7 +717,7 @@ func BenchmarkCopyOnWriteByteSlice(b *testing.B) {
|
||||
size := len(s)
|
||||
buf = s[:size:size]
|
||||
}
|
||||
fmt.Fprint(ioutil.Discard, buf)
|
||||
fmt.Fprint(io.Discard, buf)
|
||||
}
|
||||
|
||||
func BenchmarkCacheByteSlice(b *testing.B) {
|
||||
@@ -451,7 +731,7 @@ func BenchmarkCacheByteSlice(b *testing.B) {
|
||||
func BenchmarkLogs(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
log.SetOutput(ioutil.Discard)
|
||||
log.SetOutput(io.Discard)
|
||||
for i := 0; i < b.N; i++ {
|
||||
Info(i)
|
||||
}
|
||||
@@ -487,39 +767,36 @@ func put(b []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
func doTestStructedLog(t *testing.T, level string, setup func(writer io.WriteCloser),
|
||||
write func(...interface{})) {
|
||||
func doTestStructedLog(t *testing.T, level string, w *mockWriter, write func(...interface{})) {
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
setup(writer)
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
write(message)
|
||||
var entry logEntry
|
||||
if err := json.Unmarshal([]byte(writer.builder.String()), &entry); err != nil {
|
||||
|
||||
var entry map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(w.String()), &entry); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
assert.Equal(t, level, entry.Level)
|
||||
val, ok := entry.Content.(string)
|
||||
|
||||
assert.Equal(t, level, entry[levelKey])
|
||||
val, ok := entry[contentKey]
|
||||
assert.True(t, ok)
|
||||
assert.True(t, strings.Contains(val, message))
|
||||
assert.True(t, strings.Contains(val.(string), message))
|
||||
}
|
||||
|
||||
func doTestStructedLogConsole(t *testing.T, setup func(writer io.WriteCloser),
|
||||
write func(...interface{})) {
|
||||
func doTestStructedLogConsole(t *testing.T, w *mockWriter, write func(...interface{})) {
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
setup(writer)
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
write(message)
|
||||
println(writer.String())
|
||||
assert.True(t, strings.Contains(writer.String(), message))
|
||||
assert.True(t, strings.Contains(w.String(), message))
|
||||
}
|
||||
|
||||
func testSetLevelTwiceWithMode(t *testing.T, mode string) {
|
||||
func testSetLevelTwiceWithMode(t *testing.T, mode string, w *mockWriter) {
|
||||
writer.Store(nil)
|
||||
SetUp(LogConf{
|
||||
Mode: mode,
|
||||
Level: "error",
|
||||
Path: "/dev/null",
|
||||
Mode: mode,
|
||||
Level: "debug",
|
||||
Path: "/dev/null",
|
||||
Encoding: plainEncoding,
|
||||
Stat: false,
|
||||
TimeFormat: time.RFC3339,
|
||||
})
|
||||
SetUp(LogConf{
|
||||
Mode: mode,
|
||||
@@ -527,17 +804,14 @@ func testSetLevelTwiceWithMode(t *testing.T, mode string) {
|
||||
Path: "/dev/null",
|
||||
})
|
||||
const message = "hello there"
|
||||
writer := new(mockWriter)
|
||||
infoLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
Info(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
assert.Equal(t, 0, w.builder.Len())
|
||||
Infof(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
assert.Equal(t, 0, w.builder.Len())
|
||||
ErrorStack(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
assert.Equal(t, 0, w.builder.Len())
|
||||
ErrorStackf(message)
|
||||
assert.Equal(t, 0, writer.builder.Len())
|
||||
assert.Equal(t, 0, w.builder.Len())
|
||||
}
|
||||
|
||||
type ValStringer struct {
|
||||
@@ -547,3 +821,18 @@ type ValStringer struct {
|
||||
func (v ValStringer) String() string {
|
||||
return v.val
|
||||
}
|
||||
|
||||
func validateFields(t *testing.T, content string, fields map[string]interface{}) {
|
||||
var m map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(content), &m); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
for k, v := range fields {
|
||||
if reflect.TypeOf(v).Kind() == reflect.Slice {
|
||||
assert.EqualValues(t, v, m[k])
|
||||
} else {
|
||||
assert.Equal(t, v, m[k], content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
22
core/logx/logwriter.go
Normal file
22
core/logx/logwriter.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package logx
|
||||
|
||||
import "log"
|
||||
|
||||
type logWriter struct {
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func newLogWriter(logger *log.Logger) logWriter {
|
||||
return logWriter{
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (lw logWriter) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lw logWriter) Write(data []byte) (int, error) {
|
||||
lw.logger.Print(string(data))
|
||||
return len(data), nil
|
||||
}
|
||||
206
core/logx/readme-cn.md
Normal file
206
core/logx/readme-cn.md
Normal file
@@ -0,0 +1,206 @@
|
||||
<IMG align="right" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png">
|
||||
|
||||
# logx
|
||||
|
||||
[English](readme.md) | 简体中文
|
||||
|
||||
## logx 配置
|
||||
|
||||
```go
|
||||
type LogConf struct {
|
||||
ServiceName string `json:",optional"`
|
||||
Mode string `json:",default=console,options=[console,file,volume]"`
|
||||
Encoding string `json:",default=json,options=[json,plain]"`
|
||||
TimeFormat string `json:",optional"`
|
||||
Path string `json:",default=logs"`
|
||||
Level string `json:",default=info,options=[info,error,severe]"`
|
||||
Compress bool `json:",optional"`
|
||||
KeepDays int `json:",optional"`
|
||||
StackCooldownMillis int `json:",default=100"`
|
||||
MaxBackups int `json:",default=0"`
|
||||
MaxSize int `json:",default=0"`
|
||||
Rotation string `json:",default=daily,options=[daily,size]"`
|
||||
}
|
||||
```
|
||||
|
||||
- `ServiceName`:设置服务名称,可选。在 `volume` 模式下,该名称用于生成日志文件。在 `rest/zrpc` 服务中,名称将被自动设置为 `rest`或`zrpc` 的名称。
|
||||
- `Mode`:输出日志的模式,默认是 `console`
|
||||
- `console` 模式将日志写到 `stdout/stderr`
|
||||
- `file` 模式将日志写到 `Path` 指定目录的文件中
|
||||
- `volume` 模式在 docker 中使用,将日志写入挂载的卷中
|
||||
- `Encoding`: 指示如何对日志进行编码,默认是 `json`
|
||||
- `json`模式以 json 格式写日志
|
||||
- `plain`模式用纯文本写日志,并带有终端颜色显示
|
||||
- `TimeFormat`:自定义时间格式,可选。默认是 `2006-01-02T15:04:05.000Z07:00`
|
||||
- `Path`:设置日志路径,默认为 `logs`
|
||||
- `Level`: 用于过滤日志的日志级别。默认为 `info`
|
||||
- `info`,所有日志都被写入
|
||||
- `error`, `info` 的日志被丢弃
|
||||
- `severe`, `info` 和 `error` 日志被丢弃,只有 `severe` 日志被写入
|
||||
- `Compress`: 是否压缩日志文件,只在 `file` 模式下工作
|
||||
- `KeepDays`:日志文件被保留多少天,在给定的天数之后,过期的文件将被自动删除。对 `console` 模式没有影响
|
||||
- `StackCooldownMillis`:多少毫秒后再次写入堆栈跟踪。用来避免堆栈跟踪日志过多
|
||||
- `MaxBackups`: 多少个日志文件备份将被保存。0代表所有备份都被保存。当`Rotation`被设置为`size`时才会起作用。注意:`KeepDays`选项的优先级会比`MaxBackups`高,即使`MaxBackups`被设置为0,当达到`KeepDays`上限时备份文件同样会被删除。
|
||||
- `MaxSize`: 当前被写入的日志文件最大可占用多少空间。0代表没有上限。单位为`MB`。当`Rotation`被设置为`size`时才会起作用。
|
||||
- `Rotation`: 日志轮转策略类型。默认为`daily`(按天轮转)。
|
||||
- `daily` 按天轮转。
|
||||
- `size` 按日志大小轮转。
|
||||
|
||||
|
||||
## 打印日志方法
|
||||
|
||||
```go
|
||||
type Logger interface {
|
||||
// Error logs a message at error level.
|
||||
Error(...interface{})
|
||||
// Errorf logs a message at error level.
|
||||
Errorf(string, ...interface{})
|
||||
// Errorv logs a message at error level.
|
||||
Errorv(interface{})
|
||||
// Errorw logs a message at error level.
|
||||
Errorw(string, ...LogField)
|
||||
// Info logs a message at info level.
|
||||
Info(...interface{})
|
||||
// Infof logs a message at info level.
|
||||
Infof(string, ...interface{})
|
||||
// Infov logs a message at info level.
|
||||
Infov(interface{})
|
||||
// Infow logs a message at info level.
|
||||
Infow(string, ...LogField)
|
||||
// Slow logs a message at slow level.
|
||||
Slow(...interface{})
|
||||
// Slowf logs a message at slow level.
|
||||
Slowf(string, ...interface{})
|
||||
// Slowv logs a message at slow level.
|
||||
Slowv(interface{})
|
||||
// Sloww logs a message at slow level.
|
||||
Sloww(string, ...LogField)
|
||||
// WithContext returns a new logger with the given context.
|
||||
WithContext(context.Context) Logger
|
||||
// WithDuration returns a new logger with the given duration.
|
||||
WithDuration(time.Duration) Logger
|
||||
}
|
||||
```
|
||||
|
||||
- `Error`, `Info`, `Slow`: 将任何类型的信息写进日志,使用 `fmt.Sprint(...)` 来转换为 `string`
|
||||
- `Errorf`, `Infof`, `Slowf`: 将指定格式的信息写入日志
|
||||
- `Errorv`, `Infov`, `Slowv`: 将任何类型的信息写入日志,用 `json marshal` 编码
|
||||
- `Errorw`, `Infow`, `Sloww`: 写日志,并带上给定的 `key:value` 字段
|
||||
- `WithContext`:将给定的 ctx 注入日志信息,例如用于记录 `trace-id`和`span-id`
|
||||
- `WithDuration`: 将指定的时间写入日志信息中,字段名为 `duration`
|
||||
|
||||
## 与第三方日志库集成
|
||||
|
||||
- zap
|
||||
- 实现:[https://github.com/zeromicro/zero-contrib/blob/main/logx/zapx/zap.go](https://github.com/zeromicro/zero-contrib/blob/main/logx/zapx/zap.go)
|
||||
- 使用示例:[https://github.com/zeromicro/zero-examples/blob/main/logx/zaplog/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/zaplog/main.go)
|
||||
- logrus
|
||||
- 实现:[https://github.com/zeromicro/zero-contrib/blob/main/logx/logrusx/logrus.go](https://github.com/zeromicro/zero-contrib/blob/main/logx/logrusx/logrus.go)
|
||||
- 使用示例:[https://github.com/zeromicro/zero-examples/blob/main/logx/logrus/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/logrus/main.go)
|
||||
|
||||
对于其它的日志库,请参考上面示例实现,并欢迎提交 `PR` 到 [https://github.com/zeromicro/zero-contrib](https://github.com/zeromicro/zero-contrib)
|
||||
|
||||
## 将日志写到指定的存储
|
||||
|
||||
`logx`定义了两个接口,方便自定义 `logx`,将日志写入任何存储。
|
||||
|
||||
- `logx.NewWriter(w io.Writer)`
|
||||
- `logx.SetWriter(write logx.Writer)`
|
||||
|
||||
例如,如果我们想把日志写进kafka,而不是控制台或文件,我们可以像下面这样做。
|
||||
|
||||
```go
|
||||
type KafkaWriter struct {
|
||||
Pusher *kq.Pusher
|
||||
}
|
||||
|
||||
func NewKafkaWriter(pusher *kq.Pusher) *KafkaWriter {
|
||||
return &KafkaWriter{
|
||||
Pusher: pusher,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *KafkaWriter) Write(p []byte) (n int, err error) {
|
||||
// writing log with newlines, trim them.
|
||||
if err := w.Pusher.Push(strings.TrimSpace(string(p))); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
pusher := kq.NewPusher([]string{"localhost:9092"}, "go-zero")
|
||||
defer pusher.Close()
|
||||
|
||||
writer := logx.NewWriter(NewKafkaWriter(pusher))
|
||||
logx.SetWriter(writer)
|
||||
|
||||
// more code
|
||||
}
|
||||
```
|
||||
|
||||
完整代码:[https://github.com/zeromicro/zero-examples/blob/main/logx/tokafka/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/tokafka/main.go)
|
||||
|
||||
## 过滤敏感字段
|
||||
|
||||
如果我们需要防止 `password` 字段被记录下来,我们可以像下面这样实现。
|
||||
|
||||
```go
|
||||
type (
|
||||
Message struct {
|
||||
Name string
|
||||
Password string
|
||||
Message string
|
||||
}
|
||||
|
||||
SensitiveLogger struct {
|
||||
logx.Writer
|
||||
}
|
||||
)
|
||||
|
||||
func NewSensitiveLogger(writer logx.Writer) *SensitiveLogger {
|
||||
return &SensitiveLogger{
|
||||
Writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SensitiveLogger) Info(msg interface{}, fields ...logx.LogField) {
|
||||
if m, ok := msg.(Message); ok {
|
||||
l.Writer.Info(Message{
|
||||
Name: m.Name,
|
||||
Password: "******",
|
||||
Message: m.Message,
|
||||
}, fields...)
|
||||
} else {
|
||||
l.Writer.Info(msg, fields...)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// setup logx to make sure originalWriter not nil,
|
||||
// the injected writer is only for filtering, like a middleware.
|
||||
|
||||
originalWriter := logx.Reset()
|
||||
writer := NewSensitiveLogger(originalWriter)
|
||||
logx.SetWriter(writer)
|
||||
|
||||
logx.Infov(Message{
|
||||
Name: "foo",
|
||||
Password: "shouldNotAppear",
|
||||
Message: "bar",
|
||||
})
|
||||
|
||||
// more code
|
||||
}
|
||||
```
|
||||
|
||||
完整代码:[https://github.com/zeromicro/zero-examples/blob/main/logx/filterfields/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/filterfields/main.go)
|
||||
|
||||
## 更多示例
|
||||
|
||||
[https://github.com/zeromicro/zero-examples/tree/main/logx](https://github.com/zeromicro/zero-examples/tree/main/logx)
|
||||
|
||||
## Give a Star! ⭐
|
||||
|
||||
如果你正在使用或者觉得这个项目对你有帮助,请 **star** 支持,感谢!
|
||||
205
core/logx/readme.md
Normal file
205
core/logx/readme.md
Normal file
@@ -0,0 +1,205 @@
|
||||
<img align="right" width="150px" src="https://raw.githubusercontent.com/zeromicro/zero-doc/main/doc/images/go-zero.png">
|
||||
|
||||
# logx
|
||||
|
||||
English | [简体中文](readme-cn.md)
|
||||
|
||||
## logx configurations
|
||||
|
||||
```go
|
||||
type LogConf struct {
|
||||
ServiceName string `json:",optional"`
|
||||
Mode string `json:",default=console,options=[console,file,volume]"`
|
||||
Encoding string `json:",default=json,options=[json,plain]"`
|
||||
TimeFormat string `json:",optional"`
|
||||
Path string `json:",default=logs"`
|
||||
Level string `json:",default=info,options=[info,error,severe]"`
|
||||
Compress bool `json:",optional"`
|
||||
KeepDays int `json:",optional"`
|
||||
StackCooldownMillis int `json:",default=100"`
|
||||
MaxBackups int `json:",default=0"`
|
||||
MaxSize int `json:",default=0"`
|
||||
Rotation string `json:",default=daily,options=[daily,size]"`
|
||||
}
|
||||
```
|
||||
|
||||
- `ServiceName`: set the service name, optional. on `volume` mode, the name is used to generate the log files. Within `rest/zrpc` services, the name will be set to the name of `rest` or `zrpc` automatically.
|
||||
- `Mode`: the mode to output the logs, default is `console`.
|
||||
- `console` mode writes the logs to `stdout/stderr`.
|
||||
- `file` mode writes the logs to the files specified by `Path`.
|
||||
- `volume` mode is used in docker, to write logs into mounted volumes.
|
||||
- `Encoding`: indicates how to encode the logs, default is `json`.
|
||||
- `json` mode writes the logs in json format.
|
||||
- `plain` mode writes the logs with plain text, with terminal color enabled.
|
||||
- `TimeFormat`: customize the time format, optional. Default is `2006-01-02T15:04:05.000Z07:00`.
|
||||
- `Path`: set the log path, default to `logs`.
|
||||
- `Level`: the logging level to filter logs. Default is `info`.
|
||||
- `info`, all logs are written.
|
||||
- `error`, `info` logs are suppressed.
|
||||
- `severe`, `info` and `error` logs are suppressed, only `severe` logs are written.
|
||||
- `Compress`: whether or not to compress log files, only works with `file` mode.
|
||||
- `KeepDays`: how many days that the log files are kept, after the given days, the outdated files will be deleted automatically. It has no effect on `console` mode.
|
||||
- `StackCooldownMillis`: how many milliseconds to rewrite stacktrace again. It’s used to avoid stacktrace flooding.
|
||||
- `MaxBackups`: represents how many backup log files will be kept. 0 means all files will be kept forever. Only take effect when `Rotation` is `size`. NOTE: the level of option `KeepDays` will be higher. Even thougth `MaxBackups` sets 0, log files will still be removed if the `KeepDays` limitation is reached.
|
||||
- `MaxSize`: represents how much space the writing log file takes up. 0 means no limit. The unit is `MB`. Only take effect when `Rotation` is `size`.
|
||||
- `Rotation`: represents the type of log rotation rule. Default is `daily`.
|
||||
- `daily` rotate the logs by day.
|
||||
- `size` rotate the logs by size of logs.
|
||||
|
||||
## Logging methods
|
||||
|
||||
```go
|
||||
type Logger interface {
|
||||
// Error logs a message at error level.
|
||||
Error(...interface{})
|
||||
// Errorf logs a message at error level.
|
||||
Errorf(string, ...interface{})
|
||||
// Errorv logs a message at error level.
|
||||
Errorv(interface{})
|
||||
// Errorw logs a message at error level.
|
||||
Errorw(string, ...LogField)
|
||||
// Info logs a message at info level.
|
||||
Info(...interface{})
|
||||
// Infof logs a message at info level.
|
||||
Infof(string, ...interface{})
|
||||
// Infov logs a message at info level.
|
||||
Infov(interface{})
|
||||
// Infow logs a message at info level.
|
||||
Infow(string, ...LogField)
|
||||
// Slow logs a message at slow level.
|
||||
Slow(...interface{})
|
||||
// Slowf logs a message at slow level.
|
||||
Slowf(string, ...interface{})
|
||||
// Slowv logs a message at slow level.
|
||||
Slowv(interface{})
|
||||
// Sloww logs a message at slow level.
|
||||
Sloww(string, ...LogField)
|
||||
// WithContext returns a new logger with the given context.
|
||||
WithContext(context.Context) Logger
|
||||
// WithDuration returns a new logger with the given duration.
|
||||
WithDuration(time.Duration) Logger
|
||||
}
|
||||
```
|
||||
|
||||
- `Error`, `Info`, `Slow`: write any kind of messages into logs, with like `fmt.Sprint(…)`.
|
||||
- `Errorf`, `Infof`, `Slowf`: write messages with given format into logs.
|
||||
- `Errorv`, `Infov`, `Slowv`: write any kind of messages into logs, with json marshalling to encode them.
|
||||
- `Errorw`, `Infow`, `Sloww`: write the string message with given `key:value` fields.
|
||||
- `WithContext`: inject the given ctx into the log messages, typically used to log `trace-id` and `span-id`.
|
||||
- `WithDuration`: write elapsed duration into the log messages, with key `duration`.
|
||||
|
||||
## Integrating with third-party logging libs
|
||||
|
||||
- zap
|
||||
- implementation: [https://github.com/zeromicro/zero-contrib/blob/main/logx/zapx/zap.go](https://github.com/zeromicro/zero-contrib/blob/main/logx/zapx/zap.go)
|
||||
- usage example: [https://github.com/zeromicro/zero-examples/blob/main/logx/zaplog/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/zaplog/main.go)
|
||||
- logrus
|
||||
- implementation: [https://github.com/zeromicro/zero-contrib/blob/main/logx/logrusx/logrus.go](https://github.com/zeromicro/zero-contrib/blob/main/logx/logrusx/logrus.go)
|
||||
- usage example: [https://github.com/zeromicro/zero-examples/blob/main/logx/logrus/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/logrus/main.go)
|
||||
|
||||
For more libs, please implement and PR to [https://github.com/zeromicro/zero-contrib](https://github.com/zeromicro/zero-contrib)
|
||||
|
||||
## Write the logs to specific stores
|
||||
|
||||
`logx` defined two interfaces to let you customize `logx` to write logs into any stores.
|
||||
|
||||
- `logx.NewWriter(w io.Writer)`
|
||||
- `logx.SetWriter(writer logx.Writer)`
|
||||
|
||||
For example, if we want to write the logs into kafka instead of console or files, we can do it like below:
|
||||
|
||||
```go
|
||||
type KafkaWriter struct {
|
||||
Pusher *kq.Pusher
|
||||
}
|
||||
|
||||
func NewKafkaWriter(pusher *kq.Pusher) *KafkaWriter {
|
||||
return &KafkaWriter{
|
||||
Pusher: pusher,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *KafkaWriter) Write(p []byte) (n int, err error) {
|
||||
// writing log with newlines, trim them.
|
||||
if err := w.Pusher.Push(strings.TrimSpace(string(p))); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
pusher := kq.NewPusher([]string{"localhost:9092"}, "go-zero")
|
||||
defer pusher.Close()
|
||||
|
||||
writer := logx.NewWriter(NewKafkaWriter(pusher))
|
||||
logx.SetWriter(writer)
|
||||
|
||||
// more code
|
||||
}
|
||||
```
|
||||
|
||||
Complete code: [https://github.com/zeromicro/zero-examples/blob/main/logx/tokafka/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/tokafka/main.go)
|
||||
|
||||
## Filtering sensitive fields
|
||||
|
||||
If we need to prevent the `password` fields from logging, we can do it like below:
|
||||
|
||||
```go
|
||||
type (
|
||||
Message struct {
|
||||
Name string
|
||||
Password string
|
||||
Message string
|
||||
}
|
||||
|
||||
SensitiveLogger struct {
|
||||
logx.Writer
|
||||
}
|
||||
)
|
||||
|
||||
func NewSensitiveLogger(writer logx.Writer) *SensitiveLogger {
|
||||
return &SensitiveLogger{
|
||||
Writer: writer,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SensitiveLogger) Info(msg interface{}, fields ...logx.LogField) {
|
||||
if m, ok := msg.(Message); ok {
|
||||
l.Writer.Info(Message{
|
||||
Name: m.Name,
|
||||
Password: "******",
|
||||
Message: m.Message,
|
||||
}, fields...)
|
||||
} else {
|
||||
l.Writer.Info(msg, fields...)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// setup logx to make sure originalWriter not nil,
|
||||
// the injected writer is only for filtering, like a middleware.
|
||||
|
||||
originalWriter := logx.Reset()
|
||||
writer := NewSensitiveLogger(originalWriter)
|
||||
logx.SetWriter(writer)
|
||||
|
||||
logx.Infov(Message{
|
||||
Name: "foo",
|
||||
Password: "shouldNotAppear",
|
||||
Message: "bar",
|
||||
})
|
||||
|
||||
// more code
|
||||
}
|
||||
```
|
||||
|
||||
Complete code: [https://github.com/zeromicro/zero-examples/blob/main/logx/filterfields/main.go](https://github.com/zeromicro/zero-examples/blob/main/logx/filterfields/main.go)
|
||||
|
||||
## More examples
|
||||
|
||||
[https://github.com/zeromicro/zero-examples/tree/main/logx](https://github.com/zeromicro/zero-examples/tree/main/logx)
|
||||
|
||||
## Give a Star! ⭐
|
||||
|
||||
If you like or are using this project to learn or start your solution, please give it a star. Thanks!
|
||||
181
core/logx/richlogger.go
Normal file
181
core/logx/richlogger.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
"github.com/zeromicro/go-zero/internal/trace"
|
||||
)
|
||||
|
||||
// WithCallerSkip returns a Logger with given caller skip.
|
||||
func WithCallerSkip(skip int) Logger {
|
||||
if skip <= 0 {
|
||||
return new(richLogger)
|
||||
}
|
||||
|
||||
return &richLogger{
|
||||
callerSkip: skip,
|
||||
}
|
||||
}
|
||||
|
||||
// WithContext sets ctx to log, for keeping tracing information.
|
||||
func WithContext(ctx context.Context) Logger {
|
||||
return &richLogger{
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
// WithDuration returns a Logger with given duration.
|
||||
func WithDuration(d time.Duration) Logger {
|
||||
return &richLogger{
|
||||
fields: []LogField{Field(durationKey, timex.ReprOfDuration(d))},
|
||||
}
|
||||
}
|
||||
|
||||
type richLogger struct {
|
||||
ctx context.Context
|
||||
callerSkip int
|
||||
fields []LogField
|
||||
}
|
||||
|
||||
func (l *richLogger) Debug(v ...interface{}) {
|
||||
l.debug(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Debugf(format string, v ...interface{}) {
|
||||
l.debug(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Debugv(v interface{}) {
|
||||
l.debug(v)
|
||||
}
|
||||
|
||||
func (l *richLogger) Debugw(msg string, fields ...LogField) {
|
||||
l.debug(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *richLogger) Error(v ...interface{}) {
|
||||
l.err(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Errorf(format string, v ...interface{}) {
|
||||
l.err(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Errorv(v interface{}) {
|
||||
l.err(fmt.Sprint(v))
|
||||
}
|
||||
|
||||
func (l *richLogger) Errorw(msg string, fields ...LogField) {
|
||||
l.err(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *richLogger) Info(v ...interface{}) {
|
||||
l.info(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Infof(format string, v ...interface{}) {
|
||||
l.info(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Infov(v interface{}) {
|
||||
l.info(v)
|
||||
}
|
||||
|
||||
func (l *richLogger) Infow(msg string, fields ...LogField) {
|
||||
l.info(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *richLogger) Slow(v ...interface{}) {
|
||||
l.slow(fmt.Sprint(v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Slowf(format string, v ...interface{}) {
|
||||
l.slow(fmt.Sprintf(format, v...))
|
||||
}
|
||||
|
||||
func (l *richLogger) Slowv(v interface{}) {
|
||||
l.slow(v)
|
||||
}
|
||||
|
||||
func (l *richLogger) Sloww(msg string, fields ...LogField) {
|
||||
l.slow(msg, fields...)
|
||||
}
|
||||
|
||||
func (l *richLogger) WithCallerSkip(skip int) Logger {
|
||||
if skip <= 0 {
|
||||
return l
|
||||
}
|
||||
|
||||
l.callerSkip = skip
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *richLogger) WithContext(ctx context.Context) Logger {
|
||||
l.ctx = ctx
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *richLogger) WithDuration(duration time.Duration) Logger {
|
||||
l.fields = append(l.fields, Field(durationKey, timex.ReprOfDuration(duration)))
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *richLogger) WithFields(fields ...LogField) Logger {
|
||||
l.fields = append(l.fields, fields...)
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *richLogger) buildFields(fields ...LogField) []LogField {
|
||||
fields = append(l.fields, fields...)
|
||||
fields = append(fields, Field(callerKey, getCaller(callerDepth+l.callerSkip)))
|
||||
|
||||
if l.ctx == nil {
|
||||
return fields
|
||||
}
|
||||
|
||||
traceID := trace.TraceIDFromContext(l.ctx)
|
||||
if len(traceID) > 0 {
|
||||
fields = append(fields, Field(traceKey, traceID))
|
||||
}
|
||||
|
||||
spanID := trace.SpanIDFromContext(l.ctx)
|
||||
if len(spanID) > 0 {
|
||||
fields = append(fields, Field(spanKey, spanID))
|
||||
}
|
||||
|
||||
val := l.ctx.Value(fieldsContextKey)
|
||||
if val != nil {
|
||||
if arr, ok := val.([]LogField); ok {
|
||||
fields = append(fields, arr...)
|
||||
}
|
||||
}
|
||||
|
||||
return fields
|
||||
}
|
||||
|
||||
func (l *richLogger) debug(v interface{}, fields ...LogField) {
|
||||
if shallLog(DebugLevel) {
|
||||
getWriter().Debug(v, l.buildFields(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *richLogger) err(v interface{}, fields ...LogField) {
|
||||
if shallLog(ErrorLevel) {
|
||||
getWriter().Error(v, l.buildFields(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *richLogger) info(v interface{}, fields ...LogField) {
|
||||
if shallLog(InfoLevel) {
|
||||
getWriter().Info(v, l.buildFields(fields...)...)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *richLogger) slow(v interface{}, fields ...LogField) {
|
||||
if shallLog(ErrorLevel) {
|
||||
getWriter().Slow(v, l.buildFields(fields...)...)
|
||||
}
|
||||
}
|
||||
318
core/logx/richlogger_test.go
Normal file
318
core/logx/richlogger_test.go
Normal file
@@ -0,0 +1,318 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.opentelemetry.io/otel"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
func TestTraceLog(t *testing.T) {
|
||||
SetLevel(InfoLevel)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||||
defer span.End()
|
||||
|
||||
WithContext(ctx).Info(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
}
|
||||
|
||||
func TestTraceDebug(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, span := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
defer span.End()
|
||||
|
||||
l := WithContext(ctx)
|
||||
SetLevel(DebugLevel)
|
||||
l.WithDuration(time.Second).Debug(testlog)
|
||||
assert.True(t, strings.Contains(w.String(), traceKey))
|
||||
assert.True(t, strings.Contains(w.String(), spanKey))
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Debugf(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Debugv(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Debugw(testlog, Field("foo", "bar"))
|
||||
validate(t, w.String(), true, true)
|
||||
assert.True(t, strings.Contains(w.String(), "foo"), w.String())
|
||||
assert.True(t, strings.Contains(w.String(), "bar"), w.String())
|
||||
}
|
||||
|
||||
func TestTraceError(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||||
defer span.End()
|
||||
|
||||
var nilCtx context.Context
|
||||
l := WithContext(context.Background())
|
||||
l = l.WithContext(nilCtx)
|
||||
l = l.WithContext(ctx)
|
||||
SetLevel(ErrorLevel)
|
||||
l.WithDuration(time.Second).Error(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Errorf(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Errorv(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Errorw(testlog, Field("basket", "ball"))
|
||||
validate(t, w.String(), true, true)
|
||||
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||||
assert.True(t, strings.Contains(w.String(), "ball"), w.String())
|
||||
}
|
||||
|
||||
func TestTraceInfo(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||||
defer span.End()
|
||||
|
||||
SetLevel(InfoLevel)
|
||||
l := WithContext(ctx)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Infov(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Infow(testlog, Field("basket", "ball"))
|
||||
validate(t, w.String(), true, true)
|
||||
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||||
assert.True(t, strings.Contains(w.String(), "ball"), w.String())
|
||||
}
|
||||
|
||||
func TestTraceInfoConsole(t *testing.T) {
|
||||
old := atomic.SwapUint32(&encoding, jsonEncodingType)
|
||||
defer atomic.StoreUint32(&encoding, old)
|
||||
|
||||
w := new(mockWriter)
|
||||
o := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(o)
|
||||
}()
|
||||
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||||
defer span.End()
|
||||
|
||||
l := WithContext(ctx)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Infov(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
}
|
||||
|
||||
func TestTraceSlow(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, span := tp.Tracer("trace-id").Start(context.Background(), "span-id")
|
||||
defer span.End()
|
||||
|
||||
l := WithContext(ctx)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Slow(testlog)
|
||||
assert.True(t, strings.Contains(w.String(), traceKey))
|
||||
assert.True(t, strings.Contains(w.String(), spanKey))
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Slowf(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Slowv(testlog)
|
||||
validate(t, w.String(), true, true)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Sloww(testlog, Field("basket", "ball"))
|
||||
validate(t, w.String(), true, true)
|
||||
assert.True(t, strings.Contains(w.String(), "basket"), w.String())
|
||||
assert.True(t, strings.Contains(w.String(), "ball"), w.String())
|
||||
}
|
||||
|
||||
func TestTraceWithoutContext(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
l := WithContext(context.Background())
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
validate(t, w.String(), false, false)
|
||||
w.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
validate(t, w.String(), false, false)
|
||||
}
|
||||
|
||||
func TestLogWithFields(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
ctx := ContextWithFields(context.Background(), Field("foo", "bar"))
|
||||
l := WithContext(ctx)
|
||||
SetLevel(InfoLevel)
|
||||
l.Info(testlog)
|
||||
|
||||
var val mockValue
|
||||
assert.Nil(t, json.Unmarshal([]byte(w.String()), &val))
|
||||
assert.Equal(t, "bar", val.Foo)
|
||||
}
|
||||
|
||||
func TestLogWithCallerSkip(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
l := WithCallerSkip(1).WithCallerSkip(0)
|
||||
p := func(v string) {
|
||||
l.Info(v)
|
||||
}
|
||||
|
||||
file, line := getFileLine()
|
||||
p(testlog)
|
||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
|
||||
w.Reset()
|
||||
l = WithCallerSkip(0).WithCallerSkip(1)
|
||||
file, line = getFileLine()
|
||||
p(testlog)
|
||||
assert.True(t, w.Contains(fmt.Sprintf("%s:%d", file, line+1)))
|
||||
}
|
||||
|
||||
func TestLoggerWithFields(t *testing.T) {
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
writer.lock.RLock()
|
||||
defer func() {
|
||||
writer.lock.RUnlock()
|
||||
writer.Store(old)
|
||||
}()
|
||||
|
||||
l := WithContext(context.Background()).WithFields(Field("foo", "bar"))
|
||||
l.Info(testlog)
|
||||
|
||||
var val mockValue
|
||||
assert.Nil(t, json.Unmarshal([]byte(w.String()), &val))
|
||||
assert.Equal(t, "bar", val.Foo)
|
||||
}
|
||||
|
||||
func validate(t *testing.T, body string, expectedTrace, expectedSpan bool) {
|
||||
var val mockValue
|
||||
dec := json.NewDecoder(strings.NewReader(body))
|
||||
|
||||
for {
|
||||
var doc mockValue
|
||||
err := dec.Decode(&doc)
|
||||
if err == io.EOF {
|
||||
// all done
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
val = doc
|
||||
}
|
||||
|
||||
assert.Equal(t, expectedTrace, len(val.Trace) > 0, body)
|
||||
assert.Equal(t, expectedSpan, len(val.Span) > 0, body)
|
||||
}
|
||||
|
||||
type mockValue struct {
|
||||
Trace string `json:"trace"`
|
||||
Span string `json:"span"`
|
||||
Foo string `json:"foo"`
|
||||
}
|
||||
@@ -9,21 +9,24 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/fs"
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
)
|
||||
|
||||
const (
|
||||
dateFormat = "2006-01-02"
|
||||
fileTimeFormat = time.RFC3339
|
||||
hoursPerDay = 24
|
||||
bufferSize = 100
|
||||
defaultDirMode = 0o755
|
||||
defaultFileMode = 0o600
|
||||
gzipExt = ".gz"
|
||||
megaBytes = 1 << 20
|
||||
)
|
||||
|
||||
// ErrLogFileClosed is an error that indicates the log file is already closed.
|
||||
@@ -35,7 +38,7 @@ type (
|
||||
BackupFileName() string
|
||||
MarkRotated()
|
||||
OutdatedFiles() []string
|
||||
ShallRotate() bool
|
||||
ShallRotate(size int64) bool
|
||||
}
|
||||
|
||||
// A RotateLogger is a Logger that can rotate log files with given rules.
|
||||
@@ -48,8 +51,9 @@ type (
|
||||
rule RotateRule
|
||||
compress bool
|
||||
// can't use threading.RoutineGroup because of cycle import
|
||||
waitGroup sync.WaitGroup
|
||||
closeOnce sync.Once
|
||||
waitGroup sync.WaitGroup
|
||||
closeOnce sync.Once
|
||||
currentSize int64
|
||||
}
|
||||
|
||||
// A DailyRotateRule is a rule to daily rotate the log files.
|
||||
@@ -60,6 +64,13 @@ type (
|
||||
days int
|
||||
gzip bool
|
||||
}
|
||||
|
||||
// SizeLimitRotateRule a rotation rule that make the log file rotated base on size
|
||||
SizeLimitRotateRule struct {
|
||||
DailyRotateRule
|
||||
maxSize int64
|
||||
maxBackups int
|
||||
}
|
||||
)
|
||||
|
||||
// DefaultRotateRule is a default log rotating rule, currently DailyRotateRule.
|
||||
@@ -91,7 +102,7 @@ func (r *DailyRotateRule) OutdatedFiles() []string {
|
||||
|
||||
var pattern string
|
||||
if r.gzip {
|
||||
pattern = fmt.Sprintf("%s%s*.gz", r.filename, r.delimiter)
|
||||
pattern = fmt.Sprintf("%s%s*%s", r.filename, r.delimiter, gzipExt)
|
||||
} else {
|
||||
pattern = fmt.Sprintf("%s%s*", r.filename, r.delimiter)
|
||||
}
|
||||
@@ -104,9 +115,11 @@ func (r *DailyRotateRule) OutdatedFiles() []string {
|
||||
|
||||
var buf strings.Builder
|
||||
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(dateFormat)
|
||||
fmt.Fprintf(&buf, "%s%s%s", r.filename, r.delimiter, boundary)
|
||||
buf.WriteString(r.filename)
|
||||
buf.WriteString(r.delimiter)
|
||||
buf.WriteString(boundary)
|
||||
if r.gzip {
|
||||
buf.WriteString(".gz")
|
||||
buf.WriteString(gzipExt)
|
||||
}
|
||||
boundaryFile := buf.String()
|
||||
|
||||
@@ -121,10 +134,100 @@ func (r *DailyRotateRule) OutdatedFiles() []string {
|
||||
}
|
||||
|
||||
// ShallRotate checks if the file should be rotated.
|
||||
func (r *DailyRotateRule) ShallRotate() bool {
|
||||
func (r *DailyRotateRule) ShallRotate(_ int64) bool {
|
||||
return len(r.rotatedTime) > 0 && getNowDate() != r.rotatedTime
|
||||
}
|
||||
|
||||
// NewSizeLimitRotateRule returns the rotation rule with size limit
|
||||
func NewSizeLimitRotateRule(filename, delimiter string, days, maxSize, maxBackups int, gzip bool) RotateRule {
|
||||
return &SizeLimitRotateRule{
|
||||
DailyRotateRule: DailyRotateRule{
|
||||
rotatedTime: getNowDateInRFC3339Format(),
|
||||
filename: filename,
|
||||
delimiter: delimiter,
|
||||
days: days,
|
||||
gzip: gzip,
|
||||
},
|
||||
maxSize: int64(maxSize) * megaBytes,
|
||||
maxBackups: maxBackups,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *SizeLimitRotateRule) BackupFileName() string {
|
||||
dir := filepath.Dir(r.filename)
|
||||
prefix, ext := r.parseFilename()
|
||||
timestamp := getNowDateInRFC3339Format()
|
||||
return filepath.Join(dir, fmt.Sprintf("%s%s%s%s", prefix, r.delimiter, timestamp, ext))
|
||||
}
|
||||
|
||||
func (r *SizeLimitRotateRule) MarkRotated() {
|
||||
r.rotatedTime = getNowDateInRFC3339Format()
|
||||
}
|
||||
|
||||
func (r *SizeLimitRotateRule) OutdatedFiles() []string {
|
||||
dir := filepath.Dir(r.filename)
|
||||
prefix, ext := r.parseFilename()
|
||||
|
||||
var pattern string
|
||||
if r.gzip {
|
||||
pattern = fmt.Sprintf("%s%s%s%s*%s%s", dir, string(filepath.Separator),
|
||||
prefix, r.delimiter, ext, gzipExt)
|
||||
} else {
|
||||
pattern = fmt.Sprintf("%s%s%s%s*%s", dir, string(filepath.Separator),
|
||||
prefix, r.delimiter, ext)
|
||||
}
|
||||
|
||||
files, err := filepath.Glob(pattern)
|
||||
if err != nil {
|
||||
Errorf("failed to delete outdated log files, error: %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
sort.Strings(files)
|
||||
|
||||
outdated := make(map[string]lang.PlaceholderType)
|
||||
|
||||
// test if too many backups
|
||||
if r.maxBackups > 0 && len(files) > r.maxBackups {
|
||||
for _, f := range files[:len(files)-r.maxBackups] {
|
||||
outdated[f] = lang.Placeholder
|
||||
}
|
||||
files = files[len(files)-r.maxBackups:]
|
||||
}
|
||||
|
||||
// test if any too old backups
|
||||
if r.days > 0 {
|
||||
boundary := time.Now().Add(-time.Hour * time.Duration(hoursPerDay*r.days)).Format(fileTimeFormat)
|
||||
boundaryFile := filepath.Join(dir, fmt.Sprintf("%s%s%s%s", prefix, r.delimiter, boundary, ext))
|
||||
if r.gzip {
|
||||
boundaryFile += gzipExt
|
||||
}
|
||||
for _, f := range files {
|
||||
if f >= boundaryFile {
|
||||
break
|
||||
}
|
||||
outdated[f] = lang.Placeholder
|
||||
}
|
||||
}
|
||||
|
||||
var result []string
|
||||
for k := range outdated {
|
||||
result = append(result, k)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (r *SizeLimitRotateRule) ShallRotate(size int64) bool {
|
||||
return r.maxSize > 0 && r.maxSize < size
|
||||
}
|
||||
|
||||
func (r *SizeLimitRotateRule) parseFilename() (prefix, ext string) {
|
||||
logName := filepath.Base(r.filename)
|
||||
ext = filepath.Ext(r.filename)
|
||||
prefix = logName[:len(logName)-len(ext)]
|
||||
return
|
||||
}
|
||||
|
||||
// NewLogger returns a RotateLogger with given filename and rule, etc.
|
||||
func NewLogger(filename string, rule RotateRule, compress bool) (*RotateLogger, error) {
|
||||
l := &RotateLogger{
|
||||
@@ -181,7 +284,7 @@ func (l *RotateLogger) getBackupFilename() string {
|
||||
func (l *RotateLogger) init() error {
|
||||
l.backup = l.rule.BackupFileName()
|
||||
|
||||
if _, err := os.Stat(l.filename); err != nil {
|
||||
if fileInfo, err := os.Stat(l.filename); err != nil {
|
||||
basePath := path.Dir(l.filename)
|
||||
if _, err = os.Stat(basePath); err != nil {
|
||||
if err = os.MkdirAll(basePath, defaultDirMode); err != nil {
|
||||
@@ -192,8 +295,11 @@ func (l *RotateLogger) init() error {
|
||||
if l.fp, err = os.Create(l.filename); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if l.fp, err = os.OpenFile(l.filename, os.O_APPEND|os.O_WRONLY, defaultFileMode); err != nil {
|
||||
return err
|
||||
} else {
|
||||
if l.fp, err = os.OpenFile(l.filename, os.O_APPEND|os.O_WRONLY, defaultFileMode); err != nil {
|
||||
return err
|
||||
}
|
||||
l.currentSize = fileInfo.Size()
|
||||
}
|
||||
|
||||
fs.CloseOnExec(l.fp)
|
||||
@@ -211,6 +317,12 @@ func (l *RotateLogger) maybeCompressFile(file string) {
|
||||
ErrorStack(r)
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := os.Stat(file); err != nil {
|
||||
// file not exists or other error, ignore compression
|
||||
return
|
||||
}
|
||||
|
||||
compressLogFile(file)
|
||||
}
|
||||
|
||||
@@ -277,25 +389,27 @@ func (l *RotateLogger) startWorker() {
|
||||
}
|
||||
|
||||
func (l *RotateLogger) write(v []byte) {
|
||||
if l.rule.ShallRotate() {
|
||||
if l.rule.ShallRotate(l.currentSize + int64(len(v))) {
|
||||
if err := l.rotate(); err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
l.rule.MarkRotated()
|
||||
l.currentSize = 0
|
||||
}
|
||||
}
|
||||
if l.fp != nil {
|
||||
l.fp.Write(v)
|
||||
l.currentSize += int64(len(v))
|
||||
}
|
||||
}
|
||||
|
||||
func compressLogFile(file string) {
|
||||
start := timex.Now()
|
||||
start := time.Now()
|
||||
Infof("compressing log file: %s", file)
|
||||
if err := gzipFile(file); err != nil {
|
||||
Errorf("compress error: %s", err)
|
||||
} else {
|
||||
Infof("compressed log file: %s, took %s", file, timex.Since(start))
|
||||
Infof("compressed log file: %s, took %s", file, time.Since(start))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -303,6 +417,10 @@ func getNowDate() string {
|
||||
return time.Now().Format(dateFormat)
|
||||
}
|
||||
|
||||
func getNowDateInRFC3339Format() string {
|
||||
return time.Now().Format(fileTimeFormat)
|
||||
}
|
||||
|
||||
func gzipFile(file string) error {
|
||||
in, err := os.Open(file)
|
||||
if err != nil {
|
||||
@@ -310,7 +428,7 @@ func gzipFile(file string) error {
|
||||
}
|
||||
defer in.Close()
|
||||
|
||||
out, err := os.Create(fmt.Sprintf("%s.gz", file))
|
||||
out, err := os.Create(fmt.Sprintf("%s%s", file, gzipExt))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -29,7 +29,34 @@ func TestDailyRotateRuleOutdatedFiles(t *testing.T) {
|
||||
func TestDailyRotateRuleShallRotate(t *testing.T) {
|
||||
var rule DailyRotateRule
|
||||
rule.rotatedTime = time.Now().Add(time.Hour * 24).Format(dateFormat)
|
||||
assert.True(t, rule.ShallRotate())
|
||||
assert.True(t, rule.ShallRotate(0))
|
||||
}
|
||||
|
||||
func TestSizeLimitRotateRuleMarkRotated(t *testing.T) {
|
||||
var rule SizeLimitRotateRule
|
||||
rule.MarkRotated()
|
||||
assert.Equal(t, getNowDateInRFC3339Format(), rule.rotatedTime)
|
||||
}
|
||||
|
||||
func TestSizeLimitRotateRuleOutdatedFiles(t *testing.T) {
|
||||
var rule SizeLimitRotateRule
|
||||
assert.Empty(t, rule.OutdatedFiles())
|
||||
rule.days = 1
|
||||
assert.Empty(t, rule.OutdatedFiles())
|
||||
rule.gzip = true
|
||||
assert.Empty(t, rule.OutdatedFiles())
|
||||
rule.maxBackups = 0
|
||||
assert.Empty(t, rule.OutdatedFiles())
|
||||
}
|
||||
|
||||
func TestSizeLimitRotateRuleShallRotate(t *testing.T) {
|
||||
var rule SizeLimitRotateRule
|
||||
rule.rotatedTime = time.Now().Add(time.Hour * 24).Format(fileTimeFormat)
|
||||
rule.maxSize = 0
|
||||
assert.False(t, rule.ShallRotate(0))
|
||||
rule.maxSize = 100
|
||||
assert.False(t, rule.ShallRotate(0))
|
||||
assert.True(t, rule.ShallRotate(101*megaBytes))
|
||||
}
|
||||
|
||||
func TestRotateLoggerClose(t *testing.T) {
|
||||
@@ -57,6 +84,12 @@ func TestRotateLoggerGetBackupFilename(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRotateLoggerMayCompressFile(t *testing.T) {
|
||||
old := os.Stdout
|
||||
os.Stdout = os.NewFile(0, os.DevNull)
|
||||
defer func() {
|
||||
os.Stdout = old
|
||||
}()
|
||||
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
@@ -70,15 +103,18 @@ func TestRotateLoggerMayCompressFile(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRotateLoggerMayCompressFileTrue(t *testing.T) {
|
||||
old := os.Stdout
|
||||
os.Stdout = os.NewFile(0, os.DevNull)
|
||||
defer func() {
|
||||
os.Stdout = old
|
||||
}()
|
||||
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
logger, err := NewLogger(filename, new(DailyRotateRule), true)
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer func() {
|
||||
os.Remove(filename)
|
||||
os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
|
||||
}()
|
||||
defer os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
|
||||
}
|
||||
logger.maybeCompressFile(filename)
|
||||
_, err = os.Stat(filename)
|
||||
@@ -92,7 +128,6 @@ func TestRotateLoggerRotate(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer func() {
|
||||
os.Remove(filename)
|
||||
os.Remove(logger.getBackupFilename())
|
||||
os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
|
||||
}()
|
||||
@@ -102,6 +137,10 @@ func TestRotateLoggerRotate(t *testing.T) {
|
||||
case *os.LinkError:
|
||||
// avoid rename error on docker container
|
||||
assert.Equal(t, syscall.EXDEV, v.Err)
|
||||
case *os.PathError:
|
||||
// ignore remove error for tests,
|
||||
// files are cleaned in GitHub actions.
|
||||
assert.Equal(t, "remove", v.Op)
|
||||
default:
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
@@ -115,12 +154,177 @@ func TestRotateLoggerWrite(t *testing.T) {
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer func() {
|
||||
os.Remove(filename)
|
||||
os.Remove(logger.getBackupFilename())
|
||||
os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
|
||||
}()
|
||||
}
|
||||
// the following write calls cannot be changed to Write, because of DATA RACE.
|
||||
logger.write([]byte(`foo`))
|
||||
rule.rotatedTime = time.Now().Add(-time.Hour * 24).Format(dateFormat)
|
||||
logger.write([]byte(`bar`))
|
||||
logger.Close()
|
||||
logger.write([]byte(`baz`))
|
||||
}
|
||||
|
||||
func TestLogWriterClose(t *testing.T) {
|
||||
assert.Nil(t, newLogWriter(nil).Close())
|
||||
}
|
||||
|
||||
func TestRotateLoggerWithSizeLimitRotateRuleClose(t *testing.T) {
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer os.Remove(filename)
|
||||
}
|
||||
logger, err := NewLogger(filename, new(SizeLimitRotateRule), false)
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, logger.Close())
|
||||
}
|
||||
|
||||
func TestRotateLoggerGetBackupWithSizeLimitRotateRuleFilename(t *testing.T) {
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer os.Remove(filename)
|
||||
}
|
||||
logger, err := NewLogger(filename, new(SizeLimitRotateRule), false)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, len(logger.getBackupFilename()) > 0)
|
||||
logger.backup = ""
|
||||
assert.True(t, len(logger.getBackupFilename()) > 0)
|
||||
}
|
||||
|
||||
func TestRotateLoggerWithSizeLimitRotateRuleMayCompressFile(t *testing.T) {
|
||||
old := os.Stdout
|
||||
os.Stdout = os.NewFile(0, os.DevNull)
|
||||
defer func() {
|
||||
os.Stdout = old
|
||||
}()
|
||||
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer os.Remove(filename)
|
||||
}
|
||||
logger, err := NewLogger(filename, new(SizeLimitRotateRule), false)
|
||||
assert.Nil(t, err)
|
||||
logger.maybeCompressFile(filename)
|
||||
_, err = os.Stat(filename)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestRotateLoggerWithSizeLimitRotateRuleMayCompressFileTrue(t *testing.T) {
|
||||
old := os.Stdout
|
||||
os.Stdout = os.NewFile(0, os.DevNull)
|
||||
defer func() {
|
||||
os.Stdout = old
|
||||
}()
|
||||
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
logger, err := NewLogger(filename, new(SizeLimitRotateRule), true)
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
|
||||
}
|
||||
logger.maybeCompressFile(filename)
|
||||
_, err = os.Stat(filename)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestRotateLoggerWithSizeLimitRotateRuleRotate(t *testing.T) {
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
logger, err := NewLogger(filename, new(SizeLimitRotateRule), true)
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer func() {
|
||||
os.Remove(logger.getBackupFilename())
|
||||
os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
|
||||
}()
|
||||
}
|
||||
err = logger.rotate()
|
||||
switch v := err.(type) {
|
||||
case *os.LinkError:
|
||||
// avoid rename error on docker container
|
||||
assert.Equal(t, syscall.EXDEV, v.Err)
|
||||
case *os.PathError:
|
||||
// ignore remove error for tests,
|
||||
// files are cleaned in GitHub actions.
|
||||
assert.Equal(t, "remove", v.Op)
|
||||
default:
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRotateLoggerWithSizeLimitRotateRuleWrite(t *testing.T) {
|
||||
filename, err := fs.TempFilenameWithText("foo")
|
||||
assert.Nil(t, err)
|
||||
rule := new(SizeLimitRotateRule)
|
||||
logger, err := NewLogger(filename, rule, true)
|
||||
assert.Nil(t, err)
|
||||
if len(filename) > 0 {
|
||||
defer func() {
|
||||
os.Remove(logger.getBackupFilename())
|
||||
os.Remove(filepath.Base(logger.getBackupFilename()) + ".gz")
|
||||
}()
|
||||
}
|
||||
// the following write calls cannot be changed to Write, because of DATA RACE.
|
||||
logger.write([]byte(`foo`))
|
||||
rule.rotatedTime = time.Now().Add(-time.Hour * 24).Format(dateFormat)
|
||||
logger.write([]byte(`bar`))
|
||||
logger.Close()
|
||||
logger.write([]byte(`baz`))
|
||||
}
|
||||
|
||||
func BenchmarkRotateLogger(b *testing.B) {
|
||||
filename := "./test.log"
|
||||
filename2 := "./test2.log"
|
||||
dailyRotateRuleLogger, err1 := NewLogger(
|
||||
filename,
|
||||
DefaultRotateRule(
|
||||
filename,
|
||||
backupFileDelimiter,
|
||||
1,
|
||||
true,
|
||||
),
|
||||
true,
|
||||
)
|
||||
if err1 != nil {
|
||||
b.Logf("Failed to new daily rotate rule logger: %v", err1)
|
||||
b.FailNow()
|
||||
}
|
||||
sizeLimitRotateRuleLogger, err2 := NewLogger(
|
||||
filename2,
|
||||
NewSizeLimitRotateRule(
|
||||
filename,
|
||||
backupFileDelimiter,
|
||||
1,
|
||||
100,
|
||||
10,
|
||||
true,
|
||||
),
|
||||
true,
|
||||
)
|
||||
if err2 != nil {
|
||||
b.Logf("Failed to new size limit rotate rule logger: %v", err1)
|
||||
b.FailNow()
|
||||
}
|
||||
defer func() {
|
||||
dailyRotateRuleLogger.Close()
|
||||
sizeLimitRotateRuleLogger.Close()
|
||||
os.Remove(filename)
|
||||
os.Remove(filename2)
|
||||
}()
|
||||
|
||||
b.Run("daily rotate rule", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
dailyRotateRuleLogger.write([]byte("testing\ntesting\n"))
|
||||
}
|
||||
})
|
||||
b.Run("size limit rotate rule", func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
sizeLimitRotateRuleLogger.write([]byte("testing\ntesting\n"))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -29,24 +29,31 @@ func TestRedirector(t *testing.T) {
|
||||
}
|
||||
|
||||
func captureOutput(f func()) string {
|
||||
writer := new(mockWriter)
|
||||
infoLog = writer
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
w := new(mockWriter)
|
||||
old := writer.Swap(w)
|
||||
defer writer.Store(old)
|
||||
|
||||
prevLevel := atomic.LoadUint32(&logLevel)
|
||||
SetLevel(InfoLevel)
|
||||
f()
|
||||
SetLevel(prevLevel)
|
||||
|
||||
return writer.builder.String()
|
||||
return w.String()
|
||||
}
|
||||
|
||||
func getContent(jsonStr string) string {
|
||||
var entry logEntry
|
||||
var entry map[string]interface{}
|
||||
json.Unmarshal([]byte(jsonStr), &entry)
|
||||
val, ok := entry.Content.(string)
|
||||
if ok {
|
||||
return val
|
||||
|
||||
val, ok := entry[contentKey]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return ""
|
||||
|
||||
str, ok := val.(string)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
@@ -1,124 +0,0 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/timex"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type traceLogger struct {
|
||||
logEntry
|
||||
Trace string `json:"trace,omitempty"`
|
||||
Span string `json:"span,omitempty"`
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func (l *traceLogger) Error(v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprint(v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Errorf(format string, v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, formatWithCaller(fmt.Sprintf(format, v...), durationCallerDepth))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Errorv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(errorLog, levelError, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Info(v ...interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Infof(format string, v ...interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Infov(v interface{}) {
|
||||
if shallLog(InfoLevel) {
|
||||
l.write(infoLog, levelInfo, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Slow(v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprint(v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Slowf(format string, v ...interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, fmt.Sprintf(format, v...))
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) Slowv(v interface{}) {
|
||||
if shallLog(ErrorLevel) {
|
||||
l.write(slowLog, levelSlow, v)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *traceLogger) WithDuration(duration time.Duration) Logger {
|
||||
l.Duration = timex.ReprOfDuration(duration)
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *traceLogger) write(writer io.Writer, level string, val interface{}) {
|
||||
traceID := traceIdFromContext(l.ctx)
|
||||
spanID := spanIdFromContext(l.ctx)
|
||||
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
writePlainAny(writer, level, val, l.Duration, traceID, spanID)
|
||||
default:
|
||||
outputJson(writer, &traceLogger{
|
||||
logEntry: logEntry{
|
||||
Timestamp: getTimestamp(),
|
||||
Level: level,
|
||||
Duration: l.Duration,
|
||||
Content: val,
|
||||
},
|
||||
Trace: traceID,
|
||||
Span: spanID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// WithContext sets ctx to log, for keeping tracing information.
|
||||
func WithContext(ctx context.Context) Logger {
|
||||
return &traceLogger{
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func spanIdFromContext(ctx context.Context) string {
|
||||
spanCtx := trace.SpanContextFromContext(ctx)
|
||||
if spanCtx.HasSpanID() {
|
||||
return spanCtx.SpanID().String()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func traceIdFromContext(ctx context.Context) string {
|
||||
spanCtx := trace.SpanContextFromContext(ctx)
|
||||
if spanCtx.HasTraceID() {
|
||||
return spanCtx.TraceID().String()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.opentelemetry.io/otel"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
const (
|
||||
traceKey = "trace"
|
||||
spanKey = "span"
|
||||
)
|
||||
|
||||
func TestTraceLog(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
WithContext(ctx).(*traceLogger).write(&buf, levelInfo, testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceError(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
errorLog = newLogWriter(log.New(&buf, "", flags))
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
l := WithContext(ctx).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Error(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Errorf(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Errorv(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceInfo(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
infoLog = newLogWriter(log.New(&buf, "", flags))
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
l := WithContext(ctx).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Infov(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceInfoConsole(t *testing.T) {
|
||||
old := atomic.LoadUint32(&encoding)
|
||||
atomic.StoreUint32(&encoding, jsonEncodingType)
|
||||
defer func() {
|
||||
atomic.StoreUint32(&encoding, old)
|
||||
}()
|
||||
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
infoLog = newLogWriter(log.New(&buf, "", flags))
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
l := WithContext(ctx).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
|
||||
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
|
||||
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Infov(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceIdFromContext(ctx)))
|
||||
assert.True(t, strings.Contains(buf.String(), spanIdFromContext(ctx)))
|
||||
}
|
||||
|
||||
func TestTraceSlow(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
slowLog = newLogWriter(log.New(&buf, "", flags))
|
||||
otp := otel.GetTracerProvider()
|
||||
tp := sdktrace.NewTracerProvider(sdktrace.WithSampler(sdktrace.AlwaysSample()))
|
||||
otel.SetTracerProvider(tp)
|
||||
defer otel.SetTracerProvider(otp)
|
||||
|
||||
ctx, _ := tp.Tracer("foo").Start(context.Background(), "bar")
|
||||
l := WithContext(ctx).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Slow(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Slowf(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Slowv(testlog)
|
||||
assert.True(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.True(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
|
||||
func TestTraceWithoutContext(t *testing.T) {
|
||||
var buf mockWriter
|
||||
atomic.StoreUint32(&initialized, 1)
|
||||
infoLog = newLogWriter(log.New(&buf, "", flags))
|
||||
l := WithContext(context.Background()).(*traceLogger)
|
||||
SetLevel(InfoLevel)
|
||||
l.WithDuration(time.Second).Info(testlog)
|
||||
assert.False(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.False(t, strings.Contains(buf.String(), spanKey))
|
||||
buf.Reset()
|
||||
l.WithDuration(time.Second).Infof(testlog)
|
||||
assert.False(t, strings.Contains(buf.String(), traceKey))
|
||||
assert.False(t, strings.Contains(buf.String(), spanKey))
|
||||
}
|
||||
35
core/logx/util.go
Normal file
35
core/logx/util.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func getCaller(callDepth int) string {
|
||||
_, file, line, ok := runtime.Caller(callDepth)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
|
||||
return prettyCaller(file, line)
|
||||
}
|
||||
|
||||
func getTimestamp() string {
|
||||
return time.Now().Format(timeFormat)
|
||||
}
|
||||
|
||||
func prettyCaller(file string, line int) string {
|
||||
idx := strings.LastIndexByte(file, '/')
|
||||
if idx < 0 {
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
idx = strings.LastIndexByte(file[:idx], '/')
|
||||
if idx < 0 {
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s:%d", file[idx+1:], line)
|
||||
}
|
||||
72
core/logx/util_test.go
Normal file
72
core/logx/util_test.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetCaller(t *testing.T) {
|
||||
_, file, _, _ := runtime.Caller(0)
|
||||
assert.Contains(t, getCaller(1), filepath.Base(file))
|
||||
assert.True(t, len(getCaller(1<<10)) == 0)
|
||||
}
|
||||
|
||||
func TestGetTimestamp(t *testing.T) {
|
||||
ts := getTimestamp()
|
||||
tm, err := time.Parse(timeFormat, ts)
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, time.Since(tm) < time.Minute)
|
||||
}
|
||||
|
||||
func TestPrettyCaller(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
file string
|
||||
line int
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "regular",
|
||||
file: "logx_test.go",
|
||||
line: 123,
|
||||
want: "logx_test.go:123",
|
||||
},
|
||||
{
|
||||
name: "relative",
|
||||
file: "adhoc/logx_test.go",
|
||||
line: 123,
|
||||
want: "adhoc/logx_test.go:123",
|
||||
},
|
||||
{
|
||||
name: "long path",
|
||||
file: "github.com/zeromicro/go-zero/core/logx/util_test.go",
|
||||
line: 12,
|
||||
want: "logx/util_test.go:12",
|
||||
},
|
||||
{
|
||||
name: "local path",
|
||||
file: "/Users/kevin/go-zero/core/logx/util_test.go",
|
||||
line: 1234,
|
||||
want: "logx/util_test.go:1234",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
assert.Equal(t, test.want, prettyCaller(test.file, test.line))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGetCaller(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
getCaller(1)
|
||||
}
|
||||
}
|
||||
66
core/logx/vars.go
Normal file
66
core/logx/vars.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package logx
|
||||
|
||||
import "errors"
|
||||
|
||||
const (
|
||||
// DebugLevel logs everything
|
||||
DebugLevel uint32 = iota
|
||||
// InfoLevel does not include debugs
|
||||
InfoLevel
|
||||
// ErrorLevel includes errors, slows, stacks
|
||||
ErrorLevel
|
||||
// SevereLevel only log severe messages
|
||||
SevereLevel
|
||||
)
|
||||
|
||||
const (
|
||||
jsonEncodingType = iota
|
||||
plainEncodingType
|
||||
)
|
||||
|
||||
const (
|
||||
plainEncoding = "plain"
|
||||
plainEncodingSep = '\t'
|
||||
sizeRotationRule = "size"
|
||||
|
||||
accessFilename = "access.log"
|
||||
errorFilename = "error.log"
|
||||
severeFilename = "severe.log"
|
||||
slowFilename = "slow.log"
|
||||
statFilename = "stat.log"
|
||||
|
||||
fileMode = "file"
|
||||
volumeMode = "volume"
|
||||
|
||||
levelAlert = "alert"
|
||||
levelInfo = "info"
|
||||
levelError = "error"
|
||||
levelSevere = "severe"
|
||||
levelFatal = "fatal"
|
||||
levelSlow = "slow"
|
||||
levelStat = "stat"
|
||||
levelDebug = "debug"
|
||||
|
||||
backupFileDelimiter = "-"
|
||||
flags = 0x0
|
||||
)
|
||||
|
||||
const (
|
||||
callerKey = "caller"
|
||||
contentKey = "content"
|
||||
durationKey = "duration"
|
||||
levelKey = "level"
|
||||
spanKey = "span"
|
||||
timestampKey = "@timestamp"
|
||||
traceKey = "trace"
|
||||
truncatedKey = "truncated"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrLogPathNotSet is an error that indicates the log path is not set.
|
||||
ErrLogPathNotSet = errors.New("log path must be set")
|
||||
// ErrLogServiceNameNotSet is an error that indicates that the service name is not set.
|
||||
ErrLogServiceNameNotSet = errors.New("log service name must be set")
|
||||
|
||||
truncatedField = Field(truncatedKey, true)
|
||||
)
|
||||
404
core/logx/writer.go
Normal file
404
core/logx/writer.go
Normal file
@@ -0,0 +1,404 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"path"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
fatihcolor "github.com/fatih/color"
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
)
|
||||
|
||||
type (
|
||||
Writer interface {
|
||||
Alert(v interface{})
|
||||
Close() error
|
||||
Debug(v interface{}, fields ...LogField)
|
||||
Error(v interface{}, fields ...LogField)
|
||||
Info(v interface{}, fields ...LogField)
|
||||
Severe(v interface{})
|
||||
Slow(v interface{}, fields ...LogField)
|
||||
Stack(v interface{})
|
||||
Stat(v interface{}, fields ...LogField)
|
||||
}
|
||||
|
||||
atomicWriter struct {
|
||||
writer Writer
|
||||
lock sync.RWMutex
|
||||
}
|
||||
|
||||
concreteWriter struct {
|
||||
infoLog io.WriteCloser
|
||||
errorLog io.WriteCloser
|
||||
severeLog io.WriteCloser
|
||||
slowLog io.WriteCloser
|
||||
statLog io.WriteCloser
|
||||
stackLog io.Writer
|
||||
}
|
||||
)
|
||||
|
||||
// NewWriter creates a new Writer with the given io.Writer.
|
||||
func NewWriter(w io.Writer) Writer {
|
||||
lw := newLogWriter(log.New(w, "", flags))
|
||||
|
||||
return &concreteWriter{
|
||||
infoLog: lw,
|
||||
errorLog: lw,
|
||||
severeLog: lw,
|
||||
slowLog: lw,
|
||||
statLog: lw,
|
||||
stackLog: lw,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *atomicWriter) Load() Writer {
|
||||
w.lock.RLock()
|
||||
defer w.lock.RUnlock()
|
||||
return w.writer
|
||||
}
|
||||
|
||||
func (w *atomicWriter) Store(v Writer) {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
w.writer = v
|
||||
}
|
||||
|
||||
func (w *atomicWriter) StoreIfNil(v Writer) Writer {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
|
||||
if w.writer == nil {
|
||||
w.writer = v
|
||||
}
|
||||
|
||||
return w.writer
|
||||
}
|
||||
|
||||
func (w *atomicWriter) Swap(v Writer) Writer {
|
||||
w.lock.Lock()
|
||||
defer w.lock.Unlock()
|
||||
old := w.writer
|
||||
w.writer = v
|
||||
return old
|
||||
}
|
||||
|
||||
func newConsoleWriter() Writer {
|
||||
outLog := newLogWriter(log.New(fatihcolor.Output, "", flags))
|
||||
errLog := newLogWriter(log.New(fatihcolor.Error, "", flags))
|
||||
return &concreteWriter{
|
||||
infoLog: outLog,
|
||||
errorLog: errLog,
|
||||
severeLog: errLog,
|
||||
slowLog: errLog,
|
||||
stackLog: newLessWriter(errLog, options.logStackCooldownMills),
|
||||
statLog: outLog,
|
||||
}
|
||||
}
|
||||
|
||||
func newFileWriter(c LogConf) (Writer, error) {
|
||||
var err error
|
||||
var opts []LogOption
|
||||
var infoLog io.WriteCloser
|
||||
var errorLog io.WriteCloser
|
||||
var severeLog io.WriteCloser
|
||||
var slowLog io.WriteCloser
|
||||
var statLog io.WriteCloser
|
||||
var stackLog io.Writer
|
||||
|
||||
if len(c.Path) == 0 {
|
||||
return nil, ErrLogPathNotSet
|
||||
}
|
||||
|
||||
opts = append(opts, WithCooldownMillis(c.StackCooldownMillis))
|
||||
if c.Compress {
|
||||
opts = append(opts, WithGzip())
|
||||
}
|
||||
if c.KeepDays > 0 {
|
||||
opts = append(opts, WithKeepDays(c.KeepDays))
|
||||
}
|
||||
if c.MaxBackups > 0 {
|
||||
opts = append(opts, WithMaxBackups(c.MaxBackups))
|
||||
}
|
||||
if c.MaxSize > 0 {
|
||||
opts = append(opts, WithMaxSize(c.MaxSize))
|
||||
}
|
||||
|
||||
opts = append(opts, WithRotation(c.Rotation))
|
||||
|
||||
accessFile := path.Join(c.Path, accessFilename)
|
||||
errorFile := path.Join(c.Path, errorFilename)
|
||||
severeFile := path.Join(c.Path, severeFilename)
|
||||
slowFile := path.Join(c.Path, slowFilename)
|
||||
statFile := path.Join(c.Path, statFilename)
|
||||
|
||||
handleOptions(opts)
|
||||
setupLogLevel(c)
|
||||
|
||||
if infoLog, err = createOutput(accessFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if errorLog, err = createOutput(errorFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if severeLog, err = createOutput(severeFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if slowLog, err = createOutput(slowFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if statLog, err = createOutput(statFile); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
stackLog = newLessWriter(errorLog, options.logStackCooldownMills)
|
||||
|
||||
return &concreteWriter{
|
||||
infoLog: infoLog,
|
||||
errorLog: errorLog,
|
||||
severeLog: severeLog,
|
||||
slowLog: slowLog,
|
||||
statLog: statLog,
|
||||
stackLog: stackLog,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Alert(v interface{}) {
|
||||
output(w.errorLog, levelAlert, v)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Close() error {
|
||||
if err := w.infoLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := w.errorLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := w.severeLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := w.slowLog.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return w.statLog.Close()
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Debug(v interface{}, fields ...LogField) {
|
||||
output(w.infoLog, levelDebug, v, fields...)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Error(v interface{}, fields ...LogField) {
|
||||
output(w.errorLog, levelError, v, fields...)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Info(v interface{}, fields ...LogField) {
|
||||
output(w.infoLog, levelInfo, v, fields...)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Severe(v interface{}) {
|
||||
output(w.severeLog, levelFatal, v)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Slow(v interface{}, fields ...LogField) {
|
||||
output(w.slowLog, levelSlow, v, fields...)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Stack(v interface{}) {
|
||||
output(w.stackLog, levelError, v)
|
||||
}
|
||||
|
||||
func (w *concreteWriter) Stat(v interface{}, fields ...LogField) {
|
||||
output(w.statLog, levelStat, v, fields...)
|
||||
}
|
||||
|
||||
type nopWriter struct{}
|
||||
|
||||
func (n nopWriter) Alert(_ interface{}) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n nopWriter) Debug(_ interface{}, _ ...LogField) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Error(_ interface{}, _ ...LogField) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Info(_ interface{}, _ ...LogField) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Severe(_ interface{}) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Slow(_ interface{}, _ ...LogField) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Stack(_ interface{}) {
|
||||
}
|
||||
|
||||
func (n nopWriter) Stat(_ interface{}, _ ...LogField) {
|
||||
}
|
||||
|
||||
func buildPlainFields(fields ...LogField) []string {
|
||||
var items []string
|
||||
|
||||
for _, field := range fields {
|
||||
items = append(items, fmt.Sprintf("%s=%+v", field.Key, field.Value))
|
||||
}
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
func combineGlobalFields(fields []LogField) []LogField {
|
||||
globals := globalFields.Load()
|
||||
if globals == nil {
|
||||
return fields
|
||||
}
|
||||
|
||||
gf := globals.([]LogField)
|
||||
ret := make([]LogField, 0, len(gf)+len(fields))
|
||||
ret = append(ret, gf...)
|
||||
ret = append(ret, fields...)
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func output(writer io.Writer, level string, val interface{}, fields ...LogField) {
|
||||
// only truncate string content, don't know how to truncate the values of other types.
|
||||
if v, ok := val.(string); ok {
|
||||
maxLen := atomic.LoadUint32(&maxContentLength)
|
||||
if maxLen > 0 && len(v) > int(maxLen) {
|
||||
val = v[:maxLen]
|
||||
fields = append(fields, truncatedField)
|
||||
}
|
||||
}
|
||||
|
||||
fields = combineGlobalFields(fields)
|
||||
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
writePlainAny(writer, level, val, buildPlainFields(fields...)...)
|
||||
default:
|
||||
entry := make(logEntry)
|
||||
for _, field := range fields {
|
||||
entry[field.Key] = field.Value
|
||||
}
|
||||
entry[timestampKey] = getTimestamp()
|
||||
entry[levelKey] = level
|
||||
entry[contentKey] = val
|
||||
writeJson(writer, entry)
|
||||
}
|
||||
}
|
||||
|
||||
func wrapLevelWithColor(level string) string {
|
||||
var colour color.Color
|
||||
switch level {
|
||||
case levelAlert:
|
||||
colour = color.FgRed
|
||||
case levelError:
|
||||
colour = color.FgRed
|
||||
case levelFatal:
|
||||
colour = color.FgRed
|
||||
case levelInfo:
|
||||
colour = color.FgBlue
|
||||
case levelSlow:
|
||||
colour = color.FgYellow
|
||||
case levelDebug:
|
||||
colour = color.FgYellow
|
||||
case levelStat:
|
||||
colour = color.FgGreen
|
||||
}
|
||||
|
||||
if colour == color.NoColor {
|
||||
return level
|
||||
}
|
||||
|
||||
return color.WithColorPadding(level, colour)
|
||||
}
|
||||
|
||||
func writeJson(writer io.Writer, info interface{}) {
|
||||
if content, err := json.Marshal(info); err != nil {
|
||||
log.Println(err.Error())
|
||||
} else if writer == nil {
|
||||
log.Println(string(content))
|
||||
} else {
|
||||
writer.Write(append(content, '\n'))
|
||||
}
|
||||
}
|
||||
|
||||
func writePlainAny(writer io.Writer, level string, val interface{}, fields ...string) {
|
||||
level = wrapLevelWithColor(level)
|
||||
|
||||
switch v := val.(type) {
|
||||
case string:
|
||||
writePlainText(writer, level, v, fields...)
|
||||
case error:
|
||||
writePlainText(writer, level, v.Error(), fields...)
|
||||
case fmt.Stringer:
|
||||
writePlainText(writer, level, v.String(), fields...)
|
||||
default:
|
||||
writePlainValue(writer, level, v, fields...)
|
||||
}
|
||||
}
|
||||
|
||||
func writePlainText(writer io.Writer, level, msg string, fields ...string) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(getTimestamp())
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(level)
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(msg)
|
||||
for _, item := range fields {
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(item)
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
if writer == nil {
|
||||
log.Println(buf.String())
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func writePlainValue(writer io.Writer, level string, val interface{}, fields ...string) {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(getTimestamp())
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(level)
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
if err := json.NewEncoder(&buf).Encode(val); err != nil {
|
||||
log.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range fields {
|
||||
buf.WriteByte(plainEncodingSep)
|
||||
buf.WriteString(item)
|
||||
}
|
||||
buf.WriteByte('\n')
|
||||
if writer == nil {
|
||||
log.Println(buf.String())
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := writer.Write(buf.Bytes()); err != nil {
|
||||
log.Println(err.Error())
|
||||
}
|
||||
}
|
||||
221
core/logx/writer_test.go
Normal file
221
core/logx/writer_test.go
Normal file
@@ -0,0 +1,221 @@
|
||||
package logx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewWriter(t *testing.T) {
|
||||
const literal = "foo bar"
|
||||
var buf bytes.Buffer
|
||||
w := NewWriter(&buf)
|
||||
w.Info(literal)
|
||||
assert.Contains(t, buf.String(), literal)
|
||||
buf.Reset()
|
||||
w.Debug(literal)
|
||||
assert.Contains(t, buf.String(), literal)
|
||||
}
|
||||
|
||||
func TestConsoleWriter(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
w := newConsoleWriter()
|
||||
lw := newLogWriter(log.New(&buf, "", 0))
|
||||
w.(*concreteWriter).errorLog = lw
|
||||
w.Alert("foo bar 1")
|
||||
var val mockedEntry
|
||||
if err := json.Unmarshal(buf.Bytes(), &val); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, levelAlert, val.Level)
|
||||
assert.Equal(t, "foo bar 1", val.Content)
|
||||
|
||||
buf.Reset()
|
||||
w.(*concreteWriter).errorLog = lw
|
||||
w.Error("foo bar 2")
|
||||
if err := json.Unmarshal(buf.Bytes(), &val); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, levelError, val.Level)
|
||||
assert.Equal(t, "foo bar 2", val.Content)
|
||||
|
||||
buf.Reset()
|
||||
w.(*concreteWriter).infoLog = lw
|
||||
w.Info("foo bar 3")
|
||||
if err := json.Unmarshal(buf.Bytes(), &val); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, levelInfo, val.Level)
|
||||
assert.Equal(t, "foo bar 3", val.Content)
|
||||
|
||||
buf.Reset()
|
||||
w.(*concreteWriter).severeLog = lw
|
||||
w.Severe("foo bar 4")
|
||||
if err := json.Unmarshal(buf.Bytes(), &val); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, levelFatal, val.Level)
|
||||
assert.Equal(t, "foo bar 4", val.Content)
|
||||
|
||||
buf.Reset()
|
||||
w.(*concreteWriter).slowLog = lw
|
||||
w.Slow("foo bar 5")
|
||||
if err := json.Unmarshal(buf.Bytes(), &val); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, levelSlow, val.Level)
|
||||
assert.Equal(t, "foo bar 5", val.Content)
|
||||
|
||||
buf.Reset()
|
||||
w.(*concreteWriter).statLog = lw
|
||||
w.Stat("foo bar 6")
|
||||
if err := json.Unmarshal(buf.Bytes(), &val); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, levelStat, val.Level)
|
||||
assert.Equal(t, "foo bar 6", val.Content)
|
||||
|
||||
w.(*concreteWriter).infoLog = hardToCloseWriter{}
|
||||
assert.NotNil(t, w.Close())
|
||||
w.(*concreteWriter).infoLog = easyToCloseWriter{}
|
||||
w.(*concreteWriter).errorLog = hardToCloseWriter{}
|
||||
assert.NotNil(t, w.Close())
|
||||
w.(*concreteWriter).errorLog = easyToCloseWriter{}
|
||||
w.(*concreteWriter).severeLog = hardToCloseWriter{}
|
||||
assert.NotNil(t, w.Close())
|
||||
w.(*concreteWriter).severeLog = easyToCloseWriter{}
|
||||
w.(*concreteWriter).slowLog = hardToCloseWriter{}
|
||||
assert.NotNil(t, w.Close())
|
||||
w.(*concreteWriter).slowLog = easyToCloseWriter{}
|
||||
w.(*concreteWriter).statLog = hardToCloseWriter{}
|
||||
assert.NotNil(t, w.Close())
|
||||
w.(*concreteWriter).statLog = easyToCloseWriter{}
|
||||
}
|
||||
|
||||
func TestNopWriter(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
var w nopWriter
|
||||
w.Alert("foo")
|
||||
w.Debug("foo")
|
||||
w.Error("foo")
|
||||
w.Info("foo")
|
||||
w.Severe("foo")
|
||||
w.Stack("foo")
|
||||
w.Stat("foo")
|
||||
w.Slow("foo")
|
||||
w.Close()
|
||||
})
|
||||
}
|
||||
|
||||
func TestWriteJson(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
writeJson(nil, "foo")
|
||||
assert.Contains(t, buf.String(), "foo")
|
||||
buf.Reset()
|
||||
writeJson(nil, make(chan int))
|
||||
assert.Contains(t, buf.String(), "unsupported type")
|
||||
}
|
||||
|
||||
func TestWritePlainAny(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
log.SetOutput(&buf)
|
||||
writePlainAny(nil, levelInfo, "foo")
|
||||
assert.Contains(t, buf.String(), "foo")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(nil, levelDebug, make(chan int))
|
||||
assert.Contains(t, buf.String(), "unsupported type")
|
||||
writePlainAny(nil, levelDebug, 100)
|
||||
assert.Contains(t, buf.String(), "100")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(nil, levelError, make(chan int))
|
||||
assert.Contains(t, buf.String(), "unsupported type")
|
||||
writePlainAny(nil, levelSlow, 100)
|
||||
assert.Contains(t, buf.String(), "100")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(hardToWriteWriter{}, levelStat, 100)
|
||||
assert.Contains(t, buf.String(), "write error")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(hardToWriteWriter{}, levelSevere, "foo")
|
||||
assert.Contains(t, buf.String(), "write error")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(hardToWriteWriter{}, levelAlert, "foo")
|
||||
assert.Contains(t, buf.String(), "write error")
|
||||
|
||||
buf.Reset()
|
||||
writePlainAny(hardToWriteWriter{}, levelFatal, "foo")
|
||||
assert.Contains(t, buf.String(), "write error")
|
||||
|
||||
}
|
||||
|
||||
func TestLogWithLimitContentLength(t *testing.T) {
|
||||
maxLen := atomic.LoadUint32(&maxContentLength)
|
||||
atomic.StoreUint32(&maxContentLength, 10)
|
||||
|
||||
t.Cleanup(func() {
|
||||
atomic.StoreUint32(&maxContentLength, maxLen)
|
||||
})
|
||||
|
||||
t.Run("alert", func(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
w := NewWriter(&buf)
|
||||
w.Info("1234567890")
|
||||
var v1 mockedEntry
|
||||
if err := json.Unmarshal(buf.Bytes(), &v1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, "1234567890", v1.Content)
|
||||
assert.False(t, v1.Truncated)
|
||||
|
||||
buf.Reset()
|
||||
var v2 mockedEntry
|
||||
w.Info("12345678901")
|
||||
if err := json.Unmarshal(buf.Bytes(), &v2); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, "1234567890", v2.Content)
|
||||
assert.True(t, v2.Truncated)
|
||||
})
|
||||
}
|
||||
|
||||
type mockedEntry struct {
|
||||
Level string `json:"level"`
|
||||
Content string `json:"content"`
|
||||
Truncated bool `json:"truncated"`
|
||||
}
|
||||
|
||||
type easyToCloseWriter struct{}
|
||||
|
||||
func (h easyToCloseWriter) Write(_ []byte) (_ int, _ error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (h easyToCloseWriter) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type hardToCloseWriter struct{}
|
||||
|
||||
func (h hardToCloseWriter) Write(_ []byte) (_ int, _ error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (h hardToCloseWriter) Close() error {
|
||||
return errors.New("close error")
|
||||
}
|
||||
|
||||
type hardToWriteWriter struct{}
|
||||
|
||||
func (h hardToWriteWriter) Write(_ []byte) (_ int, _ error) {
|
||||
return 0, errors.New("write error")
|
||||
}
|
||||
@@ -8,10 +8,12 @@ type (
|
||||
// use context and OptionalDep option to determine the value of Optional
|
||||
// nothing to do with context.Context
|
||||
fieldOptionsWithContext struct {
|
||||
Inherit bool
|
||||
FromString bool
|
||||
Optional bool
|
||||
Options []string
|
||||
Default string
|
||||
EnvVar string
|
||||
Range *numberRange
|
||||
}
|
||||
|
||||
@@ -40,6 +42,10 @@ func (o *fieldOptionsWithContext) getDefault() (string, bool) {
|
||||
return o.Default, len(o.Default) > 0
|
||||
}
|
||||
|
||||
func (o *fieldOptionsWithContext) inherit() bool {
|
||||
return o != nil && o.Inherit
|
||||
}
|
||||
|
||||
func (o *fieldOptionsWithContext) optional() bool {
|
||||
return o != nil && o.Optional
|
||||
}
|
||||
@@ -101,5 +107,6 @@ func (o *fieldOptions) toOptionsWithContext(key string, m Valuer, fullName strin
|
||||
Optional: optional,
|
||||
Options: o.Options,
|
||||
Default: o.Default,
|
||||
EnvVar: o.EnvVar,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -11,22 +11,30 @@ const jsonTagKey = "json"
|
||||
var jsonUnmarshaler = NewUnmarshaler(jsonTagKey)
|
||||
|
||||
// UnmarshalJsonBytes unmarshals content into v.
|
||||
func UnmarshalJsonBytes(content []byte, v interface{}) error {
|
||||
return unmarshalJsonBytes(content, v, jsonUnmarshaler)
|
||||
func UnmarshalJsonBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
|
||||
return unmarshalJsonBytes(content, v, getJsonUnmarshaler(opts...))
|
||||
}
|
||||
|
||||
// UnmarshalJsonMap unmarshals content from m into v.
|
||||
func UnmarshalJsonMap(m map[string]interface{}, v interface{}) error {
|
||||
return jsonUnmarshaler.Unmarshal(m, v)
|
||||
func UnmarshalJsonMap(m map[string]interface{}, v interface{}, opts ...UnmarshalOption) error {
|
||||
return getJsonUnmarshaler(opts...).Unmarshal(m, v)
|
||||
}
|
||||
|
||||
// UnmarshalJsonReader unmarshals content from reader into v.
|
||||
func UnmarshalJsonReader(reader io.Reader, v interface{}) error {
|
||||
return unmarshalJsonReader(reader, v, jsonUnmarshaler)
|
||||
func UnmarshalJsonReader(reader io.Reader, v interface{}, opts ...UnmarshalOption) error {
|
||||
return unmarshalJsonReader(reader, v, getJsonUnmarshaler(opts...))
|
||||
}
|
||||
|
||||
func getJsonUnmarshaler(opts ...UnmarshalOption) *Unmarshaler {
|
||||
if len(opts) > 0 {
|
||||
return NewUnmarshaler(jsonTagKey, opts...)
|
||||
}
|
||||
|
||||
return jsonUnmarshaler
|
||||
}
|
||||
|
||||
func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler) error {
|
||||
var m map[string]interface{}
|
||||
var m interface{}
|
||||
if err := jsonx.Unmarshal(content, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -35,7 +43,7 @@ func unmarshalJsonBytes(content []byte, v interface{}, unmarshaler *Unmarshaler)
|
||||
}
|
||||
|
||||
func unmarshalJsonReader(reader io.Reader, v interface{}, unmarshaler *Unmarshaler) error {
|
||||
var m map[string]interface{}
|
||||
var m interface{}
|
||||
if err := jsonx.UnmarshalFromReader(reader, &m); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -856,8 +856,7 @@ func TestUnmarshalBytesError(t *testing.T) {
|
||||
}
|
||||
|
||||
err := UnmarshalJsonBytes([]byte(payload), &v)
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), payload))
|
||||
assert.Equal(t, errTypeMismatch, err)
|
||||
}
|
||||
|
||||
func TestUnmarshalReaderError(t *testing.T) {
|
||||
@@ -867,9 +866,7 @@ func TestUnmarshalReaderError(t *testing.T) {
|
||||
Any string
|
||||
}
|
||||
|
||||
err := UnmarshalJsonReader(reader, &v)
|
||||
assert.NotNil(t, err)
|
||||
assert.True(t, strings.Contains(err.Error(), payload))
|
||||
assert.Equal(t, errTypeMismatch, UnmarshalJsonReader(reader, &v))
|
||||
}
|
||||
|
||||
func TestUnmarshalMap(t *testing.T) {
|
||||
@@ -900,7 +897,9 @@ func TestUnmarshalMap(t *testing.T) {
|
||||
Any string `json:",optional"`
|
||||
}
|
||||
|
||||
err := UnmarshalJsonMap(m, &v)
|
||||
err := UnmarshalJsonMap(m, &v, WithCanonicalKeyFunc(func(s string) string {
|
||||
return s
|
||||
}))
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, len(v.Any) == 0)
|
||||
})
|
||||
@@ -918,3 +917,26 @@ func TestUnmarshalMap(t *testing.T) {
|
||||
assert.Equal(t, "foo", v.Any)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonArray(t *testing.T) {
|
||||
var v []struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
body := `[{"name":"kevin", "age": 18}]`
|
||||
assert.NoError(t, UnmarshalJsonBytes([]byte(body), &v))
|
||||
assert.Equal(t, 1, len(v))
|
||||
assert.Equal(t, "kevin", v[0].Name)
|
||||
assert.Equal(t, 18, v[0].Age)
|
||||
}
|
||||
|
||||
func TestUnmarshalJsonBytesError(t *testing.T) {
|
||||
var v []struct {
|
||||
Name string `json:"name"`
|
||||
Age int `json:"age"`
|
||||
}
|
||||
|
||||
assert.Error(t, UnmarshalJsonBytes([]byte((``)), &v))
|
||||
assert.Error(t, UnmarshalJsonReader(strings.NewReader(``), &v))
|
||||
}
|
||||
|
||||
186
core/mapping/marshaler.go
Normal file
186
core/mapping/marshaler.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
emptyTag = ""
|
||||
tagKVSeparator = ":"
|
||||
)
|
||||
|
||||
// Marshal marshals the given val and returns the map that contains the fields.
|
||||
// optional=another is not implemented, and it's hard to implement and not common used.
|
||||
func Marshal(val interface{}) (map[string]map[string]interface{}, error) {
|
||||
ret := make(map[string]map[string]interface{})
|
||||
tp := reflect.TypeOf(val)
|
||||
if tp.Kind() == reflect.Ptr {
|
||||
tp = tp.Elem()
|
||||
}
|
||||
rv := reflect.ValueOf(val)
|
||||
if rv.Kind() == reflect.Ptr {
|
||||
rv = rv.Elem()
|
||||
}
|
||||
|
||||
for i := 0; i < tp.NumField(); i++ {
|
||||
field := tp.Field(i)
|
||||
value := rv.Field(i)
|
||||
if err := processMember(field, value, ret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func getTag(field reflect.StructField) (string, bool) {
|
||||
tag := string(field.Tag)
|
||||
if i := strings.Index(tag, tagKVSeparator); i >= 0 {
|
||||
return strings.TrimSpace(tag[:i]), true
|
||||
}
|
||||
|
||||
return strings.TrimSpace(tag), false
|
||||
}
|
||||
|
||||
func processMember(field reflect.StructField, value reflect.Value,
|
||||
collector map[string]map[string]interface{}) error {
|
||||
var key string
|
||||
var opt *fieldOptions
|
||||
var err error
|
||||
tag, ok := getTag(field)
|
||||
if !ok {
|
||||
tag = emptyTag
|
||||
key = field.Name
|
||||
} else {
|
||||
key, opt, err = parseKeyAndOptions(tag, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = validate(field, value, opt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
val := value.Interface()
|
||||
if opt != nil && opt.FromString {
|
||||
val = fmt.Sprint(val)
|
||||
}
|
||||
|
||||
m, ok := collector[tag]
|
||||
if ok {
|
||||
m[key] = val
|
||||
} else {
|
||||
m = map[string]interface{}{
|
||||
key: val,
|
||||
}
|
||||
}
|
||||
collector[tag] = m
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validate(field reflect.StructField, value reflect.Value, opt *fieldOptions) error {
|
||||
if opt == nil || !opt.Optional {
|
||||
if err := validateOptional(field, value); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if opt == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if opt.Optional && value.IsZero() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(opt.Options) > 0 {
|
||||
if err := validateOptions(value, opt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if opt.Range != nil {
|
||||
if err := validateRange(value, opt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateOptional(field reflect.StructField, value reflect.Value) error {
|
||||
switch field.Type.Kind() {
|
||||
case reflect.Ptr:
|
||||
if value.IsNil() {
|
||||
return fmt.Errorf("field %q is nil", field.Name)
|
||||
}
|
||||
case reflect.Array, reflect.Slice, reflect.Map:
|
||||
if value.IsNil() || value.Len() == 0 {
|
||||
return fmt.Errorf("field %q is empty", field.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateOptions(value reflect.Value, opt *fieldOptions) error {
|
||||
var found bool
|
||||
val := fmt.Sprint(value.Interface())
|
||||
for i := range opt.Options {
|
||||
if opt.Options[i] == val {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return fmt.Errorf("field %q not in options", val)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateRange(value reflect.Value, opt *fieldOptions) error {
|
||||
var val float64
|
||||
switch v := value.Interface().(type) {
|
||||
case int:
|
||||
val = float64(v)
|
||||
case int8:
|
||||
val = float64(v)
|
||||
case int16:
|
||||
val = float64(v)
|
||||
case int32:
|
||||
val = float64(v)
|
||||
case int64:
|
||||
val = float64(v)
|
||||
case uint:
|
||||
val = float64(v)
|
||||
case uint8:
|
||||
val = float64(v)
|
||||
case uint16:
|
||||
val = float64(v)
|
||||
case uint32:
|
||||
val = float64(v)
|
||||
case uint64:
|
||||
val = float64(v)
|
||||
case float32:
|
||||
val = float64(v)
|
||||
case float64:
|
||||
val = v
|
||||
default:
|
||||
return fmt.Errorf("unknown support type for range %q", value.Type().String())
|
||||
}
|
||||
|
||||
// validates [left, right], [left, right), (left, right], (left, right)
|
||||
if val < opt.Range.left ||
|
||||
(!opt.Range.leftInclude && val == opt.Range.left) ||
|
||||
val > opt.Range.right ||
|
||||
(!opt.Range.rightInclude && val == opt.Range.right) {
|
||||
return fmt.Errorf("%v out of range", value.Interface())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
346
core/mapping/marshaler_test.go
Normal file
346
core/mapping/marshaler_test.go
Normal file
@@ -0,0 +1,346 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMarshal(t *testing.T) {
|
||||
v := struct {
|
||||
Name string `path:"name"`
|
||||
Address string `json:"address,options=[beijing,shanghai]"`
|
||||
Age int `json:"age"`
|
||||
Anonymous bool
|
||||
}{
|
||||
Name: "kevin",
|
||||
Address: "shanghai",
|
||||
Age: 20,
|
||||
Anonymous: true,
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "kevin", m["path"]["name"])
|
||||
assert.Equal(t, "shanghai", m["json"]["address"])
|
||||
assert.Equal(t, 20, m["json"]["age"].(int))
|
||||
assert.True(t, m[emptyTag]["Anonymous"].(bool))
|
||||
}
|
||||
|
||||
func TestMarshal_Ptr(t *testing.T) {
|
||||
v := &struct {
|
||||
Name string `path:"name"`
|
||||
Address string `json:"address,options=[beijing,shanghai]"`
|
||||
Age int `json:"age"`
|
||||
Anonymous bool
|
||||
}{
|
||||
Name: "kevin",
|
||||
Address: "shanghai",
|
||||
Age: 20,
|
||||
Anonymous: true,
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "kevin", m["path"]["name"])
|
||||
assert.Equal(t, "shanghai", m["json"]["address"])
|
||||
assert.Equal(t, 20, m["json"]["age"].(int))
|
||||
assert.True(t, m[emptyTag]["Anonymous"].(bool))
|
||||
}
|
||||
|
||||
func TestMarshal_OptionalPtr(t *testing.T) {
|
||||
var val = 1
|
||||
v := struct {
|
||||
Age *int `json:"age"`
|
||||
}{
|
||||
Age: &val,
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, *m["json"]["age"].(*int))
|
||||
}
|
||||
|
||||
func TestMarshal_OptionalPtrNil(t *testing.T) {
|
||||
v := struct {
|
||||
Age *int `json:"age"`
|
||||
}{}
|
||||
|
||||
_, err := Marshal(v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestMarshal_BadOptions(t *testing.T) {
|
||||
v := struct {
|
||||
Name string `json:"name,options"`
|
||||
}{
|
||||
Name: "kevin",
|
||||
}
|
||||
|
||||
_, err := Marshal(v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestMarshal_NotInOptions(t *testing.T) {
|
||||
v := struct {
|
||||
Name string `json:"name,options=[a,b]"`
|
||||
}{
|
||||
Name: "kevin",
|
||||
}
|
||||
|
||||
_, err := Marshal(v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestMarshal_NotInOptionsOptional(t *testing.T) {
|
||||
v := struct {
|
||||
Name string `json:"name,options=[a,b],optional"`
|
||||
}{}
|
||||
|
||||
_, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestMarshal_NotInOptionsOptionalWrongValue(t *testing.T) {
|
||||
v := struct {
|
||||
Name string `json:"name,options=[a,b],optional"`
|
||||
}{
|
||||
Name: "kevin",
|
||||
}
|
||||
|
||||
_, err := Marshal(v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestMarshal_Nested(t *testing.T) {
|
||||
type address struct {
|
||||
Country string `json:"country"`
|
||||
City string `json:"city"`
|
||||
}
|
||||
v := struct {
|
||||
Name string `json:"name,options=[kevin,wan]"`
|
||||
Address address `json:"address"`
|
||||
}{
|
||||
Name: "kevin",
|
||||
Address: address{
|
||||
Country: "China",
|
||||
City: "Shanghai",
|
||||
},
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "kevin", m["json"]["name"])
|
||||
assert.Equal(t, "China", m["json"]["address"].(address).Country)
|
||||
assert.Equal(t, "Shanghai", m["json"]["address"].(address).City)
|
||||
}
|
||||
|
||||
func TestMarshal_NestedPtr(t *testing.T) {
|
||||
type address struct {
|
||||
Country string `json:"country"`
|
||||
City string `json:"city"`
|
||||
}
|
||||
v := struct {
|
||||
Name string `json:"name,options=[kevin,wan]"`
|
||||
Address *address `json:"address"`
|
||||
}{
|
||||
Name: "kevin",
|
||||
Address: &address{
|
||||
Country: "China",
|
||||
City: "Shanghai",
|
||||
},
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "kevin", m["json"]["name"])
|
||||
assert.Equal(t, "China", m["json"]["address"].(*address).Country)
|
||||
assert.Equal(t, "Shanghai", m["json"]["address"].(*address).City)
|
||||
}
|
||||
|
||||
func TestMarshal_Slice(t *testing.T) {
|
||||
v := struct {
|
||||
Name []string `json:"name"`
|
||||
}{
|
||||
Name: []string{"kevin", "wan"},
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.ElementsMatch(t, []string{"kevin", "wan"}, m["json"]["name"].([]string))
|
||||
}
|
||||
|
||||
func TestMarshal_SliceNil(t *testing.T) {
|
||||
v := struct {
|
||||
Name []string `json:"name"`
|
||||
}{
|
||||
Name: nil,
|
||||
}
|
||||
|
||||
_, err := Marshal(v)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
|
||||
func TestMarshal_Range(t *testing.T) {
|
||||
v := struct {
|
||||
Int int `json:"int,range=[1:3]"`
|
||||
Int8 int8 `json:"int8,range=[1:3)"`
|
||||
Int16 int16 `json:"int16,range=(1:3]"`
|
||||
Int32 int32 `json:"int32,range=(1:3)"`
|
||||
Int64 int64 `json:"int64,range=(1:3)"`
|
||||
Uint uint `json:"uint,range=[1:3]"`
|
||||
Uint8 uint8 `json:"uint8,range=[1:3)"`
|
||||
Uint16 uint16 `json:"uint16,range=(1:3]"`
|
||||
Uint32 uint32 `json:"uint32,range=(1:3)"`
|
||||
Uint64 uint64 `json:"uint64,range=(1:3)"`
|
||||
Float32 float32 `json:"float32,range=(1:3)"`
|
||||
Float64 float64 `json:"float64,range=(1:3)"`
|
||||
}{
|
||||
Int: 1,
|
||||
Int8: 1,
|
||||
Int16: 2,
|
||||
Int32: 2,
|
||||
Int64: 2,
|
||||
Uint: 1,
|
||||
Uint8: 1,
|
||||
Uint16: 2,
|
||||
Uint32: 2,
|
||||
Uint64: 2,
|
||||
Float32: 2,
|
||||
Float64: 2,
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 1, m["json"]["int"].(int))
|
||||
assert.Equal(t, int8(1), m["json"]["int8"].(int8))
|
||||
assert.Equal(t, int16(2), m["json"]["int16"].(int16))
|
||||
assert.Equal(t, int32(2), m["json"]["int32"].(int32))
|
||||
assert.Equal(t, int64(2), m["json"]["int64"].(int64))
|
||||
assert.Equal(t, uint(1), m["json"]["uint"].(uint))
|
||||
assert.Equal(t, uint8(1), m["json"]["uint8"].(uint8))
|
||||
assert.Equal(t, uint16(2), m["json"]["uint16"].(uint16))
|
||||
assert.Equal(t, uint32(2), m["json"]["uint32"].(uint32))
|
||||
assert.Equal(t, uint64(2), m["json"]["uint64"].(uint64))
|
||||
assert.Equal(t, float32(2), m["json"]["float32"].(float32))
|
||||
assert.Equal(t, float64(2), m["json"]["float64"].(float64))
|
||||
}
|
||||
|
||||
func TestMarshal_RangeOut(t *testing.T) {
|
||||
tests := []interface{}{
|
||||
struct {
|
||||
Int int `json:"int,range=[1:3]"`
|
||||
}{
|
||||
Int: 4,
|
||||
},
|
||||
struct {
|
||||
Int int `json:"int,range=(1:3]"`
|
||||
}{
|
||||
Int: 1,
|
||||
},
|
||||
struct {
|
||||
Int int `json:"int,range=[1:3)"`
|
||||
}{
|
||||
Int: 3,
|
||||
},
|
||||
struct {
|
||||
Int int `json:"int,range=(1:3)"`
|
||||
}{
|
||||
Int: 3,
|
||||
},
|
||||
struct {
|
||||
Bool bool `json:"bool,range=(1:3)"`
|
||||
}{
|
||||
Bool: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
_, err := Marshal(test)
|
||||
assert.NotNil(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshal_RangeIllegal(t *testing.T) {
|
||||
tests := []interface{}{
|
||||
struct {
|
||||
Int int `json:"int,range=[3:1]"`
|
||||
}{
|
||||
Int: 2,
|
||||
},
|
||||
struct {
|
||||
Int int `json:"int,range=(3:1]"`
|
||||
}{
|
||||
Int: 2,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
_, err := Marshal(test)
|
||||
assert.Equal(t, err, errNumberRange)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshal_RangeLeftEqualsToRight(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
value interface{}
|
||||
err error
|
||||
}{
|
||||
{
|
||||
name: "left inclusive, right inclusive",
|
||||
value: struct {
|
||||
Int int `json:"int,range=[2:2]"`
|
||||
}{
|
||||
Int: 2,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "left inclusive, right exclusive",
|
||||
value: struct {
|
||||
Int int `json:"int,range=[2:2)"`
|
||||
}{
|
||||
Int: 2,
|
||||
},
|
||||
err: errNumberRange,
|
||||
},
|
||||
{
|
||||
name: "left exclusive, right inclusive",
|
||||
value: struct {
|
||||
Int int `json:"int,range=(2:2]"`
|
||||
}{
|
||||
Int: 2,
|
||||
},
|
||||
err: errNumberRange,
|
||||
},
|
||||
{
|
||||
name: "left exclusive, right exclusive",
|
||||
value: struct {
|
||||
Int int `json:"int,range=(2:2)"`
|
||||
}{
|
||||
Int: 2,
|
||||
},
|
||||
err: errNumberRange,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
test := test
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
_, err := Marshal(test.value)
|
||||
assert.Equal(t, test.err, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarshal_FromString(t *testing.T) {
|
||||
v := struct {
|
||||
Age int `json:"age,string"`
|
||||
}{
|
||||
Age: 10,
|
||||
}
|
||||
|
||||
m, err := Marshal(v)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, "10", m["json"]["age"].(string))
|
||||
}
|
||||
27
core/mapping/tomlunmarshaler.go
Normal file
27
core/mapping/tomlunmarshaler.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/zeromicro/go-zero/internal/encoding"
|
||||
)
|
||||
|
||||
// UnmarshalTomlBytes unmarshals TOML bytes into the given v.
|
||||
func UnmarshalTomlBytes(content []byte, v interface{}, opts ...UnmarshalOption) error {
|
||||
b, err := encoding.TomlToJson(content)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return UnmarshalJsonBytes(b, v, opts...)
|
||||
}
|
||||
|
||||
// UnmarshalTomlReader unmarshals TOML from the given io.Reader into the given v.
|
||||
func UnmarshalTomlReader(r io.Reader, v interface{}, opts ...UnmarshalOption) error {
|
||||
b, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return UnmarshalTomlBytes(b, v, opts...)
|
||||
}
|
||||
49
core/mapping/tomlunmarshaler_test.go
Normal file
49
core/mapping/tomlunmarshaler_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUnmarshalToml(t *testing.T) {
|
||||
const input = `a = "foo"
|
||||
b = 1
|
||||
c = "${FOO}"
|
||||
d = "abcd!@#$112"
|
||||
`
|
||||
var val struct {
|
||||
A string `json:"a"`
|
||||
B int `json:"b"`
|
||||
C string `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
assert.NoError(t, UnmarshalTomlReader(strings.NewReader(input), &val))
|
||||
assert.Equal(t, "foo", val.A)
|
||||
assert.Equal(t, 1, val.B)
|
||||
assert.Equal(t, "${FOO}", val.C)
|
||||
assert.Equal(t, "abcd!@#$112", val.D)
|
||||
}
|
||||
|
||||
func TestUnmarshalTomlErrorToml(t *testing.T) {
|
||||
const input = `foo"
|
||||
b = 1
|
||||
c = "${FOO}"
|
||||
d = "abcd!@#$112"
|
||||
`
|
||||
var val struct {
|
||||
A string `json:"a"`
|
||||
B int `json:"b"`
|
||||
C string `json:"c"`
|
||||
D string `json:"d"`
|
||||
}
|
||||
assert.Error(t, UnmarshalTomlReader(strings.NewReader(input), &val))
|
||||
}
|
||||
|
||||
func TestUnmarshalTomlBadReader(t *testing.T) {
|
||||
var val struct {
|
||||
A string `json:"a"`
|
||||
}
|
||||
assert.Error(t, UnmarshalTomlReader(new(badReader), &val))
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -10,11 +10,14 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/lang"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultOption = "default"
|
||||
envOption = "env"
|
||||
inheritOption = "inherit"
|
||||
stringOption = "string"
|
||||
optionalOption = "optional"
|
||||
optionsOption = "options"
|
||||
@@ -53,7 +56,7 @@ type (
|
||||
|
||||
// Deref dereferences a type, if pointer type, returns its element type.
|
||||
func Deref(t reflect.Type) reflect.Type {
|
||||
if t.Kind() == reflect.Ptr {
|
||||
for t.Kind() == reflect.Ptr {
|
||||
t = t.Elem()
|
||||
}
|
||||
|
||||
@@ -62,22 +65,17 @@ func Deref(t reflect.Type) reflect.Type {
|
||||
|
||||
// Repr returns the string representation of v.
|
||||
func Repr(v interface{}) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
return lang.Repr(v)
|
||||
}
|
||||
|
||||
// if func (v *Type) String() string, we can't use Elem()
|
||||
switch vt := v.(type) {
|
||||
case fmt.Stringer:
|
||||
return vt.String()
|
||||
}
|
||||
// SetValue sets target to value, pointers are processed automatically.
|
||||
func SetValue(tp reflect.Type, value, target reflect.Value) {
|
||||
value.Set(convertTypeOfPtr(tp, target))
|
||||
}
|
||||
|
||||
val := reflect.ValueOf(v)
|
||||
if val.Kind() == reflect.Ptr && !val.IsNil() {
|
||||
val = val.Elem()
|
||||
}
|
||||
|
||||
return reprOfValue(val)
|
||||
// SetMapIndexValue sets target to value at key position, pointers are processed automatically.
|
||||
func SetMapIndexValue(tp reflect.Type, value, key, target reflect.Value) {
|
||||
value.SetMapIndex(key, convertTypeOfPtr(tp, target))
|
||||
}
|
||||
|
||||
// ValidatePtr validates v if it's a valid pointer.
|
||||
@@ -91,10 +89,17 @@ func ValidatePtr(v *reflect.Value) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertType(kind reflect.Kind, str string) (interface{}, error) {
|
||||
func convertTypeFromString(kind reflect.Kind, str string) (interface{}, error) {
|
||||
switch kind {
|
||||
case reflect.Bool:
|
||||
return str == "1" || strings.ToLower(str) == "true", nil
|
||||
switch strings.ToLower(str) {
|
||||
case "1", "true":
|
||||
return true, nil
|
||||
case "0", "false":
|
||||
return false, nil
|
||||
default:
|
||||
return false, errTypeMismatch
|
||||
}
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
intValue, err := strconv.ParseInt(str, 10, 64)
|
||||
if err != nil {
|
||||
@@ -123,6 +128,23 @@ func convertType(kind reflect.Kind, str string) (interface{}, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func convertTypeOfPtr(tp reflect.Type, target reflect.Value) reflect.Value {
|
||||
// keep the original value is a pointer
|
||||
if tp.Kind() == reflect.Ptr && target.CanAddr() {
|
||||
tp = tp.Elem()
|
||||
target = target.Addr()
|
||||
}
|
||||
|
||||
for tp.Kind() == reflect.Ptr {
|
||||
p := reflect.New(target.Type())
|
||||
p.Elem().Set(target)
|
||||
target = p
|
||||
tp = tp.Elem()
|
||||
}
|
||||
|
||||
return target
|
||||
}
|
||||
|
||||
func doParseKeyAndOptions(field reflect.StructField, value string) (string, *fieldOptions, error) {
|
||||
segments := parseSegments(value)
|
||||
key := strings.TrimSpace(segments[0])
|
||||
@@ -143,6 +165,23 @@ func doParseKeyAndOptions(field reflect.StructField, value string) (string, *fie
|
||||
return key, &fieldOpts, nil
|
||||
}
|
||||
|
||||
// ensureValue ensures nested members not to be nil.
|
||||
// If pointer value is nil, set to a new value.
|
||||
func ensureValue(v reflect.Value) reflect.Value {
|
||||
for {
|
||||
if v.Kind() != reflect.Ptr {
|
||||
break
|
||||
}
|
||||
|
||||
if v.IsNil() {
|
||||
v.Set(reflect.New(v.Type().Elem()))
|
||||
}
|
||||
v = v.Elem()
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
|
||||
func implicitValueRequiredStruct(tag string, tp reflect.Type) (bool, error) {
|
||||
numFields := tp.NumField()
|
||||
for i := 0; i < numFields; i++ {
|
||||
@@ -198,8 +237,8 @@ func isRightInclude(b byte) (bool, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func maybeNewValue(field reflect.StructField, value reflect.Value) {
|
||||
if field.Type.Kind() == reflect.Ptr && value.IsNil() {
|
||||
func maybeNewValue(fieldType reflect.Type, value reflect.Value) {
|
||||
if fieldType.Kind() == reflect.Ptr && value.IsNil() {
|
||||
value.Set(reflect.New(value.Type().Elem()))
|
||||
}
|
||||
}
|
||||
@@ -294,6 +333,20 @@ func parseNumberRange(str string) (*numberRange, error) {
|
||||
right = math.MaxFloat64
|
||||
}
|
||||
|
||||
if left > right {
|
||||
return nil, errNumberRange
|
||||
}
|
||||
|
||||
// [2:2] valid
|
||||
// [2:2) invalid
|
||||
// (2:2] invalid
|
||||
// (2:2) invalid
|
||||
if left == right {
|
||||
if !leftInclude || !rightInclude {
|
||||
return nil, errNumberRange
|
||||
}
|
||||
}
|
||||
|
||||
return &numberRange{
|
||||
left: left,
|
||||
leftInclude: leftInclude,
|
||||
@@ -304,6 +357,8 @@ func parseNumberRange(str string) (*numberRange, error) {
|
||||
|
||||
func parseOption(fieldOpts *fieldOptions, fieldName, option string) error {
|
||||
switch {
|
||||
case option == inheritOption:
|
||||
fieldOpts.Inherit = true
|
||||
case option == stringOption:
|
||||
fieldOpts.FromString = true
|
||||
case strings.HasPrefix(option, optionalOption):
|
||||
@@ -320,26 +375,33 @@ func parseOption(fieldOpts *fieldOptions, fieldName, option string) error {
|
||||
case option == optionalOption:
|
||||
fieldOpts.Optional = true
|
||||
case strings.HasPrefix(option, optionsOption):
|
||||
segs := strings.Split(option, equalToken)
|
||||
if len(segs) != 2 {
|
||||
return fmt.Errorf("field %s has wrong options", fieldName)
|
||||
val, err := parseProperty(fieldName, optionsOption, option)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fieldOpts.Options = parseOptions(segs[1])
|
||||
fieldOpts.Options = parseOptions(val)
|
||||
case strings.HasPrefix(option, defaultOption):
|
||||
segs := strings.Split(option, equalToken)
|
||||
if len(segs) != 2 {
|
||||
return fmt.Errorf("field %s has wrong default option", fieldName)
|
||||
val, err := parseProperty(fieldName, defaultOption, option)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fieldOpts.Default = strings.TrimSpace(segs[1])
|
||||
fieldOpts.Default = val
|
||||
case strings.HasPrefix(option, envOption):
|
||||
val, err := parseProperty(fieldName, envOption, option)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fieldOpts.EnvVar = val
|
||||
case strings.HasPrefix(option, rangeOption):
|
||||
segs := strings.Split(option, equalToken)
|
||||
if len(segs) != 2 {
|
||||
return fmt.Errorf("field %s has wrong range", fieldName)
|
||||
val, err := parseProperty(fieldName, rangeOption, option)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
nr, err := parseNumberRange(segs[1])
|
||||
nr, err := parseNumberRange(val)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -364,6 +426,15 @@ func parseOptions(val string) []string {
|
||||
return strings.Split(val, optionSeparator)
|
||||
}
|
||||
|
||||
func parseProperty(field, tag, val string) (string, error) {
|
||||
segs := strings.Split(val, equalToken)
|
||||
if len(segs) != 2 {
|
||||
return "", fmt.Errorf("field %s has wrong %s", field, tag)
|
||||
}
|
||||
|
||||
return strings.TrimSpace(segs[1]), nil
|
||||
}
|
||||
|
||||
func parseSegments(val string) []string {
|
||||
var segments []string
|
||||
var escaped, grouped bool
|
||||
@@ -413,47 +484,6 @@ func parseSegments(val string) []string {
|
||||
return segments
|
||||
}
|
||||
|
||||
func reprOfValue(val reflect.Value) string {
|
||||
switch vt := val.Interface().(type) {
|
||||
case bool:
|
||||
return strconv.FormatBool(vt)
|
||||
case error:
|
||||
return vt.Error()
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(vt), 'f', -1, 32)
|
||||
case float64:
|
||||
return strconv.FormatFloat(vt, 'f', -1, 64)
|
||||
case fmt.Stringer:
|
||||
return vt.String()
|
||||
case int:
|
||||
return strconv.Itoa(vt)
|
||||
case int8:
|
||||
return strconv.Itoa(int(vt))
|
||||
case int16:
|
||||
return strconv.Itoa(int(vt))
|
||||
case int32:
|
||||
return strconv.Itoa(int(vt))
|
||||
case int64:
|
||||
return strconv.FormatInt(vt, 10)
|
||||
case string:
|
||||
return vt
|
||||
case uint:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint8:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint16:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint32:
|
||||
return strconv.FormatUint(uint64(vt), 10)
|
||||
case uint64:
|
||||
return strconv.FormatUint(vt, 10)
|
||||
case []byte:
|
||||
return string(vt)
|
||||
default:
|
||||
return fmt.Sprint(val.Interface())
|
||||
}
|
||||
}
|
||||
|
||||
func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v interface{}) error {
|
||||
switch kind {
|
||||
case reflect.Bool:
|
||||
@@ -473,12 +503,13 @@ func setMatchedPrimitiveValue(kind reflect.Kind, value reflect.Value, v interfac
|
||||
return nil
|
||||
}
|
||||
|
||||
func setValue(kind reflect.Kind, value reflect.Value, str string) error {
|
||||
func setValueFromString(kind reflect.Kind, value reflect.Value, str string) error {
|
||||
if !value.CanSet() {
|
||||
return errValueNotSettable
|
||||
}
|
||||
|
||||
v, err := convertType(kind, str)
|
||||
value = ensureValue(value)
|
||||
v, err := convertTypeFromString(kind, str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -551,7 +582,7 @@ func validateAndSetValue(kind reflect.Kind, value reflect.Value, str string, opt
|
||||
return errValueNotSettable
|
||||
}
|
||||
|
||||
v, err := convertType(kind, str)
|
||||
v, err := convertTypeFromString(kind, str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -592,16 +623,16 @@ func validateNumberRange(fv float64, nr *numberRange) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateValueInOptions(options []string, value interface{}) error {
|
||||
func validateValueInOptions(val interface{}, options []string) error {
|
||||
if len(options) > 0 {
|
||||
switch v := value.(type) {
|
||||
switch v := val.(type) {
|
||||
case string:
|
||||
if !stringx.Contains(options, v) {
|
||||
return fmt.Errorf(`error: value "%s" is not defined in options "%v"`, v, options)
|
||||
}
|
||||
default:
|
||||
if !stringx.Contains(options, Repr(v)) {
|
||||
return fmt.Errorf(`error: value "%v" is not defined in options "%v"`, value, options)
|
||||
return fmt.Errorf(`error: value "%v" is not defined in options "%v"`, val, options)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ type Foo struct {
|
||||
StrWithTagAndOption string `key:"stringwithtag,string"`
|
||||
}
|
||||
|
||||
func TestDeferInt(t *testing.T) {
|
||||
func TestDerefInt(t *testing.T) {
|
||||
i := 1
|
||||
s := "hello"
|
||||
number := struct {
|
||||
@@ -60,6 +60,51 @@ func TestDeferInt(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDerefValInt(t *testing.T) {
|
||||
i := 1
|
||||
s := "hello"
|
||||
number := struct {
|
||||
f float64
|
||||
}{
|
||||
f: 6.4,
|
||||
}
|
||||
cases := []struct {
|
||||
t reflect.Value
|
||||
expect reflect.Kind
|
||||
}{
|
||||
{
|
||||
t: reflect.ValueOf(i),
|
||||
expect: reflect.Int,
|
||||
},
|
||||
{
|
||||
t: reflect.ValueOf(&i),
|
||||
expect: reflect.Int,
|
||||
},
|
||||
{
|
||||
t: reflect.ValueOf(s),
|
||||
expect: reflect.String,
|
||||
},
|
||||
{
|
||||
t: reflect.ValueOf(&s),
|
||||
expect: reflect.String,
|
||||
},
|
||||
{
|
||||
t: reflect.ValueOf(number.f),
|
||||
expect: reflect.Float64,
|
||||
},
|
||||
{
|
||||
t: reflect.ValueOf(&number.f),
|
||||
expect: reflect.Float64,
|
||||
},
|
||||
}
|
||||
|
||||
for _, each := range cases {
|
||||
t.Run(each.t.String(), func(t *testing.T) {
|
||||
assert.Equal(t, each.expect, ensureValue(each.t).Kind())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseKeyAndOptionWithoutTag(t *testing.T) {
|
||||
var foo Foo
|
||||
rte := reflect.TypeOf(&foo).Elem()
|
||||
@@ -192,7 +237,7 @@ func TestValidatePtrWithZeroValue(t *testing.T) {
|
||||
|
||||
func TestSetValueNotSettable(t *testing.T) {
|
||||
var i int
|
||||
assert.NotNil(t, setValue(reflect.Int, reflect.ValueOf(i), "1"))
|
||||
assert.NotNil(t, setValueFromString(reflect.Int, reflect.ValueOf(i), "1"))
|
||||
}
|
||||
|
||||
func TestParseKeyAndOptionsErrors(t *testing.T) {
|
||||
@@ -245,133 +290,9 @@ func TestSetValueFormatErrors(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.kind.String(), func(t *testing.T) {
|
||||
err := setValue(test.kind, test.target, test.value)
|
||||
err := setValueFromString(test.kind, test.target, test.value)
|
||||
assert.NotEqual(t, errValueNotSettable, err)
|
||||
assert.NotNil(t, err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepr(t *testing.T) {
|
||||
var (
|
||||
f32 float32 = 1.1
|
||||
f64 = 2.2
|
||||
i8 int8 = 1
|
||||
i16 int16 = 2
|
||||
i32 int32 = 3
|
||||
i64 int64 = 4
|
||||
u8 uint8 = 5
|
||||
u16 uint16 = 6
|
||||
u32 uint32 = 7
|
||||
u64 uint64 = 8
|
||||
)
|
||||
tests := []struct {
|
||||
v interface{}
|
||||
expect string
|
||||
}{
|
||||
{
|
||||
nil,
|
||||
"",
|
||||
},
|
||||
{
|
||||
mockStringable{},
|
||||
"mocked",
|
||||
},
|
||||
{
|
||||
new(mockStringable),
|
||||
"mocked",
|
||||
},
|
||||
{
|
||||
newMockPtr(),
|
||||
"mockptr",
|
||||
},
|
||||
{
|
||||
&mockOpacity{
|
||||
val: 1,
|
||||
},
|
||||
"{1}",
|
||||
},
|
||||
{
|
||||
true,
|
||||
"true",
|
||||
},
|
||||
{
|
||||
false,
|
||||
"false",
|
||||
},
|
||||
{
|
||||
f32,
|
||||
"1.1",
|
||||
},
|
||||
{
|
||||
f64,
|
||||
"2.2",
|
||||
},
|
||||
{
|
||||
i8,
|
||||
"1",
|
||||
},
|
||||
{
|
||||
i16,
|
||||
"2",
|
||||
},
|
||||
{
|
||||
i32,
|
||||
"3",
|
||||
},
|
||||
{
|
||||
i64,
|
||||
"4",
|
||||
},
|
||||
{
|
||||
u8,
|
||||
"5",
|
||||
},
|
||||
{
|
||||
u16,
|
||||
"6",
|
||||
},
|
||||
{
|
||||
u32,
|
||||
"7",
|
||||
},
|
||||
{
|
||||
u64,
|
||||
"8",
|
||||
},
|
||||
{
|
||||
[]byte(`abcd`),
|
||||
"abcd",
|
||||
},
|
||||
{
|
||||
mockOpacity{val: 1},
|
||||
"{1}",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.expect, func(t *testing.T) {
|
||||
assert.Equal(t, test.expect, Repr(test.v))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type mockStringable struct{}
|
||||
|
||||
func (m mockStringable) String() string {
|
||||
return "mocked"
|
||||
}
|
||||
|
||||
type mockPtr struct{}
|
||||
|
||||
func newMockPtr() *mockPtr {
|
||||
return new(mockPtr)
|
||||
}
|
||||
|
||||
func (m *mockPtr) String() string {
|
||||
return "mockptr"
|
||||
}
|
||||
|
||||
type mockOpacity struct {
|
||||
val int
|
||||
}
|
||||
|
||||
@@ -7,12 +7,106 @@ type (
|
||||
Value(key string) (interface{}, bool)
|
||||
}
|
||||
|
||||
// A MapValuer is a map that can use Value method to get values with given keys.
|
||||
MapValuer map[string]interface{}
|
||||
// A valuerWithParent defines a node that has a parent node.
|
||||
valuerWithParent interface {
|
||||
Valuer
|
||||
// Parent get the parent valuer for current node.
|
||||
Parent() valuerWithParent
|
||||
}
|
||||
|
||||
// A node is a map that can use Value method to get values with given keys.
|
||||
node struct {
|
||||
current Valuer
|
||||
parent valuerWithParent
|
||||
}
|
||||
|
||||
// A valueWithParent is used to wrap the value with its parent.
|
||||
valueWithParent struct {
|
||||
value interface{}
|
||||
parent valuerWithParent
|
||||
}
|
||||
|
||||
// mapValuer is a type for map to meet the Valuer interface.
|
||||
mapValuer map[string]interface{}
|
||||
// simpleValuer is a type to get value from current node.
|
||||
simpleValuer node
|
||||
// recursiveValuer is a type to get the value recursively from current and parent nodes.
|
||||
recursiveValuer node
|
||||
)
|
||||
|
||||
// Value gets the value associated with the given key from mv.
|
||||
func (mv MapValuer) Value(key string) (interface{}, bool) {
|
||||
// Value gets the value assciated with the given key from mv.
|
||||
func (mv mapValuer) Value(key string) (interface{}, bool) {
|
||||
v, ok := mv[key]
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// Value gets the value associated with the given key from sv.
|
||||
func (sv simpleValuer) Value(key string) (interface{}, bool) {
|
||||
v, ok := sv.current.Value(key)
|
||||
return v, ok
|
||||
}
|
||||
|
||||
// Parent get the parent valuer from sv.
|
||||
func (sv simpleValuer) Parent() valuerWithParent {
|
||||
if sv.parent == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return recursiveValuer{
|
||||
current: sv.parent,
|
||||
parent: sv.parent.Parent(),
|
||||
}
|
||||
}
|
||||
|
||||
// Value gets the value associated with the given key from rv,
|
||||
// and it will inherit the value from parent nodes.
|
||||
func (rv recursiveValuer) Value(key string) (interface{}, bool) {
|
||||
val, ok := rv.current.Value(key)
|
||||
if !ok {
|
||||
if parent := rv.Parent(); parent != nil {
|
||||
return parent.Value(key)
|
||||
}
|
||||
|
||||
return nil, false
|
||||
}
|
||||
|
||||
vm, ok := val.(map[string]interface{})
|
||||
if !ok {
|
||||
return val, true
|
||||
}
|
||||
|
||||
parent := rv.Parent()
|
||||
if parent == nil {
|
||||
return val, true
|
||||
}
|
||||
|
||||
pv, ok := parent.Value(key)
|
||||
if !ok {
|
||||
return val, true
|
||||
}
|
||||
|
||||
pm, ok := pv.(map[string]interface{})
|
||||
if !ok {
|
||||
return val, true
|
||||
}
|
||||
|
||||
for k, v := range pm {
|
||||
if _, ok := vm[k]; !ok {
|
||||
vm[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
return vm, true
|
||||
}
|
||||
|
||||
// Parent get the parent valuer from rv.
|
||||
func (rv recursiveValuer) Parent() valuerWithParent {
|
||||
if rv.parent == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return recursiveValuer{
|
||||
current: rv.parent,
|
||||
parent: rv.parent.Parent(),
|
||||
}
|
||||
}
|
||||
|
||||
57
core/mapping/valuer_test.go
Normal file
57
core/mapping/valuer_test.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package mapping
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestMapValuerWithInherit_Value(t *testing.T) {
|
||||
input := map[string]interface{}{
|
||||
"discovery": map[string]interface{}{
|
||||
"host": "localhost",
|
||||
"port": 8080,
|
||||
},
|
||||
"component": map[string]interface{}{
|
||||
"name": "test",
|
||||
},
|
||||
}
|
||||
valuer := recursiveValuer{
|
||||
current: mapValuer(input["component"].(map[string]interface{})),
|
||||
parent: simpleValuer{
|
||||
current: mapValuer(input),
|
||||
},
|
||||
}
|
||||
|
||||
val, ok := valuer.Value("discovery")
|
||||
assert.True(t, ok)
|
||||
|
||||
m, ok := val.(map[string]interface{})
|
||||
assert.True(t, ok)
|
||||
assert.Equal(t, "localhost", m["host"])
|
||||
assert.Equal(t, 8080, m["port"])
|
||||
}
|
||||
|
||||
func TestRecursiveValuer_Value(t *testing.T) {
|
||||
input := map[string]interface{}{
|
||||
"component": map[string]interface{}{
|
||||
"name": "test",
|
||||
"foo": map[string]interface{}{
|
||||
"bar": "baz",
|
||||
},
|
||||
},
|
||||
"foo": "value",
|
||||
}
|
||||
valuer := recursiveValuer{
|
||||
current: mapValuer(input["component"].(map[string]interface{})),
|
||||
parent: simpleValuer{
|
||||
current: mapValuer(input),
|
||||
},
|
||||
}
|
||||
|
||||
val, ok := valuer.Value("foo")
|
||||
assert.True(t, ok)
|
||||
assert.EqualValues(t, map[string]interface{}{
|
||||
"bar": "baz",
|
||||
}, val)
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user