/**
* 获取指定log文件的指定关键字的日志信息
* @param connection SSH的连接
* @param logFile 需要读取的log文件 全路径 /opt/dubbo_server/risk-server-provider-xjd/logs/server-info.log
* @param key 关键字 例如 节点3150调用策略引擎开始
* @return 返回需要的日志信息行 如果有多天日志信息满足条件,只返回最后一天日志信息
* @throws IOException
*/
public static String getLogInfo(Connection connection, String logFile, String key) throws IOException {
String cmd = "tail -1000 " + logFile + " | grep \'" + key + "\'" ;
System.out.println("====cmd===" + cmd);
// "tail -1000 /opt/dubbo_server/risk-server-provider-xjd/logs/server-info.log | grep \'节点3150调用策略引擎开始,\' | grep \'09-21 14:42:1\'";
Session session = connection.openSession();
session.execCommand(cmd);//执行shell命令
//处理获取的shell命令的输出信息
InputStream stdout = session.getStdout();
InputStreamReader inputStreamReader = new InputStreamReader(stdout);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String s = bufferedReader.readLine();
String lastLineLog = null;
System.out.println("==========以下是获取日志的全部信息============");
while(s != null){
if (s != null){
lastLineLog = s;
System.out.println(s);
}
s = bufferedReader.readLine();
}
System.out.println("==========以上是获取日志的全部信息============");
System.out.println("========以下是日志的最后一行数据=======");
System.out.println(lastLineLog);
//最后关闭session资源
if (session != null){ session.close(); }
return lastLineLog;
}