-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ebb4e7
commit 82dbf65
Showing
1 changed file
with
48 additions
and
0 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,48 @@ | ||
package logtext | ||
|
||
// A Logrus hook that adds filename/line number/stack trace to our log outputs | ||
|
||
import ( | ||
"github.com/Sirupsen/logrus" | ||
"runtime" | ||
) | ||
|
||
// Log depth is how many levels to ascend to find where the actual log call occurred | ||
// while debugOnly sets whether or not stack traces should be printed outside of | ||
// debug prints | ||
type Logtext struct{ | ||
Formatter logrus.Formatter | ||
LogDepth int | ||
DebugOnly bool | ||
} | ||
|
||
func NewLogtext(formatter logrus.Formatter, debugOnly bool) *Logtext { | ||
return &Logtext{ | ||
LogDepth: 4, | ||
Formatter: formatter, | ||
DebugOnly: debugOnly, | ||
} | ||
} | ||
|
||
// Creates a hook to be added to an instance of logger. This is called with | ||
func (hook *Logtext) Format(entry *logrus.Entry) ([]byte, error) { | ||
|
||
if _, file, line, ok := runtime.Caller(hook.LogDepth); ok { | ||
entry.Data["line"] = line | ||
entry.Data["file"] = file | ||
} | ||
|
||
if !hook.DebugOnly || entry.Level == logrus.DebugLevel { | ||
stack := getTrace() | ||
entry.Data["stack"] = stack | ||
} | ||
|
||
return hook.Formatter.Format(entry) | ||
} | ||
|
||
// handles getting the stack trace and returns it as a string | ||
func getTrace() string { | ||
stack := make([]byte, 2048) | ||
size := runtime.Stack(stack, false) | ||
return string(stack[:size]) | ||
} |