百度apollo - Apollo代码解析:2. log系统google glog

本文介绍了Apollo项目中使用google glog作为日志系统的实践,包括安装、基本用法、日志分级和VLOG的使用。通过设置gflags参数控制日志输出目录,演示了不同级别日志的输出效果,以及如何通过-V标志控制VLOG信息的显示。

Apollo中的glog

在Apollo中google glog 被广泛使用,glog 是 google 的一个 c++ 开源日志系统,轻巧灵活,入门简单,而且功能也比较完善。

1. 安装

以下是官方的安装方法,一句命令:

git clone https://github.com/google/glog.git
cd glog 
./autogen.sh && ./configure && make && make install

2. 使用

2.1 菜鸟级

从一个最简单的程序开始:

#include <glog/logging.h>

int main(int argc,char* argv[])
{
    google::InitGoogleLogging(argv[0]); //初始化 glog
    LOG(INFO) << "Hello,GOOGLE!";
}

编译:

1. glog g++ glog_test2.cpp -lglog -lgflags -lpthread -o glog_test #编译
2. glog ./glog_test
3. glog ll /tm
In file included from /home/acceler/code/apollo_ros/apollo_ros/src/apollo.ros-1.0.0-master/apollo_common/include/apollo_common/apollo_app.h:46:0, from /home/acceler/code/apollo_ros/apollo_ros/src/apollo.ros-1.0.0-master/apollo_common/src/apollo_app.cc:33: /home/acceler/code/apollo_ros/apollo_ros/src/apollo.ros-1.0.0-master/apollo_common/include/apollo_common/log.h:40:10: fatal error: glog/logging.h: No such file or directory #include <glog/logging.h> ^~~~~~~~~~~~~~~~ compilation terminated. apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/build.make:62: recipe for target 'apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/src/apollo_app.cc.o' failed make[2]: *** [apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/src/apollo_app.cc.o] Error 1 make[2]: *** Waiting for unfinished jobs.... In file included from /home/acceler/code/apollo_ros/apollo_ros/src/apollo.ros-1.0.0-master/apollo_common/include/apollo_common/adapters/adapter_manager.h:48:0, from /home/acceler/code/apollo_ros/apollo_ros/src/apollo.ros-1.0.0-master/apollo_common/src/adapters/adapter_manager.cc:33: /home/acceler/code/apollo_ros/apollo_ros/src/apollo.ros-1.0.0-master/apollo_common/include/apollo_common/adapters/adapter.h:49:10: fatal error: glog/logging.h: No such file or directory #include <glog/logging.h> ^~~~~~~~~~~~~~~~ compilation terminated. apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/build.make:110: recipe for target 'apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/src/adapters/adapter_manager.cc.o' failed make[2]: *** [apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/src/adapters/adapter_manager.cc.o] Error 1 CMakeFiles/Makefile2:3894: recipe for target 'apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/all' failed make[1]: *** [apollo.ros-1.0.0-master/apollo_common/CMakeFiles/apollo_common.dir/all] Error 2 make[1]: *** Waiting for unfinished jobs.... [ 54%] Linking CXX executable /home/acceler/code/apollo_ros/apollo_ros/devel/lib/IntegratedNavigation/IntegratedNavigation_node [ 54%] Built target IntegratedNavigation_node [ 55%] Linking CXX executable /home/acceler/code/apollo_ros/apollo_ros/devel/lib/TimeSynchronierProcess/timeSynchronierProcess_node [ 55%] Built target timeSynchronierProcess_node Makefile:140: recipe for target 'all' failed make: *** [all] Error 2 Invoking "make -j4 -l4" failed
07-23
Apollo 配置中心在配置更新时会通过 `AutoUpdateConfigChangeListener` 监听配置的变化,并在检测到变化后自动更新相关的 Bean 属性值。通过日志信息可以分析出具体的配置更新行为和更新结果。 ### 日志信息分析 1. **日志结构解析**: - 日志条目通常包含时间戳、日志级别、线程名称、日志内容等信息。 - 示例日志: ``` 2025-07-02 12:06:21.197 INFO 68185 --- [Apollo-Config-1] c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: val, key: apollo.value, beanName: scopedTarget.apollo, field: com.csh.springcloud.apolloclient.controller.ApolloConfigController.apolloValue ``` 该日志表明 `AutoUpdateConfigChangeListener` 成功更新了配置值,新的值为 `val`,对应的键为 `apollo.value`,更新的 Bean 名称为 `scopedTarget.apollo`,字段为 `com.csh.springcloud.apolloclient.controller.ApolloConfigController.apolloValue`。 2. **配置更新过程**: -Apollo 配置发生变化时,`AutoUpdateConfigChangeListener` 会接收到 `ConfigChangeEvent` 事件。 - 在 `onChange` 方法中,系统会根据变更的配置键值更新对应的 Bean 属性。 - 更新成功后,会在日志中记录更新的详细信息,包括新值、键名、Bean 名称和字段名[^1]。 3. **热更新验证**: - 通过日志可以确认热更新是否生效。 - 例如,以下日志表明 `mysql.password` 和 `mysql.username` 的配置值成功更新为 `root123`: ``` c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: root123, key: mysql.password, beanName: configProperties, field: com.example.config.ConfigProperties.mysqlPassword c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: root123, key: mysql.username, beanName: configProperties, field: com.example.config.ConfigProperties.mysqlUsername ``` 4. **日志中的版本信息**: - 日志中提到的 Apollo 版本为 1.4(公司内部二次开发版),基于此版本的行为可能会与开源版本(如 1.9)有所不同。 - 特别是 `AutoUpdateConfigChangeListener` 的实现逻辑可能因版本差异而略有不同[^4]。 5. **日志输出的 Bean 更新信息**: - 日志详细记录了每个配置项的更新情况,包括更新前后的值。 - 示例: ``` 2025-07-02 12:06:21.193 INFO 68185 --- [Apollo-Config-2] c.c.s.a.r.SpringBootApolloRefreshConfig : apollo value before refresh -1449272902 defaultValue 2025-07-02 12:06:21.204 INFO 68185 --- [Apollo-Config-2] c.c.s.a.r.SpringBootApolloRefreshConfig : apollo value after refresh -1449272902 val ``` 该日志表明配置值从 `defaultValue` 更新为 `val`。 ### 日志分析建议 - **关注关键字段**:查看日志中的 `key` 和 `new value` 字段,以确认哪些配置项发生了变化。 - **检查更新时间**:通过时间戳可以确定配置更新的时间点。 - **验证 Bean 更新**:检查 `beanName` 和 `field` 字段,确保配置更新作用在正确的 Bean 和字段上。 - **对比更新前后值**:如果日志中记录了更新前后的值,可以通过对比确认热更新是否按预期生效。 ### 示例日志分析代码 如果需要通过代码解析日志文件,可以使用以下 Python 示例代码: ```python import re def parse_apollo_log(log_line): # 正则表达式匹配日志格式 pattern = r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}) INFO \d+ --- $([A-Za-z0-9\-]+)$: (.+)' match = re.match(pattern, log_line) if match: timestamp, thread, message = match.groups() print(f"Timestamp: {timestamp}, Thread: {thread}, Message: {message}") else: print("Log line does not match expected format.") # 示例日志行 log_line = '2025-07-02 12:06:21.197 INFO 68185 --- [Apollo-Config-1] c.f.a.s.p.AutoUpdateConfigChangeListener : Auto update apollo changed value successfully, new value: val, key: apollo.value, beanName: scopedTarget.apollo, field: com.csh.springcloud.apolloclient.controller.ApolloConfigController.apolloValue' parse_apollo_log(log_line) ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值