How do I set the log directory of glog from code

Variable logDir really exist in glog package https://github.com/golang/glog/blob/master/glog_file.go#L41 It just not exported. So you can change it in source of your instance of glog. It's little hacky, but not hard.


This is a hack I have seen lying around: set the flags in code. Also very good for setting log levels from code.

package main

import (
    "flag"

    "github.com/golang/glog"
)

func main() {
    flag.Parse()
    glog.Info("hi_a")
    flag.Lookup("logtostderr").Value.Set("true")
    glog.Info("hi_b")

    flag.Lookup("log_dir").Value.Set("/path/to/log/dir")

    glog.V(4).Info("v4a")
    flag.Lookup("v").Value.Set("10")
    glog.V(4).Info("v4b")
    //etc.    
}

>>> hi_b
>>> v4b

The glog package is a dump of the log package used inside Google. Google configures logging using command line flags and that's what the package supports.

You should look to another log package or fork the package if you want to set the directory from code.

Tags:

Go

Glog