Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Ginx/gcore/logger.go at main · 014-code/Ginx · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
014-code
Ginx
Repository navigation
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
Ginx
/
gcore
/
logger.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
173 lines (151 loc) · 4.17 KB
main
Breadcrumbs
Ginx
/
gcore
/
logger.go
Copy path
Top
File metadata and controls
Code
Blame
173 lines (151 loc) · 4.17 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package
gcore
import
(
"encoding/json"
"io"
"os"
"sync"
"sync/atomic"
"time"
)
// LogLevel 表示日志级别,级别越小越优先输出。
type
LogLevel
uint8
const
(
// LogLevelDebug 用于开发调试信息。
LogLevelDebug
LogLevel
=
iota
// LogLevelInfo 用于正常运行信息。
LogLevelInfo
// LogLevelWarn 用于可恢复的问题或风险提示。
LogLevelWarn
// LogLevelError 用于当前操作失败的信息。
LogLevelError
)
// String 返回日志级别名称。
func
(
ll
LogLevel
)
String
()
string
{
switch
ll
{
case
LogLevelDebug
:
return
"DEBUG"
case
LogLevelInfo
:
return
"INFO"
case
LogLevelWarn
:
return
"WARN"
case
LogLevelError
:
return
"ERROR"
default
:
return
"UNKNOWN"
}
}
// LogField 表示一组结构化日志字段。
type
LogField
struct
{
Key
string
Value
interface
{}
}
// Field 创建一个结构化日志字段。
func
Field
(
key
string
,
value
interface
{})
LogField
{
return
LogField
{
Key
:
key
,
Value
:
value
}
}
// Logger 是一个线程安全的 JSON 行日志记录器。
//
// 每次日志调用输出一行 JSON,方便本地调试,也方便后续接入日志采集系统。
// Logger 不负责日志文件轮转和远程传输,这些能力应交给部署环境或业务层。
type
Logger
struct
{
service
string
output
io.
Writer
minLevel
atomic.
Int32
writeLock
sync.
Mutex
baseFields
map
[
string
]
interface
{}
}
type
logRecord
struct
{
Timestamp
string
`json:"timestamp"`
Level
string
`json:"level"`
Service
string
`json:"service,omitempty"`
Message
string
`json:"message"`
Fields
map
[
string
]
interface
{}
`json:"fields,omitempty"`
}
// NewLogger 创建一个输出到指定 Writer 的日志记录器。
// output 传入 nil 时默认写到标准输出。
func
NewLogger
(
service
string
,
output
io.
Writer
)
*
Logger
{
if
output
==
nil
{
output
=
os
.
Stdout
}
logger
:=
&
Logger
{
service
:
service
,
output
:
output
,
baseFields
:
make
(
map
[
string
]
interface
{}),
}
logger
.
minLevel
.
Store
(
int32
(
LogLevelInfo
))
return
logger
}
var
defaultLogger
=
NewLogger
(
"ginx"
,
os
.
Stdout
)
// DefaultLogger 返回框架默认日志记录器。
func
DefaultLogger
()
*
Logger
{
return
defaultLogger
}
// SetLevel 设置最低输出级别。
func
(
l
*
Logger
)
SetLevel
(
level
LogLevel
) {
l
.
minLevel
.
Store
(
int32
(
level
))
}
// GetLevel 获取当前最低输出级别。
func
(
l
*
Logger
)
GetLevel
()
LogLevel
{
return
LogLevel
(
l
.
minLevel
.
Load
())
}
// With 创建一个带固定字段的子日志记录器。
func
(
l
*
Logger
)
With
(
fields
...
LogField
)
*
Logger
{
child
:=
NewLogger
(
l
.
service
,
l
.
output
)
child
.
SetLevel
(
l
.
GetLevel
())
for
key
,
value
:=
range
l
.
baseFields
{
child
.
baseFields
[
key
]
=
value
}
for
_
,
field
:=
range
fields
{
if
field
.
Key
!=
""
{
child
.
baseFields
[
field
.
Key
]
=
field
.
Value
}
}
return
child
}
// Log 输出一条结构化日志。
func
(
l
*
Logger
)
Log
(
level
LogLevel
,
message
string
,
fields
...
LogField
)
error
{
if
level
<
l
.
GetLevel
() {
return
nil
}
allFields
:=
make
(
map
[
string
]
interface
{},
len
(
l
.
baseFields
)
+
len
(
fields
))
for
key
,
value
:=
range
l
.
baseFields
{
allFields
[
key
]
=
value
}
for
_
,
field
:=
range
fields
{
if
field
.
Key
!=
""
{
allFields
[
field
.
Key
]
=
field
.
Value
}
}
record
:=
logRecord
{
Timestamp
:
time
.
Now
().
UTC
().
Format
(
time
.
RFC3339Nano
),
Level
:
level
.
String
(),
Service
:
l
.
service
,
Message
:
message
,
Fields
:
allFields
,
}
data
,
err
:=
json
.
Marshal
(
record
)
if
err
!=
nil
{
return
err
}
data
=
append
(
data
,
'\n'
)
l
.
writeLock
.
Lock
()
defer
l
.
writeLock
.
Unlock
()
_
,
err
=
l
.
output
.
Write
(
data
)
return
err
}
// Debug 输出调试日志。
func
(
l
*
Logger
)
Debug
(
message
string
,
fields
...
LogField
)
error
{
return
l
.
Log
(
LogLevelDebug
,
message
,
fields
...
)
}
// Info 输出普通运行日志。
func
(
l
*
Logger
)
Info
(
message
string
,
fields
...
LogField
)
error
{
return
l
.
Log
(
LogLevelInfo
,
message
,
fields
...
)
}
// Warn 输出风险提示日志。
func
(
l
*
Logger
)
Warn
(
message
string
,
fields
...
LogField
)
error
{
return
l
.
Log
(
LogLevelWarn
,
message
,
fields
...
)
}
// Error 输出错误日志。
func
(
l
*
Logger
)
Error
(
message
string
,
fields
...
LogField
)
error
{
return
l
.
Log
(
LogLevelError
,
message
,
fields
...
)
}
You can’t perform that action at this time.