如何访问 Heroku 上的 CakePHP 日志文件?

2021-12-21 00:00:00 heroku php cakephp

我已将 CakePHP 应用程序部署到 Heroku.CakePHP 默认将其日志写入 APP_ROOT/app/tmp/logs/error.logAPP_ROOT/app/tmp/logs/debug.log 但因为没有办法获取正在运行的 Heroku Web dyno 的 shell,我看不到这些文件的内容.

I've deployed a CakePHP application to Heroku. CakePHP writes its logs in APP_ROOT/app/tmp/logs/error.log and APP_ROOT/app/tmp/logs/debug.log by default but since there's no way to get a shell to a running Heroku web dyno, I can't see the content of those files.

据我所知,heroku logs 命令返回已转储到 STDERRSTDOUT 的所有内容.如果我是对的,有没有办法强制 CakePHP 将其日志发送到 STDOUT?

As I understand it, the heroku logs command returns everything which has been dumped to STDERR and STDOUT. If I'm right about that, is there a way to force CakePHP to send its logs to STDOUT?

Heroku PHP Buildpack 将 Apache 和 PHP 日志文件作为后台进程跟踪为dyno 设置的一部分.见下文.

The Heroku PHP Buildpack tails the Apache and PHP log files as a background process as part of the dyno setup. See below.

cat >>boot.sh <<EOF
for var in `env|cut -f1 -d=`; do
  echo "PassEnv $var" >> /app/apache/conf/httpd.conf;
done
touch /app/apache/logs/error_log
touch /app/apache/logs/access_log
tail -F /app/apache/logs/error_log &
tail -F /app/apache/logs/access_log &
export LD_LIBRARY_PATH=/app/php/ext
export PHP_INI_SCAN_DIR=/app/www
echo "Launching apache"
exec /app/apache/bin/httpd -DNO_DETACH
EOF

在该构建包的一个分支中,我在适当的位置添加了自己的行,然后将我的应用程序配置为使用我的自定义构建包.

In a fork of that build pack, I added in my own lines in the appropriate positions, then configured my app to use my custom build pack.

touch /app/www/tmp/logs/error.log
tail -F /app/www/app/tmp/logs/error.log &

但这没有用.事实上,撇开 CakePHP 的细节不谈,我在 heroku 日志 中也看不到任何 PHP 或 Apache 日志内容.

But this didn't work. In fact, setting aside CakePHP specifics, I don't see any PHP or Apache log contents in the heroku logs either.

推荐答案

在最新版本的 CakePHP (>= 3.6.0) 中,config/app.php 文件预先配置为对股票 debugerror 日志使用环境变量覆盖.这些变量应包含定义相同的 DSN 样式字符串您通常会放置在 config/app.php 文件中的属性.

In the latest versions of CakePHP (>= 3.6.0), the config/app.php file comes pre-configured to use an environment variable override for the stock debug and error logs. These vars should contain a DSN-style string defining the same attributes you would normally place in your config/app.php file.

在设置(或通过heroku cli工具)下的Heroku仪表板中,您可以添加以下ENV变量:

In the Heroku dashboard under Settings (or via the heroku cli tool), you can add the following ENV vars:

LOG_DEBUG_URL = file:///?className=Cake%5CLog%5CEngine%5CConsoleLog&levels[]=notice&levels[]=info&levels[]=debug
LOG_ERROR_URL = file:///?className=Cake%5CLog%5CEngine%5CConsoleLog&levels[]=warning&levels[]=error&levels[]=critical&levels[]=alert&levels[]=emergency

以上两行复制了 CakePHP 的日志记录,但将所有输出重定向到控制台而不是文件.

The above two lines replicate the stock CakePHP logging, but redirect all output to the console instead of files.

相关文章