Skip to content

Instantly share code, notes, and snippets.

@jeanbza
Last active August 29, 2015 14:00
Show Gist options
  • Save jeanbza/11406719 to your computer and use it in GitHub Desktop.
Save jeanbza/11406719 to your computer and use it in GitHub Desktop.
Glog weirdness (log_dir)

See below a basic glog usage example run in bash on a mac:

// (temp.go)

package main

import (
    "log"
    "github.com/golang/glog"
)

func main() {
    log.Println("Testing glog")
    glog.Infoln("Testing glog")
    
    glog.Flush()
}

Problem 1: os.Tempdir sends to /private/var/gobbledeegook (possibly a Darwin problem, not a go one)

If I ran this as ./temp, I expect to see "Testing glog" sent to /tmp/temp-SomeMachine.log.INFO.SomeTimestamp (or rather, /private/tmp/filename.INFO since /tmp is a symlink). Howeever, let's see what actually happens:

[jeand@Jeans-MacBook-Pro-2:~/go/src]$ go build temp.go
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ ./temp
2014/04/29 13:16:42 Testing glog
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ sudo find / | grep temp.INFO
/private/var/folders/g4/b1_xxcld3y7cl7_lrmzr1dmc0015rx/T/temp.INFO
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ ls /private/var/folders/g4/b1_xxcld3y7cl7_lrmzr1dmc0015rx/T/
temp.INFO
temp.Jeans-MacBook-Pro-2.jeand.log.INFO.20140429-131642.9055 <-- there it is!
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ ls -l /tmp <-- sanity check that /tmp isn't symlinked to /private/var
lrwxr-xr-x@ 1 root  wheel  11 Oct 25  2013 /tmp -> private/tmp

Ok, so that's weird, but maybe it's a Mac thing. Let's move on.

Problem 2: -log_dir="" doesn't seem to affect log location

As documented at https://github.com/golang/glog/blob/master/glog.go, adding -log_dir="/Users/jadekler/go/src/" to my script execution should cause logs to be written at ~/go/src. Here's what actually happens:

[jeand@Jeans-MacBook-Pro-2:~/go/src]$ ls /Users/jadekler/go/src
git-go-d3-concertsap	git-go-websiteskeleton	temp
git-go-jeansite		git-misc		temp.go
git-go-practice		github.com		test
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ go build temp.go
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ ./temp -log_dir=/Users/jadekler/go/src
2014/04/29 13:23:18 Testing glog
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ ls /private/var/folders/g4/b1_xxcld3y7cl7_lrmzr1dmc0015rx/T/
temp.INFO
temp.Jeans-MacBook-Pro-2.jeand.log.INFO.20140429-131642.9055
temp.Jeans-MacBook-Pro-2.jeand.log.INFO.20140429-132318.9088 <-- No change after executing with -log_dir=/path/to/dir
[jeand@Jeans-MacBook-Pro-2:~/go/src]$ more /private/var/folders/g4/b1_xxcld3y7cl7_lrmzr1dmc0015rx/T/temp.Jeans-MacBook-Pro-2.jeand.log.INFO.20140429-132318.9088 
Log file created at: 2014/04/29 13:23:18
Running on machine: Jeans-MacBook-Pro-2
Binary: Built with gc go1.2 for darwin/386
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg
I0429 13:23:18.817073 09088 temp.go:10] Testing glog with log_dir=/Users/jadekler/go/src <-- Sanity check that this is the right one
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment