9

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!

4

4 回答 4

13

您还可以执行以下操作之一:

只要您安装了 GFlgas 库,就将日志目录作为命令行参数传递:

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

如果您不想在命令行中传递它,而是在源代码中设置它:

FLAGS_log_dir = "/some/log/directory";

如果未安装 Google gflags 库,您可以将其设置为环境变量:

GLOG_log_dir=/some/log/directory ./your_application
于 2014-09-13T15:37:53.393 回答
4

这是我做的测试,你可以试试,

#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;
}

在 Windows 7 上的 Visual Studio 2012、google-glog 0.3.3 下测试。它在我的 C 驱动程序上
生成。 如果设置,则不会生成日志文件,lvoelyGoogle20131016-141423.5160
FLAGS_logtostderr = false

我相信你已经读过这个(好吧,我对此不做评论)

希望这有帮助,祝你好运。


PS:我已经在 QtCreator (Qt5.1) 以及 Windows7 上进行了测试,没有任何输出。我现在不知道如何修复它。

于 2013-10-16T06:21:07.587 回答
1

我用这个:

fLS::FLAGS_log_dir = "c:/Documents/logs";
于 2014-02-08T15:50:47.180 回答
0

在我使用这个库的可怕经历中,我发现这个标志FLAGS_log_dir和这个函数google::SetLogDestination()相互竞争。精彩的。

我了解到您可以使用其中之一,但不能同时使用两者。

选项 1:使用标志

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

your_exec.some_number.machine_name.log.log_severity..../path/to/your/logdir. 将为您在程序中使用的每个 log_severity 生成一对文件(INFO、WARNING、ERROR、FATAL)。有趣的事实:INFO 文件包含所有内容,WARNING 文件包含从警告向下等所有内容。天知道为什么需要这些符号链接。

选项 2:使用文件名

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());

fsc++17的文件系统库在哪里。这是将所有日志发送到同一个文件(只有一个,不是很多),最后删除那个烦人的符号链接。

于 2021-10-22T14:03:28.107 回答