Jun 9, 2014 - 1 minutes

A useful logger in Go

Small function that will print out useful information when invoked:

 1 func logMsg(msg string) {
 2 	pc, _, _, _ := runtime.Caller(1)
 3 	caller := runtime.FuncForPC(pc).Name()
 4 	_, file, line, _ := runtime.Caller(0)
 5 	sp := strings.Split(file, "/")
 6 	short_path := sp[len(sp)-2 : len(sp)]
 7 	path_line := fmt.Sprintf("[%s/%s:%d]", short_path[0], short_path[1], line)
 8 	log_string := fmt.Sprintf("[%s]%s %s:: %s", time.Now(), path_line, caller, msg)
 9 	fmt.Println(log_string)
10 }

Sample output:

[2014-06-10 01:38:45.812215998 +0000 UTC][src/trex-client.go:15]{main.main}:: checking jobs - finish
[2014-06-10 01:38:47.329650331 +0000 UTC][src/trex-client.go:15]{main.main}:: building package list - start
comments powered by Disqus