【任务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

【八】将日志写入log(glog)的更多相关文章

  1. PHP实现日志写入log.txt

    引言:有时候调试,看不到效果,需要通过写入文件来实现. 案例: <?php $myfile = fopen("log.txt", "a+") or die ...

  2. Python + logging 输出到屏幕,将log日志写入文件

    日志 日志是跟踪软件运行时所发生的事件的一种方法.软件开发者在代码中调用日志函数,表明发生了特定的事件.事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数 ...

  3. Python + logging输出到屏幕,将log日志写入到文件

    logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). logging函数根据它们用来跟踪的事 ...

  4. Spring Boot从入门到精通(八)日志管理实现和配置信息分析

    Spring Boot对日志的处理,与平时我们处理日志的方式完全一致,它为Java Util Logging.Log4J2和Logback提供了默认配置.对于每种日志都预先配置使用控制台输出和可选的文 ...

  5. .NET Core的日志[4]:将日志写入EventLog

    面向Windows的编程人员应该不会对Event Log感到陌生,以至于很多人提到日志,首先想到的就是EventLog.EventLog不仅仅记录了Windows系统自身针对各种事件的日志,我们的应用 ...

  6. .NET Core的日志[3]:将日志写入Debug窗口

    定义在NuGet包"Microsoft.Extensions.Logging.Debug"中的DebugLogger会直接调用Debug的WriteLine方法来写入分发给它的日志 ...

  7. logback日志写入数据库(mysql)配置

    如题  建议将日志级别设置为ERROR.这样可以避免存储过多的数据到数据中. 1  logback 配置文件(如下) <?xml version="1.0" encoding ...

  8. 将日志写入EventLog

    将日志写入EventLog 面向Windows的编程人员应该不会对Event Log感到陌生,以至于很多人提到日志,首先想到的就是EventLog.EventLog不仅仅记录了Windows系统自身针 ...

  9. 将日志写入Debug窗口

    定义在NuGet包“Microsoft.Extensions.Logging.Debug”中的DebugLogger会直接调用Debug的WriteLine方法来写入分发给它的日志消息.如果需要使用D ...

随机推荐

  1. ls 操作命令 -l/-R和rm -r dir 功能实现

    ls -R #include <sys/stat.h> #include <dirent.h> #include <fcntl.h> #include <st ...

  2. EF学习之CodeFirst(一)--创建Model

    一.创建Model 创建Model类有两种方式: 1.直接创建model 所有约束条件都以特性的方式写在model的属性上面,映射到数据库的table表名标识在class上,例如: [Table(&q ...

  3. ZT fcntl设置FD_CLOEXEC标志作用

    fcntl设置FD_CLOEXEC标志作用 分类: C/C++ linux 2011-11-02 22:11 3217人阅读 评论(0) 收藏 举报 bufferexegccnullfile 通过fc ...

  4. ZT I Believe I Can Fly(我相信我能飞)

    I Believe I Can Fly(我相信我能飞) 歌手:R. Kelly(罗 凯利) 歌词部分 I used to think that I could not go on 我原以为我无法坚持下 ...

  5. 78、WebClient实现上传下载 System.Net、System.Uri类

    高层类.使用简单.均支持异步版本.支持http,https,fpt,files等URI. 一.下载 方法: Stream= client.OpenRead(serverUri): 打开一个可读的流.对 ...

  6. 1、Node.js 我的开始+安装

    内容:为什么开始学习node.js,需要安装哪些东西,及其安装过程 node.js的学习是按照菜鸟教程的node.js教程学习,学习这项技术主要是因为需要使用. 需要安装的东西:解释器,IDE(集成开 ...

  7. linux setup 相关text mode图形配置工具的安装

    centos 6.4 x86_64 minimal安装后发现setup命令不可用 yum update yum install setup 安装完了还是不可用,不知为什么,难道装的那个包不对?yum ...

  8. JS数据模板分离(告别字符串拼接)-template

    刚开始在写第一个动态网页的demo时,由于html不多,便使用字符串拼接的方法添加到dom来渲染,可是在后来写某外卖app时也需要如此添加,打开代码一看几千行,突然感觉累觉不爱 一行行的拼接有这功夫别 ...

  9. angularJs购物金额实例操作

    <!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...

  10. cogs [HZOI 2015]有标号的二分图计数

    题目分析 n个点的二分染色图计数 很显然的一个式子 \[ \sum_{i=0}^n\binom{n}{i}2^{i(n-i)} \] 很容易把\(2^{i(n-i)}\)拆成卷积形式,前面讲过,不再赘 ...