【八】将日志写入log(glog)
【任务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("=======================\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)的更多相关文章
- PHP实现日志写入log.txt
引言:有时候调试,看不到效果,需要通过写入文件来实现. 案例: <?php $myfile = fopen("log.txt", "a+") or die ...
- Python + logging 输出到屏幕,将log日志写入文件
日志 日志是跟踪软件运行时所发生的事件的一种方法.软件开发者在代码中调用日志函数,表明发生了特定的事件.事件由描述性消息描述,该描述性消息可以可选地包含可变数据(即,对于事件的每次出现都潜在地不同的数 ...
- Python + logging输出到屏幕,将log日志写入到文件
logging提供了一组便利的函数,用来做简单的日志.它们是 debug(). info(). warning(). error() 和 critical(). logging函数根据它们用来跟踪的事 ...
- Spring Boot从入门到精通(八)日志管理实现和配置信息分析
Spring Boot对日志的处理,与平时我们处理日志的方式完全一致,它为Java Util Logging.Log4J2和Logback提供了默认配置.对于每种日志都预先配置使用控制台输出和可选的文 ...
- .NET Core的日志[4]:将日志写入EventLog
面向Windows的编程人员应该不会对Event Log感到陌生,以至于很多人提到日志,首先想到的就是EventLog.EventLog不仅仅记录了Windows系统自身针对各种事件的日志,我们的应用 ...
- .NET Core的日志[3]:将日志写入Debug窗口
定义在NuGet包"Microsoft.Extensions.Logging.Debug"中的DebugLogger会直接调用Debug的WriteLine方法来写入分发给它的日志 ...
- logback日志写入数据库(mysql)配置
如题 建议将日志级别设置为ERROR.这样可以避免存储过多的数据到数据中. 1 logback 配置文件(如下) <?xml version="1.0" encoding ...
- 将日志写入EventLog
将日志写入EventLog 面向Windows的编程人员应该不会对Event Log感到陌生,以至于很多人提到日志,首先想到的就是EventLog.EventLog不仅仅记录了Windows系统自身针 ...
- 将日志写入Debug窗口
定义在NuGet包“Microsoft.Extensions.Logging.Debug”中的DebugLogger会直接调用Debug的WriteLine方法来写入分发给它的日志消息.如果需要使用D ...
随机推荐
- NodeJS的特点
一. NodeJS的特点 我们先来看看NodeJS官网上的介绍: Node.js is a platform built on Chrome’s JavaScript runtime for easi ...
- [翻译] HTKDynamicResizingCell
HTKDynamicResizingCell https://github.com/henrytkirk/HTKDynamicResizingCell Subclassed UITableView/U ...
- Python实例---利用正则实现计算器[FTL版]
import re # 格式化 def format_str(str): str = str.replace('--', '+') str = str.replace('-+', '-') str = ...
- Win8.1下运行环境/配置问题解决方案总结
目录 1.运行 adb shell 时报错" adb server version (26) doesn't match this client (39); killing... " ...
- ubuntu命令安装jdk
1.ubuntu使用的是openjdk,所以我们需要先找到合适的jdk版本.在命令行中输入命令: $apt-cache search openjdk 1 返回结果列表(因个人电脑而有所不同): def ...
- [运维笔记] Nginx编译安装
yum -y install pcre-devel.x86_64 yum -y install openssl openssl-devel.x86_64 useradd www -s /sbin/no ...
- [Python 多线程] threading.local类 (六)
在使用threading.local()之前,先了解一下局部变量和全局变量. 局部变量: import threading import time def worker(): x = 0 for i ...
- seek()和tell()在文件里转移
Seek()方法允许在输入和输出流移动到任意的位置,seek()有好几种形式.包含:seekp() 方法和seekg()方法,p是put的意思,g是get的意思:其中输入流里用seekg()函数,输出 ...
- Java并发基础概念
Java并发基础概念 线程和进程 线程和进程都能实现并发,在java编程领域,线程是实现并发的主要方式 每个进程都有独立的运行环境,内存空间.进程的通信需要通过,pipline或者socket 线程共 ...
- SpringBoot 默认日志
默认使用的这个类 org.apache.commons.logging.Log import org.apache.commons.logging.Log; import org.apache.com ...