diff --git a/appengine/php72/laravel-framework/README.md b/appengine/php72/laravel-framework/README.md index b55c52c466..601125af82 100644 --- a/appengine/php72/laravel-framework/README.md +++ b/appengine/php72/laravel-framework/README.md @@ -33,9 +33,9 @@ laravel.com. This version was tested to work with `laravel/laravel-framework:^5. env_variables: # Put production environment variables here. - APP_LOG: errorlog APP_KEY: YOUR_APP_KEY APP_STORAGE: /tmp + VIEW_COMPILED_PATH: /tmp 1. Copy the [`bootstrap/app.php`](bootstrap/app.php) file included in this sample into the `bootstrap` directory of your Laravel application. This file ensures @@ -107,9 +107,9 @@ Laravel, you need to manually add the `DB_SOCKET` value to env_variables: # Put production environment variables here. - APP_LOG: errorlog APP_KEY: YOUR_APP_KEY APP_STORAGE: /tmp + VIEW_COMPILED_PATH: /tmp CACHE_DRIVER: database SESSION_DRIVER: database ## Set these environment variables according to your CloudSQL configuration. @@ -172,6 +172,18 @@ You can write logs to Stackdriver Logging from PHP applications by using the Sta ], ``` +1. Finally, set the environment variable `LOG_CHANNEL` in `app.yaml` to + `stackdriver` to use the Stackdriver logger you created: + + ```yaml + runtime: php72 + + env_variables: + # Put production environment variables here. + LOG_CHANNEL: stackdriver + #... + ``` + 1. Now you can log to Stackdriver logging anywhere in your application! ```php diff --git a/appengine/php72/laravel-framework/app-dbsessions.yaml b/appengine/php72/laravel-framework/app-dbsessions.yaml index e728188114..99da8f1d80 100644 --- a/appengine/php72/laravel-framework/app-dbsessions.yaml +++ b/appengine/php72/laravel-framework/app-dbsessions.yaml @@ -1,10 +1,10 @@ runtime: php72 env_variables: - # Put production environment variables here. - APP_LOG: errorlog + ## Put production environment variables here. APP_KEY: YOUR_APP_KEY APP_STORAGE: /tmp + VIEW_COMPILED_PATH: /tmp CACHE_DRIVER: database SESSION_DRIVER: database ## Set these environment variables according to your CloudSQL configuration. @@ -12,3 +12,8 @@ env_variables: DB_USERNAME: YOUR_DB_USERNAME DB_PASSWORD: YOUR_DB_PASSWORD DB_SOCKET: "/cloudsql/YOUR_CLOUDSQL_CONNECTION_NAME" + + ## To use Stackdriver logging in your Laravel application, copy + ## "app/Logging/CreateStackdriverLogger.php" and "config/logging.php" + ## into your Laravel application. Then uncomment the following line: + # LOG_CHANNEL: stackdriver \ No newline at end of file diff --git a/appengine/php72/laravel-framework/app.yaml b/appengine/php72/laravel-framework/app.yaml index b54d0be7ba..fdb8029ae3 100644 --- a/appengine/php72/laravel-framework/app.yaml +++ b/appengine/php72/laravel-framework/app.yaml @@ -1,8 +1,12 @@ runtime: php72 env_variables: - # Put production environment variables here. - LOG_CHANNEL: stackdriver + ## Put production environment variables here. APP_KEY: YOUR_APP_KEY APP_STORAGE: /tmp VIEW_COMPILED_PATH: /tmp + + ## To use Stackdriver logging in your Laravel application, copy + ## "app/Logging/CreateStackdriverLogger.php" and "config/logging.php" + ## into your Laravel application. Then uncomment the following line: + # LOG_CHANNEL: stackdriver diff --git a/appengine/php72/laravel-framework/config/logging.php b/appengine/php72/laravel-framework/config/logging.php index a5e5007e17..53cb1c1fed 100644 --- a/appengine/php72/laravel-framework/config/logging.php +++ b/appengine/php72/laravel-framework/config/logging.php @@ -66,11 +66,13 @@ 'level' => 'debug', ], + # [START] Add Stackdriver Logging and Error Reporting to your Laraval application 'stackdriver' => [ 'driver' => 'custom', 'via' => App\Logging\CreateStackdriverLogger::class, 'level' => 'debug', ], + # [END] ], diff --git a/appengine/php72/laravel-framework/test/DeployDatabaseTest.php b/appengine/php72/laravel-framework/test/DeployDatabaseTest.php index a52a6c595f..85e5691aa2 100644 --- a/appengine/php72/laravel-framework/test/DeployDatabaseTest.php +++ b/appengine/php72/laravel-framework/test/DeployDatabaseTest.php @@ -29,6 +29,9 @@ class DeployDatabaseTest extends TestCase public static function beforeDeploy() { + // ensure logging output is displayed in phpunit + self::$logger = new \Monolog\Logger('phpunit'); + $connection = self::requireEnv('LARAVEL_CLOUDSQL_CONNECTION_NAME'); $dbName = self::requireEnv('LARAVEL_DB_DATABASE'); $dbUser = self::requireEnv('LARAVEL_DB_USERNAME'); diff --git a/appengine/php72/laravel-framework/test/DeployLaravelTrait.php b/appengine/php72/laravel-framework/test/DeployLaravelTrait.php index 6ce58fb221..6a50e6c52e 100644 --- a/appengine/php72/laravel-framework/test/DeployLaravelTrait.php +++ b/appengine/php72/laravel-framework/test/DeployLaravelTrait.php @@ -46,6 +46,18 @@ private static function createLaravelProject() self::$gcloudWrapper->setDir($tmpDir); chdir($tmpDir); + // fix "beyondcode/laravel-dump-server" issue + file_put_contents( + 'composer.json', + json_encode( + array_merge_recursive( + json_decode(file_get_contents('composer.json'), true), + ['extra' => ['laravel' => ['dont-discover' => 'beyondcode/laravel-dump-server']]] + ), + JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES + ) + ); + return $tmpDir; } diff --git a/appengine/php72/laravel-framework/test/DeployStackdriverTest.php b/appengine/php72/laravel-framework/test/DeployStackdriverTest.php new file mode 100644 index 0000000000..41c727671b --- /dev/null +++ b/appengine/php72/laravel-framework/test/DeployStackdriverTest.php @@ -0,0 +1,125 @@ + self::getProjectId() + ]); + + $token = uniqid(); + // The routes are defined in routes/web.php + $resp = $this->client->request('GET', "/log/$token", [ + 'http_errors' => false + ]); + $this->assertEquals('200', $resp->getStatusCode(), 'log page status code'); + + // 'app' is the default logname of our Stackdriver Logging integration. + $logger = $logging->logger('app'); + $this->runEventuallyConsistentTest(function () use ($logger, $token) { + $logs = $logger->entries([ + 'pageSize' => 100, + 'orderBy' => 'timestamp desc', + 'resultLimit' => 100 + ]); + $found = false; + foreach ($logs as $log) { + $info = $log->info(); + if (false !== strpos($info['jsonPayload']['message'], "token: $token")) { + $found = true; + } + } + $this->assertTrue($found, "The log entry $token was not found"); + }, $eventuallyConsistentRetryCount = 5); + } + + public function testErrorReporting() + { + $logging = new LoggingClient([ + 'projectId' => self::getProjectId() + ]); + + $token = uniqid(); + // The routes are defined in routes/web.php + $resp = $this->client->request('GET', "/exception/$token", [ + 'http_errors' => false + ]); + $this->assertEquals('500', $resp->getStatusCode(), 'exception page status code'); + + // 'app-error' is the default logname of our Stackdriver Error Reporting integration. + $logger = $logging->logger('app-error'); + $this->runEventuallyConsistentTest(function () use ($logger, $token) { + $logs = $logger->entries([ + 'pageSize' => 100, + 'orderBy' => 'timestamp desc', + 'resultLimit' => 100 + ]); + $found = false; + foreach ($logs as $log) { + $info = $log->info(); + if (false !== strpos($info['jsonPayload']['message'], "token: $token")) { + $found = true; + } + } + $this->assertTrue($found, 'The log entry was not found'); + }, $eventuallyConsistentRetryCount = 5); + } +} diff --git a/appengine/php72/laravel-framework/test/DeployTest.php b/appengine/php72/laravel-framework/test/DeployTest.php index cc3ca5e175..502be8f2a3 100644 --- a/appengine/php72/laravel-framework/test/DeployTest.php +++ b/appengine/php72/laravel-framework/test/DeployTest.php @@ -17,16 +17,13 @@ namespace Google\Cloud\Samples\AppEngine\Laravel; -use Google\Cloud\Logging\LoggingClient; use Google\Cloud\TestUtils\AppEngineDeploymentTrait; -use Google\Cloud\TestUtils\EventuallyConsistentTestTrait; use PHPUnit\Framework\TestCase; class DeployTest extends TestCase { use DeployLaravelTrait; use AppEngineDeploymentTrait; - use EventuallyConsistentTestTrait; public static function beforeDeploy() { @@ -36,17 +33,6 @@ public static function beforeDeploy() $tmpDir = self::createLaravelProject(); copy(__DIR__ . '/../app.yaml', $tmpDir . '/app.yaml'); self::addAppKeyToAppYaml($tmpDir); - - mkdir("$tmpDir/app/Logging", 0700, true); - self::copyFiles([ - 'routes/web.php', - 'config/logging.php', - 'app/Exceptions/Handler.php', - 'app/Logging/CreateStackdriverLogger.php', - ], $tmpDir); - - // require google cloud logging and error reporting dependencies - self::execute('composer require google/cloud-logging google/cloud-error-reporting'); } public function testHomepage() @@ -56,72 +42,4 @@ public function testHomepage() $this->assertEquals('200', $resp->getStatusCode(), 'top page status code'); $this->assertContains('Laravel', $resp->getBody()->getContents()); } - - public function testLogging() - { - // bump up the retry count because logs can take a bit to show up - $this->eventuallyConsistentRetryCount = 5; - - $logging = new LoggingClient([ - 'projectId' => self::getProjectId() - ]); - - $token = uniqid(); - // The routes are defined in routes/web.php - $resp = $this->client->request('GET', "/log/$token", [ - 'http_errors' => false - ]); - $this->assertEquals('200', $resp->getStatusCode(), 'log page status code'); - - // 'app' is the default logname of our Stackdriver Logging integration. - $logger = $logging->logger('app'); - $this->runEventuallyConsistentTest(function () use ($logger, $token) { - $logs = $logger->entries([ - 'pageSize' => 100, - 'orderBy' => 'timestamp desc', - 'resultLimit' => 100 - ]); - $found = false; - foreach ($logs as $log) { - $info = $log->info(); - if (false !== strpos($info['jsonPayload']['message'], "token: $token")) { - $found = true; - } - } - $this->assertTrue($found, "The log entry $token was not found"); - }); - } - - public function testErrorReporting() - { - $this->eventuallyConsistentRetryCount = 5; - $logging = new LoggingClient([ - 'projectId' => self::getProjectId() - ]); - - $token = uniqid(); - // The routes are defined in routes/web.php - $resp = $this->client->request('GET', "/exception/$token", [ - 'http_errors' => false - ]); - $this->assertEquals('500', $resp->getStatusCode(), 'exception page status code'); - - // 'app-error' is the default logname of our Stackdriver Error Reporting integration. - $logger = $logging->logger('app-error'); - $this->runEventuallyConsistentTest(function () use ($logger, $token) { - $logs = $logger->entries([ - 'pageSize' => 100, - 'orderBy' => 'timestamp desc', - 'resultLimit' => 100 - ]); - $found = false; - foreach ($logs as $log) { - $info = $log->info(); - if (false !== strpos($info['jsonPayload']['message'], "token: $token")) { - $found = true; - } - } - $this->assertTrue($found, 'The log entry was not found'); - }); - } }