zoukankan      html  css  js  c++  java
  • 【八】将日志写入log(glog)

    【任务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("=======================
      ");
                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
  • 相关阅读:
    一个网卡绑定多个IP和多个网卡用一个ip的设置
    Linux操纵细碎以太网卡的装配及设置2
    教你若何制作软盘版的 Linux系统防火墙
    Ubuntu Linux体系中装置GNOME初步劈脸菜单
    构筑Linux防火墙之集团用户设置防火墙1
    Linux系统Iptables端方执行按次过细讲解
    Proxy源代码分析 谈Linux收集编程身手
    Linux使用系统以太网卡的安装及设置装备陈设1
    操持Linux下Oracle Tomcat 8080端口争持2
    Linux操作体系上差别文件体系的兼容成就
  • 原文地址:https://www.cnblogs.com/screen/p/9481747.html
Copyright © 2011-2022 走看看