How do I set the log directory of glog from code

zhou lin picture zhou lin · Jan 29, 2015 · Viewed 10.6k times · Source

Does anyone know how to set/modify the log dir in the golang source code? I want to set the log dir in the soure code, instead of -log_dir= in the cmdline

Answer

RickyA picture RickyA · Jan 29, 2015

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