LogImp.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __LOG_IMP_H_
  17. #define __LOG_IMP_H_
  18. #include "servant/LogF.h"
  19. #include "util/tc_common.h"
  20. #include "util/tc_file.h"
  21. #include "util/tc_logger.h"
  22. #include "util/tc_monitor.h"
  23. //#include "LogServer.h"
  24. using namespace tars;
  25. /**
  26. * 全局信息
  27. */
  28. struct GlobeInfo : public TC_ThreadLock
  29. {
  30. public:
  31. /**
  32. * 获取按天日志
  33. * @param app
  34. * @param server
  35. * @param file
  36. * @param format
  37. * @param ip 服务所在节点ip
  38. *
  39. * @return TC_DayLogger&
  40. */
  41. TC_DayLogger& getLogger(const string &app, const string &server, const string &file, const string &format,const string& ip);
  42. /**
  43. * 获取按天日志
  44. * @param info 详情参看LogInfo
  45. * @param ip 服务所在节点ip
  46. *
  47. * @return TC_DayLogger&
  48. */
  49. TC_DayLogger& getLogger(const LogInfo & info,const string& ip);
  50. /**
  51. * 更新_vHourlist和_mLogType配置
  52. */
  53. void update(const vector<string> &vHourlist, const map<string,string> &mLogType);
  54. protected:
  55. /**
  56. * 生成按天日志
  57. * @param info 详情参看LogInfo
  58. * @param logname
  59. * @param format
  60. * @param ip 服务所在节点ip
  61. *
  62. * @return TC_DayLogger&
  63. */
  64. TC_DayLogger& makeDayLogger(const LogInfo & info, const string &logname, const string &format,const string& ip);
  65. /**
  66. * 生成按天日志
  67. * @param app
  68. * @param server
  69. * @param logname
  70. * @param format
  71. * @param ip 服务所在节点ip
  72. *
  73. * @return TC_DayLogger&
  74. */
  75. TC_DayLogger& makeDayLogger(const string &app, const string &server, const string &logname, const string &format,const string& ip);
  76. /**
  77. * 判断相应的logname下是否有指定的format
  78. * @param logname 由file.empty() ? (app + "." + server) : (app + "." + server + "_" + file)方式组成
  79. * @param format
  80. * @param[out] ip 如果有指定的format存在,则返回该format对应的节点ip
  81. *
  82. * @return bool
  83. */
  84. bool HasSameFormat(const string& logname,const string& format,string& ip);
  85. /**
  86. * 判断在logname下,除了节点sExcludeIp外,是否还有其它节点在使用pLoger指定的logger实例
  87. * @param logname
  88. * @param sExcludeIp
  89. * @param pLogger
  90. *
  91. * @return bool
  92. */
  93. bool IsLoggerAttached(const string& logname,const string& sExcludeIp,const TC_DayLogger* pLoger);
  94. private:
  95. /**
  96. * 获取set完整格式
  97. * @param sSetDivision set分组信息,例如:mtt.s.1 mtt.s.*
  98. *
  99. * @return 规整后的格式,例如:mtt.s.* 去掉"*"符号,返回mtts;mtts1则原样返回
  100. */
  101. string getSetGoodFormat(const string& sSetDivision);
  102. /**
  103. * 从LogInfo中获取相关信息得到标识一个logger对象的字符串
  104. * @param info
  105. */
  106. string getLogName(const LogInfo & info);
  107. /**
  108. * 从LogInfo中获取日志文件名称,包括路径名在内
  109. * @param info
  110. */
  111. string getRealLogName(const LogInfo & info);
  112. LogTypePtr getTarsLogType(const string& sFormat, const string& sCutType);
  113. public:
  114. /**
  115. * 写日志线程
  116. */
  117. TC_LoggerThreadGroup _group;
  118. /**
  119. * 写日志路径
  120. */
  121. string _log_path;
  122. /**
  123. * logger对象:logname,ip,logger
  124. */
  125. map<string, map<string,TC_DayLogger*> > _loggers;
  126. /**
  127. * logger格式:logname,ip,format
  128. */
  129. map<string, map<string,string> > _formats;
  130. /**
  131. * 按小时记录的配置
  132. */
  133. vector<string> _vHourlist;
  134. /**
  135. * 按时间方式配置
  136. * key:日志文件全名: appname.servername.filename
  137. * value:时间格式字符串:hour,2hour,5minute,10minute
  138. */
  139. map<string,string> _mLogType;
  140. /**
  141. *是否打印客户端ip
  142. */
  143. bool _bIpPrefix;
  144. };
  145. extern GlobeInfo g_globe;
  146. /**
  147. * log实现
  148. */
  149. class LogImp : public Log
  150. {
  151. public:
  152. /**
  153. *
  154. */
  155. LogImp(){};
  156. /**
  157. *
  158. */
  159. ~LogImp(){};
  160. /**
  161. * 初始化
  162. *
  163. * @return int
  164. */
  165. virtual void initialize();
  166. /**
  167. * 退出
  168. */
  169. virtual void destroy() {};
  170. /**
  171. * 输出日志信息到指定文件
  172. * @param app 业务名称
  173. * @param server 服务名称
  174. * @param file 日志文件名称
  175. * @param format 日志输出格式
  176. * @param buffer 日志内容
  177. *
  178. *
  179. */
  180. void logger(const string &app, const string &server, const string &file, const string &format, const vector<string> &buffer, tars::TarsCurrentPtr current);
  181. /**
  182. * 获取数据
  183. * @param info
  184. * @param buffer
  185. *
  186. */
  187. void loggerbyInfo(const LogInfo & info,const vector<std::string> & buffer,tars::TarsCurrentPtr current);
  188. private:
  189. };
  190. #endif