【任务8】将日志写入log(glog)
glog简介
glog是google开源的一个日志系统,相比较log4系列的日志系统,它更加轻巧灵活,而且功能也比较完善
下载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
端