Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hujiebin
hilo-common
Commits
d597b804
Commit
d597b804
authored
Jul 29, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:需要手动开启redis的hook
parent
62f557af
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
3 deletions
+40
-3
log.go
mylogrus/log.go
+33
-0
redis.go
resource/redisCli/redis.go
+6
-2
redis_hook.go
resource/redisCli/redis_hook.go
+1
-1
No files found.
mylogrus/log.go
View file @
d597b804
...
@@ -14,6 +14,7 @@ const logDir = "/var/log/hilo/"
...
@@ -14,6 +14,7 @@ const logDir = "/var/log/hilo/"
var
filenamePrefix
string
var
filenamePrefix
string
var
MyLog
=
logrus
.
New
()
var
MyLog
=
logrus
.
New
()
var
RedisLog
=
logrus
.
New
()
func
Info
(
v
interface
{})
{
func
Info
(
v
interface
{})
{
MyLog
.
Info
(
""
)
MyLog
.
Info
(
""
)
...
@@ -44,6 +45,23 @@ func init() {
...
@@ -44,6 +45,23 @@ func init() {
MyLog
.
SetReportCaller
(
true
)
MyLog
.
SetReportCaller
(
true
)
}
}
func
InitRedisLog
()
{
RedisLog
.
SetOutput
(
os
.
Stdout
)
RedisLog
.
SetFormatter
(
&
logrus
.
TextFormatter
{
ForceQuote
:
false
,
DisableQuote
:
true
,
TimestampFormat
:
"2006-01-02 15:04:05.000"
,
FullTimestamp
:
true
,
})
hook
:=
lfshook
.
NewHook
(
lfshook
.
WriterMap
{
logrus
.
InfoLevel
:
GetRedisLog
(),
},
&
logrus
.
TextFormatter
{
ForceQuote
:
false
,
DisableQuote
:
true
,
TimestampFormat
:
time
.
RFC3339Nano
})
RedisLog
.
AddHook
(
hook
)
RedisLog
.
SetLevel
(
logrus
.
InfoLevel
)
RedisLog
.
SetReportCaller
(
true
)
}
func
GetInfoLog
()
io
.
Writer
{
func
GetInfoLog
()
io
.
Writer
{
return
getLevelWrite
(
logrus
.
InfoLevel
)
return
getLevelWrite
(
logrus
.
InfoLevel
)
}
}
...
@@ -91,3 +109,18 @@ func GetSqlLog() io.Writer {
...
@@ -91,3 +109,18 @@ func GetSqlLog() io.Writer {
}
}
return
writer
return
writer
}
}
func
GetRedisLog
()
io
.
Writer
{
var
name
string
=
"redis.log"
name
=
filenamePrefix
+
name
writer
,
err
:=
rotatelogs
.
New
(
name
+
".%Y%m%d%H"
,
rotatelogs
.
WithLinkName
(
name
),
// 生成软链,指向最新日志文件
rotatelogs
.
WithMaxAge
(
7
*
24
*
time
.
Hour
),
// 文件最大保存时间
rotatelogs
.
WithRotationTime
(
time
.
Hour
),
// 日志切割时间间隔
)
if
err
!=
nil
{
MyLog
.
Fatal
(
"Failed to create log file:"
,
err
.
Error
())
}
return
writer
}
resource/redisCli/redis.go
View file @
d597b804
...
@@ -26,10 +26,14 @@ func init() {
...
@@ -26,10 +26,14 @@ func init() {
}
else
{
}
else
{
mylogrus
.
MyLog
.
Info
(
"redis db0 connection success - "
,
pong
)
mylogrus
.
MyLog
.
Info
(
"redis db0 connection success - "
,
pong
)
}
}
// log hook
RedisClient
.
AddHook
(
redisHook
{})
}
}
func
GetRedis
()
*
redis
.
Client
{
func
GetRedis
()
*
redis
.
Client
{
return
RedisClient
return
RedisClient
}
}
func
AddRedisHook
()
{
// log hook
RedisClient
.
AddHook
(
redisHook
{})
mylogrus
.
InitRedisLog
()
}
resource/redisCli/redis_hook.go
View file @
d597b804
...
@@ -25,7 +25,7 @@ func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
...
@@ -25,7 +25,7 @@ func (redisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error {
if
s
,
ok
:=
start
.
(
time
.
Time
);
ok
{
if
s
,
ok
:=
start
.
(
time
.
Time
);
ok
{
cost
=
time
.
Now
()
.
Sub
(
s
)
.
Milliseconds
()
cost
=
time
.
Now
()
.
Sub
(
s
)
.
Milliseconds
()
}
}
mylogrus
.
My
Log
.
Infof
(
"redis cmd: <%s>,err:%v traceId:%v,userId:%v,cost:%v ms"
,
cmd
.
String
(),
cmd
.
Err
(),
traceId
,
userId
,
cost
)
mylogrus
.
Redis
Log
.
Infof
(
"redis cmd: <%s>,err:%v traceId:%v,userId:%v,cost:%v ms"
,
cmd
.
String
(),
cmd
.
Err
(),
traceId
,
userId
,
cost
)
return
nil
return
nil
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment