简体   繁体   中英

How can I access CakePHP log files on Heroku?

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.

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 ?

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 &

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.

I think the following might work. Make sure you're using 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')
));

From the Heroku documentation: https://devcenter.heroku.com/articles/php-logging#cakephp

In your application configuration, instruct CakePHP to use the ConsoleLog engine for your logger setups:

CakeLog::config('default', array(
    'engine' => 'ConsoleLog',
));

You can then use the regular logging methods.

CakeLog::warning("Hello, this is a test message!");

Refer to the Logging section of the CakePHP manual for more information.

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.

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

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

you can use: '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),
    ],
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM