glog icon indicating copy to clipboard operation
glog copied to clipboard

can glog write to a specified file name?

Open xiaoyulei opened this issue 9 years ago • 5 comments

I want to write log to a specified file name, is there any parameter or interface to pass the name?

xiaoyulei avatar Dec 14 '16 06:12 xiaoyulei

@YuleiXiao There is no ready interface to implement this feature,but

  • If you just want to modify the file name suffix, you can modify the file "glog\src\logging.cc",like below:
//Add your suffix name, for example".helenxr.txt".
bool LogFileObject::CreateLogfile(const string& time_pid_string) {
  string string_filename = base_filename_+ filename_extension_+
                           time_pid_string + ".helenxr.txt";
  • If you want to completely customize the file name, then some trouble, first you need to understand that GLOG will create 4 level log files (INFO, WARNING, ERROR, FATAL), and high levels of log files will be written not only written to their lower level than their files, so you should pay attention to distinguish between these 4 files in the custom file name. The following is my verification feasible, change the file"glog\src\logging.cc" like below:
bool LogFileObject::CreateLogfile(const string& time_pid_string) {
  //string string_filename = base_filename_+ filename_extension_+
  //                         time_pid_string + ".helenxr.txt";
  string custom_file_name_head = "helenxr",custom_file_name_end = ".txt";
  if(base_filename_.find("INFO") != std::string::npos){
		custom_file_name_head += "_info";
  }
  else if(base_filename_.find("WARNING") != std::string::npos){
	  custom_file_name_head += "_warning";
  }
  else if(base_filename_.find("ERROR") != std::string::npos){
	  custom_file_name_head += "_error";
  }
  else if(base_filename_.find("FATAL") != std::string::npos){
	  custom_file_name_head += "_fatal";
  }
  else{
	  custom_file_name_head += "_other"; 		
  }
string string_filename = custom_file_name_head + custom_file_name_end;

Then,LOG(ERROR) << "your error message!",The files that are created: demo

Note this log file will created the current path in your application. The above method I just verify the feasibility, of course, you can achieve this function and package an interface to better. good luck :-)

HelenXR avatar Apr 26 '17 07:04 HelenXR

+1 to write to a specified file name, can we have this feature instead of having to modify the source ourselves?

jasjuang avatar Nov 30 '17 19:11 jasjuang

@jasjuang but i seems that, filename is hard coding inside LogFileObject::CreateLogfile

lizijie avatar Jan 15 '18 07:01 lizijie

+1 , same demand, would extract the log file extension (suffix) as an parameter? thanks

chenqiangzhishen avatar Jan 20 '19 07:01 chenqiangzhishen

Write to a specified file name may couldn't work. But the symbol link file name can be specified. For example: call google::SetLogSymlink(google::GLOG_INFO, "some_specified_name") before LOG will generate symbol link file of INFO logs in log_link (which is set by FLAGS_log_link) path.

Mag1cL avatar Apr 24 '19 04:04 Mag1cL