【八】将日志写入log(glog)

本文介绍如何配置和使用glog日志系统,包括下载、编译glog库,将其集成到Thrift服务中,并自定义日志输出路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【任务8】将日志写入log(glog)

glog简介

glog是google开源的一个日志系统,相比较log4系列的日志系统,它更加轻巧灵活,而且功能也比较完善

glog配置使用资料

下载glog

  • 命令:git clone https://github.com/google/glog.git

  • 如果没有git命令:yum -y install git

编译glog

  • 进入glog目录,打开README文件,按照其中的提示步骤进行编译:./autogen.sh && ./configure && make && make install

  • 完成后,会在/usr/local/lib路径下看到libglog*一系列库

Makefile文件和RecSys_server.skeleton.cpp文件

  • 修改之前在gen-cpp目录中的Makefile文件,在LIBS变量的末尾加上-lglog:LIBS = -L/usr/local/lib/*.so -lthrift -lfcgi -lglog

  • 修改gen-cpp目录中RecSys_server.skeleton.cpp文件:

      #include "RecSys.h"
      #include <thrift/protocol/TBinaryProtocol.h>
      #include <thrift/server/TSimpleServer.h>
      #include <thrift/transport/TServerSocket.h>
      #include <thrift/transport/TBufferTransports.h>
    
      //添加头文件
      #include <glog/logging.h>
    
      using namespace ::apache::thrift;
      using namespace ::apache::thrift::protocol;
      using namespace ::apache::thrift::transport;
      using namespace ::apache::thrift::server;
    
      using boost::shared_ptr;
    
      class RecSysHandler : virtual public RecSysIf {
      public:
          RecSysHandler() {
              // Your initialization goes here
          }
    
          void rec_data(std::string& _return, const std::string& data) {
              // Your implementation goes here
              printf("=======================\n");
              std::cout << "receive client data:" << data << std::endl;
    
              std::string ack = "yeah,I love you too!!";
    
              //log输出,使用时按情况选择
              LOG(INFO) << data;
              LOG(ERROR) << data;
              LOG(WARNING) << data;
    
              _return = ack;
          }
    
          };
    
          int main(int argc, char **argv) {
    
          google::InitGoogleLogging(argv[0]); 
    
          int port = 9090;
          shared_ptr<RecSysHandler> handler(new RecSysHandler());
          shared_ptr<TProcessor> processor(new RecSysProcessor(handler));
          shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
          shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
          shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
    
          TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
          server.serve();
          return 0;
      }

重新编译client端和server端

  • kill掉之前的client和server,在make相应的client和server

  • 再次启动server端和client,命令:./server,/usr/local/src/nginx/sbin/spawn-fcgi -a 127.0.0.1 -p 8088 -f /test/thrift_test/python_thrift_demo/gen-cpp/client

  • 同上操作,用浏览器的方式访问,server端会打印出log,并产生log日志文件,默认路径:/tmp/下的server.ERROR,server.INFO,server.WARNING

  • 更改glog产生log日志文件的路径,在gen-cpp目录下的RecSys_server.skeleton.cpp文件中的int main()方法中添加代码,添加glog输出地址:
int main(int argc, char **argv) {

//glog地址
FLAGS_log_dir = "/test/thrift_test/python_thrift_demo/gen-cpp/logs"
google::InitGoogleLogging(argv[0]); 

int port = 9090;
shared_ptr<RecSysHandler> handler(new RecSysHandler());
shared_ptr<TProcessor> processor(new RecSysProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());

TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}

其中logs目录需要自行创建

  • 然后执行make命令,重新生成client端和server

转载于:https://www.cnblogs.com/screen/p/9481747.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值