我已经向Heroku部署了一个CakePHP应用程序。CakePHP默认在APP_ROOT/app/tmp/logs/error.log
和APP_ROOT/app/tmp/logs/debug.log
中写入日志,但由于没有办法将shell获取到正在运行的Heroku web dyno,因此我无法看到这些文件的内容。
据我所知,heroku logs
命令返回已转储到STDERR
和STDOUT
的所有内容。如果我是对的,有没有办法强迫CakePHP发送它的日志到STDOUT
?
Heroku PHP Buildpack跟踪Apache和PHP日志文件作为后台进程,作为动态设置的一部分。见下文.
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
在该构建包的分支中,我在适当的位置添加了我自己的行,然后配置我的应用程序来使用我的自定义构建包。
touch /app/www/tmp/logs/error.log
tail -F /app/www/app/tmp/logs/error.log &
但这不起作用。事实上,撇开CakePHP的细节不谈,我在heroku logs
中也没有看到任何PHP或Apache日志内容。
我认为下面的方法可能有效。请确保您使用的是CakePHP 2.3.9。
App::uses('ConsoleOutput', 'Console');
CakeLog::config('default', array(
'engine' => 'ConsoleLog',
'stream' => new ConsoleOutput('php://stdout')
));
CakeLog::config('stdout', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info'),
'stream' => new ConsoleOutput('php://stdout')
));
CakeLog::config('stderr', array(
'engine' => 'ConsoleLog',
'types' => array('emergency', 'alert', 'critical', 'error', 'warning', 'debug'),
'stream' => new ConsoleOutput('php://stderr')
));
CakeLog::config('debug', array(
'engine' => 'ConsoleLog',
'types' => array('notice', 'info', 'debug'),
'format' => 'debug %s: %s',
'stream' => new ConsoleOutput('php://stdout')
));
CakeLog::config('error', array(
'engine' => 'ConsoleLog',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'format' => 'error %s: %s',
'stream' => new ConsoleOutput('php://stderr')
));
来自Heroku文档:https://devcenter.heroku.com/articles/php-logging#cakephp
在您的应用程序配置中,指示CakePHP为您的记录器设置使用ConsoleLog引擎:
CakeLog::config('default', array(
'engine' => 'ConsoleLog',
));
你可以使用常规的日志记录方法。
CakeLog::warning("Hello, this is a test message!");
更多信息请参考CakePHP手册的日志部分
在最新版本的CakePHP(>= 3.6.0)中,config/app.php
文件被预先配置为对debug
和error
日志使用环境变量覆盖。这些变量应该包含一个dsn风格的字符串,它定义了通常放在config/app.php
文件中的相同属性。
在Heroku仪表板Settings下(或通过heroku
cli工具),可以添加以下ENV变量:
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日志记录,但是将所有输出重定向到控制台而不是文件。
您可以使用:'className' => 'Cake'Log'Engine'ConsoleLog'
/**
* Configures logging options
*/
'Log' => [
'debug' => [
'className' => 'Cake'Log'Engine'ConsoleLog',
'path' => LOGS,
'file' => 'debug',
'levels' => ['notice', 'info', 'debug'],
'url' => env('LOG_DEBUG_URL', null),
],
'error' => [
'className' => 'Cake'Log'Engine'ConsoleLog',
'path' => LOGS,
'file' => 'error',
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
'url' => env('LOG_ERROR_URL', null),
],
],