Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Hepto
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ACIDES
Hepto
Commits
0a7901da
Commit
0a7901da
authored
2 years ago
by
kaiyou
Browse files
Options
Downloads
Patches
Plain Diff
Do not show logrus stacktraces
parent
acc77233
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cmd/hepto/logs.go
+13
-26
13 additions, 26 deletions
cmd/hepto/logs.go
with
13 additions
and
26 deletions
cmd/hepto/logs.go
+
13
−
26
View file @
0a7901da
...
...
@@ -2,7 +2,6 @@ package hepto
import
(
"io/ioutil"
"runtime"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
...
...
@@ -11,6 +10,10 @@ import (
// Much of the following code is copied from https://github.com/Sytten/logrus-zap-hook
var
levelMap
=
map
[
logrus
.
Level
]
zapcore
.
Level
{
logrus
.
PanicLevel
:
zapcore
.
PanicLevel
,
}
func
NewLogger
(
logLevel
int
)
(
*
zap
.
Logger
,
error
)
{
// Create the logger
logConfig
:=
zap
.
NewProductionConfig
()
...
...
@@ -21,15 +24,17 @@ func NewLogger(logLevel int) (*zap.Logger, error) {
if
err
!=
nil
{
return
nil
,
err
}
// Intercept logrus logs
hook
:=
&
ZapHook
{
logger
}
// Intercept logrus logs (7 is the magic number of stack frames
// in logrus hook infrastructures)
hookLogger
:=
logger
.
WithOptions
(
zap
.
AddCallerSkip
(
7
))
hook
:=
&
ZapHook
{
hookLogger
}
logrus
.
SetOutput
(
ioutil
.
Discard
)
logrus
.
StandardLogger
()
.
Hooks
.
Add
(
hook
)
return
logger
,
nil
}
type
ZapHook
struct
{
Logger
*
zap
.
Logger
*
zap
.
Logger
}
func
(
hook
*
ZapHook
)
Fire
(
entry
*
logrus
.
Entry
)
error
{
...
...
@@ -41,32 +46,14 @@ func (hook *ZapHook) Fire(entry *logrus.Entry) error {
fields
=
append
(
fields
,
zap
.
Any
(
key
,
value
))
}
}
switch
entry
.
Level
{
case
logrus
.
PanicLevel
:
hook
.
Write
(
zapcore
.
PanicLevel
,
entry
.
Message
,
fields
,
entry
.
Caller
)
case
logrus
.
FatalLevel
:
hook
.
Write
(
zapcore
.
FatalLevel
,
entry
.
Message
,
fields
,
entry
.
Caller
)
case
logrus
.
ErrorLevel
:
hook
.
Write
(
zapcore
.
ErrorLevel
,
entry
.
Message
,
fields
,
entry
.
Caller
)
case
logrus
.
WarnLevel
:
hook
.
Write
(
zapcore
.
WarnLevel
,
entry
.
Message
,
fields
,
entry
.
Caller
)
case
logrus
.
InfoLevel
:
hook
.
Write
(
zapcore
.
InfoLevel
,
entry
.
Message
,
fields
,
entry
.
Caller
)
case
logrus
.
DebugLevel
,
logrus
.
TraceLevel
:
hook
.
Write
(
zapcore
.
DebugLevel
,
entry
.
Message
,
fields
,
entry
.
Caller
)
level
,
ok
:=
levelMap
[
entry
.
Level
]
if
!
ok
{
level
=
zapcore
.
InfoLevel
}
hook
.
Logger
.
Log
(
level
,
entry
.
Message
,
fields
...
)
return
nil
}
func
(
hook
*
ZapHook
)
Write
(
lvl
zapcore
.
Level
,
msg
string
,
fields
[]
zap
.
Field
,
caller
*
runtime
.
Frame
)
{
if
ce
:=
hook
.
Logger
.
Check
(
lvl
,
msg
);
ce
!=
nil
{
if
caller
!=
nil
{
ce
.
Caller
=
zapcore
.
NewEntryCaller
(
caller
.
PC
,
caller
.
File
,
caller
.
Line
,
caller
.
PC
!=
0
)
}
ce
.
Write
(
fields
...
)
}
}
func
(
hook
*
ZapHook
)
Levels
()
[]
logrus
.
Level
{
return
logrus
.
AllLevels
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment