【问题标题】:using Sed with grep for the log file使用 Sed 和 grep 作为日志文件
【发布时间】:2021-05-01 13:45:21
【问题描述】:

我试图让 php 错误日志看起来很好 每当我执行 error_log($ex->getTraceAsString()) 但它打印了 \n 所以我想出了可能性,结果证明我应该从终端本身这样做

我是这样的

 tail -f /var/log/apache2/error.log | sed 's/\\n/\n/g'

但以前我的日志我曾经这样做

tail -f /var/log/apache2/error.log | grep production

对于包含关键字生产的日志

但是我想要他们两个我应该怎么做?所以我的两个标准都有效

我试过了

tail -f /var/log/apache2/error.log | grep production | sed 's/\\n/\n/g'
tail -f /var/log/apache2/error.log | sed 's/\\n/\n/g' | grep production 
(tail -f /var/log/apache2/error.log | grep production) | sed 's/\\n/\n/g'
(tail -f /var/log/apache2/error.log | sed 's/\\n/\n/g') | grep production 

但这些都不起作用。我该怎么办?

【问题讨论】:

    标签: php linux logging command-line terminal


    【解决方案1】:

    使用 /..../ 将搜索字符串添加到 sed 语句中:

    tail -f /var/log/apache2/error.log | sed -n '/production/s/\\n/\n/gp'
    

    【讨论】:

    • 这与tail -f /var/log/apache2/error.log | grep production | sed 's/\\n/\n/g'有何不同?
    • 它没有用,我得到了包含不包含生产行的日志
    • 道歉。您将需要-n。我已经修改了答案。
    • 嗨@RamanSailopal,抱歉,您的解决方案对{"time":"2021-01-28 19:26:07.402946", "function" : "[-:error]" 不起作用“进程”:“[pid25953]”,“消息”:“PHP 警告:get_headers(google.com/philips-for-sofware-technologist-ii):无法打开流:php_network_getaddresses:getaddrinfo 失败:名称或服务在 /var/www/production/file 中未知.php 在第 158 行","host":"production.google.org"}
    • 它包含生产,但如果我使用您编写的解决方案,则不会显示此行。虽然如果我做正常的 tail -f /var/log/apache2/error.log | grep 生产。它可以工作(但新行问题将无法解决)
    猜你喜欢
    • 2021-12-13
    • 2011-12-27
    • 1970-01-01
    • 2014-02-11
    • 1970-01-01
    • 2020-05-25
    • 2012-08-05
    • 1970-01-01
    • 2015-12-24
    相关资源
    最近更新 更多