Skip to content

Commit 64e332d

Browse files
committed
Merge pull request GoogleCloudPlatform#59 from GoogleCloudPlatform/gce-logging
adds compute engine logging sample
2 parents 1101142 + 784bbc5 commit 64e332d

File tree

11 files changed

+194
-0
lines changed

11 files changed

+194
-0
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ script:
8383
- if [ ${TRAVIS_PHP_VERSION} == '5.5' ]; then composer install; fi;
8484
- if [ ${TRAVIS_PHP_VERSION} == '5.5' ]; then env PHP_CGI_PATH=`which php-cgi` LOCAL_TEST_TARGETS='app.yaml backend.yaml' phpunit; fi;
8585
- popd
86+
# run compute engine logging tests
87+
- pushd compute/logging
88+
- composer install
89+
- phpunit
90+
- popd
8691

8792
after_script:
8893
- composer require "satooshi/php-coveralls:^1.0"
File renamed without changes.
File renamed without changes.

compute/logging/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Logging for Compute Engine
2+
3+
This app demonstrates how to log to Compute Engine from a PHP application. Full instructions at [https://cloud.google.com/error-reporting/docs/setting-up-on-compute-engine](https://cloud.google.com/error-reporting/docs/setting-up-on-compute-engine)

compute/logging/composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"require": {
3+
"fluent/logger": "^1.0"
4+
}
5+
}

compute/logging/composer.lock

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

compute/logging/index.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
3+
# [START logging]
4+
require_once __DIR__ . '/vendor/autoload.php';
5+
6+
use Fluent\Logger\FluentLogger;
7+
8+
$GLOBALS['logger'] = new FluentLogger('localhost', '24224');
9+
10+
function fluentd_exception_handler(Exception $e)
11+
{
12+
global $logger;
13+
14+
$msg = array(
15+
'message' => $e->getMessage(),
16+
'serviceContext' => array('service' => 'myapp'),
17+
// ... add more metadata
18+
);
19+
$logger->post('myapp.errors', $msg);
20+
}
21+
22+
set_exception_handler('fluentd_exception_handler');
23+
# [END logging]

compute/logging/phpunit.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
17+
<phpunit bootstrap="test/bootstrap.php">
18+
<testsuites>
19+
<testsuite name="PHP GCE logging test">
20+
<directory>testdirectory>
21+
testsuite>
22+
testsuites>
23+
<logging>
24+
<log type="coverage-clover" target="../../build/logs/clover-gce-logging.xml"/>
25+
logging>
26+
<filter>
27+
<whitelist>
28+
<file>index.phpfile>
29+
whitelist>
30+
filter>
31+
phpunit>

compute/logging/test/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
require_once __DIR__ . '/mocks/FluentLogger.php';

compute/logging/test/loggingTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/**
3+
* Copyright 2015 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
class compute_loggingTest extends PHPUnit_Framework_TestCase
20+
{
21+
public function testExceptionHandler()
22+
{
23+
include __DIR__ . '/../index.php';
24+
global $logger;
25+
26+
$lastHandler = set_exception_handler(null);
27+
$this->assertEquals('fluentd_exception_handler', $lastHandler);
28+
29+
$exceptionMessage = 'testing exception handler';
30+
$e = new Exception($exceptionMessage);
31+
$lastHandler($e);
32+
$this->assertEquals($logger->prefix, 'myapp.errors');
33+
$this->assertEquals($exceptionMessage, $logger->msg['message']);
34+
$this->assertEquals($logger->msg['serviceContext'], ['service' => 'myapp']);
35+
}
36+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
3+
namespace Fluent\Logger;
4+
5+
class FluentLogger
6+
{
7+
public $prefix;
8+
public $msg;
9+
10+
public function post($prefix, $msg)
11+
{
12+
$this->prefix = $prefix;
13+
$this->msg = $msg;
14+
}
15+
}

0 commit comments

Comments
 (0)