分析日志时,strings 通常有两种常见含义,我分别说明,你可以对照看看你指的是哪一种。
strings(从二进制/日志文件中提取可打印字符串)strings app.log
strings -n 6 app.log # 只显示长度 ≥ 6 的字符串
strings -t x app.log # 显示字符串在文件中的偏移(十六进制)
strings -e l app.log # 指定编码(l=16-bit little-endian)
strings core.1234 | grep ERROR
strings /usr/bin/nginx | grep "connection"
✅ 适合:
❌ 不适合:
这是最常见、最推荐的方式,适合长期分析。
2024-01-01 10:01:23 ERROR user login failed
2024-01-01 10:02:10 INFO user login success
import re
with open("app.log") as f:
for line in f:
if "ERROR" in line:
print(line.strip())
pattern = r'(?P<time>\S+ \S+) (?P<level>\S+) (?P<msg>.*)'
for line in open("app.log"):
m = re.match(pattern, line)
if m and m.group("level") == "ERROR":
print(m.group("msg"))
try (BufferedReader br = new BufferedReader(new FileReader("app.log"))) {
String line;
while ((line = br.readLine()) != null) {
if (line.contains("ERROR")) {
System.out.println(line);
}
}
}
✅ 按级别
✅ 按时间
✅ 按关键字
✅ 按频次
grep "ERROR" app.log | sort | uniq -c | sort -nr
✅ 结构化(强烈推荐)
strings,什么时候不用?| 场景 | 是否用 strings |
|---|---|
| 二进制文件/崩溃文件 | ✅ |
| 普通文本日志 | ❌ |
| 结构化日志分析 | ❌ |
| 快速应急排查 | ✅ |
你可以直接告诉我:
我可以给你针对性的分析脚本或命令。