11

How can I change the output directory in Google glog?

I only found google::SetLogDestination(google::LogSeverity, const char* path)

tried it with:

google::SetLogDestination(ERROR, "C:\\log\\error.log);
google::InitGoogleLogging("Test");  

LOG(ERROR) << "TEST";

but nothing was written!

Btw.: if you suggest another lightweight, easy to use and thread safe library please let me know!

Thx for any help!

leon22
  • 5,280
  • 19
  • 62
  • 100
  • Are you sure that the escaping is working correctly in that path string? What it you try setting the log dir at the command line with: `GLOG_log_dir=C:\log\error.log ./your_application` – NicholasM Nov 16 '13 at 08:12

5 Answers5

14

You can also do one of the following:

Pass the log directory as a commandline argument as long as you have the GFlgas library installed:

./your_application --log_dir=/some/log/directory

If you don't want to pass it in the commandline and instead set it in the source:

FLAGS_log_dir = "/some/log/directory";

If the Google gflags library isn't installed you can set it as an environment variable:

GLOG_log_dir=/some/log/directory ./your_application
5

Here is the test what I did, you may try it,

#include <glog/logging.h>

using namespace std;

int main(int /*argc*/, char** argv)
{
    FLAGS_logtostderr = true;
    google::SetLogDestination(google::GLOG_INFO,"c:/lovelyGoogle" );
    google::InitGoogleLogging(argv[0]);

    LOG(INFO) << "This is INFO";
    LOG(WARNING) << "This is WARNING";
    LOG(ERROR) << "This is Error";

    system("pause");
    return 0;
}

Tested under Visual studio 2012, google-glog 0.3.3 on Windows 7.
It generated lvoelyGoogle20131016-141423.5160 on my C driver.
If you set FLAGS_logtostderr = false, the log file will not be generated,

I believe you have already read this (well, I have no comment on it)

hope this helpful, good luck.


PS: I have tested on QtCreator (Qt5.1) as well on Windows7, nothing output. I have no idea how to fix it now.

CCC
  • 2,164
  • 8
  • 27
  • 47
  • Weird! Thx anyway for your help! – leon22 Oct 18 '13 at 13:37
  • @CCC Note that if, for instance, you wish to place it in a "dedicated" directory - this directory MUST exists (the glog code won't create during run-time of the program). For example: If you wish that the log will be saved to: `c:/yourDir/lovelyGoogle` (note the `yourDir` addition), than `yourDir` needs to exist prior to your program execution. – Guy Avraham Sep 15 '18 at 08:16
3

In my terrifying experience with this library I came to see that this flag FLAGS_log_dir and this function google::SetLogDestination() compete with each other. Wonderful.

I learned that you can use either but not both.

option 1: use flags

FLAGS_log_dir=/path/to/your/logdir
google::InitGoogleLogging(exec_name.c_str());

and generate a bunch of files named your_exec.some_number.machine_name.log.log_severity.... and their respective symbolic link inside the /path/to/your/logdir. A pair of files will be generated for each log_severity you have used in your program (INFO, WARNING, ERROR, FATAL). Fun fact: INFO files contain everything, WARNING files contain everything from warning downwards, and so on. God knows why these symlinks are needed.

option 2: use file names

std::string log_dir = "/path/to/log/dir";
for (int severity = 0; severity < google::NUM_SEVERITIES; ++severity) {
   std::string fpath = fs::path(fs::path(log_dir) / fs::path(exec_name).filename());
   google::SetLogDestination(severity, fpath.c_str());
   google::SetLogSymlink(severity, "");
}
google::InitGoogleLogging(exec_name.c_str());

where fs is the filesystem library of c++17. This is send all logs to the same file (just one, not many) and finally remove that annoying symbolic link.

angelo.mastro
  • 1,680
  • 17
  • 14
1

I use this:

fLS::FLAGS_log_dir = "c:/Documents/logs";
Adrian
  • 19,440
  • 34
  • 112
  • 219
1

This solution works for me:

FLAGS_log_dir = "./log/";
FLAGS_alsologtostderr = true;
google::InitGoogleLogging(argv[0]);

FLAGS_log_dir sets the preferred logging destination, as it says.
FLAGS_alsologtostderr sets if you also wanna log to stderr, while saving logs into a separate file for review.

Note:

Don't set FLAGS_logtostderr to true if you want your log be written to both the file and stderr, that way use FLAGS_alsologtostderr instead as I did.


Tested under Clion 2023.1.3, macOS 13.3.1 (a) (22E772610a), glog v0.6.0