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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ script:
- if [ ${TRAVIS_PHP_VERSION} == '5.5' ]; then composer install; fi;
- if [ ${TRAVIS_PHP_VERSION} == '5.5' ]; then env PHP_CGI_PATH=`which php-cgi` LOCAL_TEST_TARGETS='app.yaml backend.yaml' phpunit; fi;
- popd
# run compute engine logging tests
- pushd compute/logging
- composer install
- phpunit
- popd

after_script:
- composer require "satooshi/php-coveralls:^1.0"
Expand Down
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions compute/logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Logging for Compute Engine

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)
5 changes: 5 additions & 0 deletions compute/logging/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"fluent/logger": "^1.0"
}
}
72 changes: 72 additions & 0 deletions compute/logging/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions compute/logging/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

# [START logging]
require_once __DIR__ . '/vendor/autoload.php';

use Fluent\Logger\FluentLogger;

$GLOBALS['logger'] = new FluentLogger('localhost', '24224');

function fluentd_exception_handler(Exception $e)
{
global $logger;

$msg = array(
'message' => $e->getMessage(),
'serviceContext' => array('service' => 'myapp'),
// ... add more metadata
);
$logger->post('myapp.errors', $msg);
}

set_exception_handler('fluentd_exception_handler');
# [END logging]
31 changes: 31 additions & 0 deletions compute/logging/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2016 Google Inc.

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.
-->
<phpunit bootstrap="test/bootstrap.php">
<testsuites>
<testsuite name="PHP GCE logging test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="../../build/logs/clover-gce-logging.xml"/>
</logging>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add whitelist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I need to whitelist anything, since there's only index.php, which gets tested.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for not explaining why. We should mark which files to measure the coverage. Otherwise phpunit thinks it should measure the coverage for everything including under vendor directory and we'll have very low coverage report :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed there was a 30% decrease in coverage without this

Whitelist has been added!

<filter>
<whitelist>
<file>index.php</file>
</whitelist>
</filter>
</phpunit>
4 changes: 4 additions & 0 deletions compute/logging/test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/mocks/FluentLogger.php';
36 changes: 36 additions & 0 deletions compute/logging/test/loggingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* 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.
*/


class compute_loggingTest extends PHPUnit_Framework_TestCase
{
public function testExceptionHandler()
{
include __DIR__ . '/../index.php';
global $logger;

$lastHandler = set_exception_handler(null);
$this->assertEquals('fluentd_exception_handler', $lastHandler);

$exceptionMessage = 'testing exception handler';
$e = new Exception($exceptionMessage);
$lastHandler($e);
$this->assertEquals($logger->prefix, 'myapp.errors');
$this->assertEquals($exceptionMessage, $logger->msg['message']);
$this->assertEquals($logger->msg['serviceContext'], ['service' => 'myapp']);
}
}
15 changes: 15 additions & 0 deletions compute/logging/test/mocks/FluentLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Fluent\Logger;

class FluentLogger
{
public $prefix;
public $msg;

public function post($prefix, $msg)
{
$this->prefix = $prefix;
$this->msg = $msg;
}
}