作为IT工程师,在Linux环境下分析JS日志时,可以采取以下高效方法来快速定位问题:
# 搜索特定错误关键词
grep -i "error\|exception\|fail" application.log
# 搜索特定时间段的日志
grep "2023-11-15 14:" application.log
# 显示匹配行及其后10行内容
grep -A 10 "关键错误" application.log
# 实时查看最新日志
tail -f application.log
# 查看最后100行并实时更新
tail -n 100 -f application.log
# 提取特定列数据
awk '{print $1, $5, $7}' application.log
# 统计错误出现次数
awk '/ERROR/ {count++} END {print count}' application.log
# 提取特定时间范围的日志
sed -n '/2023-11-15 10:00/,/2023-11-15 11:00/p' application.log
# 过滤出错误级别的日志
cat application.log | jq 'select(.level == "error")'
# 提取特定字段
cat application.log | jq '.timestamp, .message'
# 查看logrotate配置
cat /etc/logrotate.d/your_application
# 将日志发送到Logstash的示例配置
input {
file {
path => "/var/log/application.log"
start_position => "beginning"
}
}
grep -A 20 "at " application.log
grep "req-id: abc123" application.log
# 查找执行时间过长的操作
grep "execution time" application.log | awk '$NF > 1000 {print}'
# 查找最近1小时内的错误并按出现次数排序
grep "$(date -d '1 hour ago' '+%Y-%m-%d %H:')" application.log | grep "ERROR" | sort | uniq -c | sort -nr
# 多文件搜索
find /var/log/ -name "*.log" -exec grep -l "特定错误" {} \;
通过以上方法的组合使用,可以快速定位Linux环境下JS应用日志中的问题根源。