glog weirdness

369 views
Skip to first unread message

Jean de Klerk

unread,
Apr 28, 2014, 9:16:59 PM4/28/14
to golan...@googlegroups.com
I decided to post a new thread since the last reply on the old one is late 2013, and I couldn't find any later than Jan 2014 (on unrelated subjects).

So, seeing some strange behavior. I'm running bash on a mac, btw.

1. Just running my script with glog.Infoln("Prepare to repel boarders") as ./main doesn't seem to write anywhere (the docs refer a 'default' file location but don't specify)
  1.a I did a find / | grep main.INFO and found these /private/var/folders/g4/b1_xxcld3y7cl7_lrmzr1dmc0015rx/T/. Is this what the docs mean by the 'default' file location?
2. Running my glog.Infoln script as ./main -log_dir="/Users/jadekler/err" seems to have no effect (after chmoding and everything)
3. Adding $TMP="/Users/jadekler/err" as an environment variable and running ./main seems to work, but is undocumented behavior in the docs (found in a discussion thread). It may be that this is some sort of normal logging behavior that I am unaware of (I am certainly no logging expert :) )

Anyone have any ideas? Am I expecting the wrong behavior with -log_dir? Is setting $TMP some sort of unspoken, known logging thing?

Thanks for any tips!
Jean

Chris Hines

unread,
Apr 28, 2014, 11:14:13 PM4/28/14
to golan...@googlegroups.com
It sounds like your program is pretty simple, but maybe it is missing something. Please share the program source so we can see better what is happening. Then we'll probably be able to help you figure it out.

Jan Mercl

unread,
Apr 29, 2014, 4:18:09 AM4/29/14
to Jean de Klerk, golang-nuts
On Tue, Apr 29, 2014 at 3:16 AM, Jean de Klerk <jade...@gmail.com> wrote:
> I decided to post a new thread since the last reply on the old one is late
> 2013, and I couldn't find any later than Jan 2014 (on unrelated subjects).
>
> So, seeing some strange behavior. I'm running bash on a mac, btw.
>
> 1. Just running my script with glog.Infoln("Prepare to repel boarders") as
> ./main doesn't seem to write anywhere (the docs refer a 'default' file
> location but don't specify)

By default it writes the log to what os.TemDir() returns:
https://github.com/golang/glog/blob/master/glog_file.go#L47

> 1.a I did a find / | grep main.INFO and found these
> /private/var/folders/g4/b1_xxcld3y7cl7_lrmzr1dmc0015rx/T/. Is this what the
> docs mean by the 'default' file location?

On Linux it's /tmp unless configured otherwise (eg. using TMP or
TMPDIR env vars IIRC).

> 2. Running my glog.Infoln script as ./main -log_dir="/Users/jadekler/err"
> seems to have no effect (after chmoding and everything)

I have no Darwin experiences but we use glog with -log_dir and it
works as expected on Linux.

> 3. Adding $TMP="/Users/jadekler/err" as an environment variable and running
> ./main seems to work, but is undocumented behavior in the docs (found in a
> discussion thread). It may be that this is some sort of normal logging
> behavior that I am unaware of (I am certainly no logging expert :) )

It's not a part of the glog docs as it has nothing to do with glog.
Glog asks os.TempDir() for the temp directory. The particular kernel
might consult the env var and thus the result of os.TempDir, which
asks the kernel I suppose, returns a different thing.

> Anyone have any ideas? Am I expecting the wrong behavior with -log_dir? Is
> setting $TMP some sort of unspoken, known logging thing?

It is not. It affects the temp dir returned by the syscall to all programs.

-j

Jean de Klerk

unread,
Apr 29, 2014, 8:14:00 AM4/29/14
to golan...@googlegroups.com, Jean de Klerk
Thanks for the very well explain reply, much appreciated. As far as I can tell, Darwin's tempdir is also /tmp (https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/mktemp.1.html). So this is odd. I will try this same process on another mac and see if I can replicate problems.

In the meantime, you have clarified that the weirdness I'm running into is system-side and not golang. Thanks for the info!
Jean

Bill

unread,
Apr 29, 2014, 10:47:55 AM4/29/14
to Jean de Klerk, golan...@googlegroups.com
Note that on a Mac TMPDIR is /var/folder/gobbledegook/ and that's what
os.TempDir uses, falling back to "/tmp" if it is unset.

On a Mac, /tmp is a symlink to /private/tmp. You can think of this as public
and private temporary directories.

As for your original problem, showing code would be the easiest way for people
to help debug your specific problem. My shot in the dark suggestion would be
to make sure you're calling Flush(). I usually have a line like this at the
top of my main:

defer glog.Flush()

For short lived programs, or programs with little logging output, the data may
not make it to disk before exiting. That race to write out the data might be
why you see the logs sometimes, but not others.

Bill

On 04/29, Jean de Klerk wrote:
> Thanks for the very well explain reply, much appreciated. As far as I can
> tell, Darwin's tempdir is also /tmp
> (https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/mktemp.1.html).
> So this is odd. I will try this same process on another mac and see if I
> can replicate problems.
>
> In the meantime, you have clarified that the weirdness I'm running into is
> system-side and not golang. Thanks for the info!
> Jean
>
> On Tuesday, April 29, 2014 4:18:09 AM UTC-4, Jan Mercl wrote:
> >
> > On Tue, Apr 29, 2014 at 3:16 AM, Jean de Klerk <jade...@gmail.com<javascript:>>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Gyepi SAM

unread,
Apr 29, 2014, 12:06:41 PM4/29/14
to Bill, Jean de Klerk, golan...@googlegroups.com
On Tue, Apr 29, 2014 at 07:47:55AM -0700, Bill wrote:
> to make sure you're calling Flush(). I usually have a line like this at the
> top of my main:
>
> defer glog.Flush()
>
> For short lived programs, or programs with little logging output, the data may
> not make it to disk before exiting. That race to write out the data might be
> why you see the logs sometimes, but not others.

Unless glog is buffering input in order to chunk log writes and does not
handle exits correctly, this should not be necessary.

Pending writes are automatically flushed when a process exits.

-Gyepi

Bill

unread,
Apr 29, 2014, 12:30:49 PM4/29/14
to Jean de Klerk, golan...@googlegroups.com
Yes, that's exactly what glog does, http://godoc.org/github.com/golang/glog

"Log output is buffered and written periodically using Flush. Programs should
call Flush before exiting to guarantee all log output is written."

Bill

Gyepi SAM

unread,
Apr 29, 2014, 12:59:53 PM4/29/14
to Bill, Jean de Klerk, golan...@googlegroups.com

Aye, I just read the code. It's an interesting design choice. I am sure there
are good reasons for using it at Google but the last thing *I* want is the
possibility of missing log entries when a program crashes.

Another good reason to use an external logging program.

-Gyepi

Jean de Klerk

unread,
Apr 29, 2014, 1:33:53 PM4/29/14
to golan...@googlegroups.com, Jean de Klerk
Hey Bill,

Thanks for clarification of the Darwin tmp directory defaults. That makes sense. re:My original problem being -log_dir not affecting the log output location; please see code at https://gist.github.com/jadekler/11406719. This is referred to as Problem 2 in that gist.

In the above gist, basically we have a simple script that imports glog, does a glog.Infoln("foo") and then immediately call glog.Flush(). This will always log to /private/var/blahblah, even when running the script with -log_dir="/path".

Perhaps I'm misunderstanding the params part of glog? As far as I know, intended usage is ./temp -log_dir="/path", right?

Thanks for any tips,
Jean

Jan Mercl

unread,
Apr 29, 2014, 1:37:16 PM4/29/14
to Jean de Klerk, golang-nuts
On Tue, Apr 29, 2014 at 7:33 PM, Jean de Klerk <jade...@gmail.com> wrote:
> In the above gist, basically we have a simple script that imports glog, does
> a glog.Infoln("foo") and then immediately call glog.Flush(). This will
> always log to /private/var/blahblah, even when running the script with
> -log_dir="/path".

Because nowhere is flag.Parse() called before using glog.

""""
By default, all log statements write to files in a temporary
directory. This package provides several flags that modify this
behavior. As a result, flag.Parse must be called before any logging is
done.
""""[0]

[0]: http://godoc.org/github.com/golang/glog

-j

Jean de Klerk

unread,
Apr 29, 2014, 1:48:04 PM4/29/14
to golan...@googlegroups.com, Jean de Klerk
Doh. That would explain it. Should have labeled this thread with 'noob'. =)

Thanks for the tip. I would perhaps suggest to whomever maintains the glog repo to add a note about that for beginners such as myself.

Jean
Reply all
Reply to author
Forward
0 new messages