Roger
Roger

Reputation: 121

glog always create two files, which has nearly the same content

My test code as blow:

std::string strLogPath = "d:/logtest/";
google::InitGoogleLogging("test1");
FLAGS_log_dir = strLogPath;
FLAGS_stderrthreshold = google::GLOG_INFO;
FLAGS_minloglevel = google::GLOG_INFO;
//FLAGS_colorlogtostderr = true;

std::string strLogPath1 = "d:/logtest/L";
google::SetLogDestination(google::GLOG_INFO, strLogPath1.c_str()); 
google::SetLogDestination(google::GLOG_ERROR, strLogPath1.c_str());
google::SetLogDestination(google::GLOG_WARNING, strLogPath1.c_str());
google::SetLogDestination(google::GLOG_FATAL, strLogPath1.c_str());

LOG(INFO) << "infoinfo";
Sleep(1000);
LOG(WARNING) << "wwwww";
LOG(WARNING) << "wwwww";
LOG(ERROR) << "eeeeee";
Sleep(2000);
//LOG(FATAL) << "ffffff";
LOG(WARNING) << "wwwww";
LOG(WARNING) << "wwwww";
LOG(WARNING) << "wwwww";
google::ShutdownGoogleLogging();

I got two log files, one file contain all messages(INFO, WARNING and ERROR), and the other contain all WARNING and ERROR messages, but without INFO. This is quiet different from my expectation. I want all messages in one file, and don't like the WARNING and ERROR messages appear twice in different files. It would be highly appreciated if someone can tell me the solution. Thanks a lot in advance.

Upvotes: 0

Views: 1356

Answers (2)

Anton
Anton

Reputation: 1

You can set the destination for the INFO log severity, since all higher severity logs also log to the lower ones and disable other levels. This will result in logs being collected in a single file.

 google::SetLogDestination(google::INFO, "/tmp/yourapp/logs/yourapp.log");
 google::SetLogDestination(google::WARNING, "");
 google::SetLogDestination(google::ERROR, "");
 google::SetLogDestination(google::FATAL, "");
 
 google::InitGoogleLogging(argv[0]);

Upvotes: 0

muXXmit2X
muXXmit2X

Reputation: 2765

You probably already have a solution for your problem but I will post this anyway for other users.


glog is not designed for logging in one file as far as I know. Therefore you can't do what you want using only glog.

However there are other solutions for your problem.

First: Write your own little logging library. This isn't very complicated and is a great Programming training ;)

Second: For *nix only. Activate logging to stderr in glog using the logtostderr-Flag and redirect the output from stderr to the desired log file.

FLAGS_logtostderr=1;
LOG(INFO) << "Info";
LOG(WARNING) << "Warning";
LOG(ERROR) << "Error";

and on the shell: ./MyProg 2>logFile

Last: Keep everything as it is and delete the logfiles you don't need. Either within your program using c/c++ or with a call to system()

Upvotes: 0

Related Questions