Last active
August 26, 2016 19:07
-
-
Save flowerinthenight/8da2984e90dd4a3a26ff645e6a37f275 to your computer and use it in GitHub Desktop.
And function name prefix to logs in Go.
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
package main | |
import ( | |
"fmt" | |
"log" | |
"regexp" | |
"runtime" | |
) | |
func traceln(v ...interface{}) { | |
pc, _, _, _ := runtime.Caller(1) | |
fn := runtime.FuncForPC(pc) | |
fno := regexp.MustCompile(`^.*\.(.*)$`) | |
fnName := fno.ReplaceAllString(fn.Name(), "$1") | |
m := fmt.Sprintln(v...) | |
log.Println("["+fnName+"]", m) | |
} | |
func main() { | |
traceln("Hello world!", 100, true, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment