在 CentOS 中使用 nohup 运行程序时,日志一般输出到 nohup.out 或你指定的日志文件。过滤日志内容常用以下几种方式:
grep 过滤关键字grep "ERROR" nohup.out
忽略大小写:
grep -i "error" nohup.out
显示行号:
grep -n "ERROR" nohup.out
tail -f nohup.out | grep "ERROR"
只显示某个接口或线程:
tail -f nohup.out | grep "OrderService"
多个关键字(或关系):
tail -f nohup.out | grep -E "ERROR|WARN"
排除某些内容:
tail -f nohup.out | grep -v "DEBUG"
nohup java -jar app.jar > app.log 2>&1 &
然后查看:
grep "ERROR" app.log
tee + grep(不常用)nohup java -jar app.jar 2>&1 | grep "ERROR" > error.log &
⚠️ 注意:这样会丢失非 ERROR 日志
sed -n '/2024-01-01 10:00/,/2024-01-01 11:00/p' nohup.out
tail -n 100 nohup.out | grep "ERROR"
示例(logback):
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>
logrotate避免 nohup.out 无限增大。
# 实时看错误
tail -f nohup.out | grep ERROR
# 统计错误次数
grep -c "ERROR" nohup.out
# 查看某用户相关日志
grep "user123" nohup.out
如果你能说明:
我可以给你更精确的命令。