This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Keep main.go as simple as possiable - Add `BYTOM_DEBUG` environment variable to set the log level to DEBUG
- Loading branch information
1 parent
83db836
commit 66fb5bb
Showing
2 changed files
with
44 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
log "github.com/sirupsen/logrus" | ||
"os" | ||
"path" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
type ContextHook struct{} | ||
|
||
func (hook ContextHook) Levels() []log.Level { | ||
return log.AllLevels | ||
} | ||
|
||
func (hook ContextHook) Fire(entry *log.Entry) error { | ||
pc := make([]uintptr, 3, 3) | ||
cnt := runtime.Callers(6, pc) | ||
|
||
for i := 0; i < cnt; i++ { | ||
fu := runtime.FuncForPC(pc[i] - 1) | ||
name := fu.Name() | ||
if !strings.Contains(name, "github.com/Sirupsen/log") { | ||
file, line := fu.FileLine(pc[i] - 1) | ||
entry.Data["file"] = path.Base(file) | ||
entry.Data["func"] = path.Base(name) | ||
entry.Data["line"] = line | ||
break | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
log.SetFormatter(&log.TextFormatter{FullTimestamp: true}) | ||
|
||
// If environment variable BYTOM_DEBUG is not empty, | ||
// then add the hook to logrus and set the log level to DEBUG | ||
if os.Getenv("BYTOM_DEBUG") != "" { | ||
log.AddHook(ContextHook{}) | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters