Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions appengine/php72/laravel-framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions appengine/php72/laravel-framework/app-dbsessions.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
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.
DB_DATABASE: YOUR_DB_DATABASE
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
8 changes: 6 additions & 2 deletions appengine/php72/laravel-framework/app.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions appengine/php72/laravel-framework/config/logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]

],

Expand Down
3 changes: 3 additions & 0 deletions appengine/php72/laravel-framework/test/DeployDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
12 changes: 12 additions & 0 deletions appengine/php72/laravel-framework/test/DeployLaravelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
125 changes: 125 additions & 0 deletions appengine/php72/laravel-framework/test/DeployStackdriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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 DeployStackdriverTest extends TestCase
{
use DeployLaravelTrait;
use AppEngineDeploymentTrait;
use EventuallyConsistentTestTrait;

public static function beforeDeploy()
{
// ensure logging output is displayed in phpunit
self::$logger = new \Monolog\Logger('phpunit');

$tmpDir = self::createLaravelProject();

// Uncomment the stackdriver logging channel line in app.yaml
file_put_contents(
$tmpDir . '/app.yaml',
str_replace(
'# LOG_CHANNEL: stackdriver',
'LOG_CHANNEL: stackdriver',
file_get_contents(__DIR__ . '/../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 testLogging()
{
$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");
}, $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);
}
}
82 changes: 0 additions & 82 deletions appengine/php72/laravel-framework/test/DeployTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
Expand All @@ -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');
});
}
}