Skip to content

Instantly share code, notes, and snippets.

@flowerinthenight
Last active August 26, 2016 19:07
Show Gist options
  • Save flowerinthenight/8da2984e90dd4a3a26ff645e6a37f275 to your computer and use it in GitHub Desktop.
Save flowerinthenight/8da2984e90dd4a3a26ff645e6a37f275 to your computer and use it in GitHub Desktop.
And function name prefix to logs in Go.
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