From 669feed6f7919248e8e7b2da40e92373925aae4c Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 13 Apr 2016 13:47:01 -0700 Subject: [PATCH 1/2] adds compute engine logging sample --- compute/{ => helloworld}/README.md | 0 compute/{ => helloworld}/app.php | 0 compute/logging/README.md | 3 ++ compute/logging/composer.json | 5 +++ compute/logging/composer.lock | 72 ++++++++++++++++++++++++++++++ compute/logging/index.php | 22 +++++++++ 6 files changed, 102 insertions(+) rename compute/{ => helloworld}/README.md (100%) rename compute/{ => helloworld}/app.php (100%) create mode 100644 compute/logging/README.md create mode 100644 compute/logging/composer.json create mode 100644 compute/logging/composer.lock create mode 100644 compute/logging/index.php diff --git a/compute/README.md b/compute/helloworld/README.md similarity index 100% rename from compute/README.md rename to compute/helloworld/README.md diff --git a/compute/app.php b/compute/helloworld/app.php similarity index 100% rename from compute/app.php rename to compute/helloworld/app.php diff --git a/compute/logging/README.md b/compute/logging/README.md new file mode 100644 index 0000000000..21fe81e77a --- /dev/null +++ b/compute/logging/README.md @@ -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) diff --git a/compute/logging/composer.json b/compute/logging/composer.json new file mode 100644 index 0000000000..66932a5ba5 --- /dev/null +++ b/compute/logging/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "fluent/logger": "^1.0" + } +} diff --git a/compute/logging/composer.lock b/compute/logging/composer.lock new file mode 100644 index 0000000000..9223167ae7 --- /dev/null +++ b/compute/logging/composer.lock @@ -0,0 +1,72 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "27dad74d408b0b848d82c498ce4cf3ef", + "content-hash": "1ac6bef12b52bca1e2bc43b46f30bc1e", + "packages": [ + { + "name": "fluent/logger", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/fluent/fluent-logger-php.git", + "reference": "360ad86483847f81ba25a79aa183520d790f6fca" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/fluent/fluent-logger-php/zipball/360ad86483847f81ba25a79aa183520d790f6fca", + "reference": "360ad86483847f81ba25a79aa183520d790f6fca", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.3", + "phpunit/phpunit-mock-objects": "2.3.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Fluent\\Logger\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache" + ], + "authors": [ + { + "name": "Shuhei Tanuma", + "email": "chobieee@gmail.com" + }, + { + "name": "Sotaro Karasawa", + "email": "sotarok@crocos.co.jp" + }, + { + "name": "DQNEO", + "email": "dqneoo@gmail.com" + } + ], + "description": "a logging library for Fluentd", + "homepage": "http://github.com/fluent/fluent-logger-php", + "keywords": [ + "log", + "logging" + ], + "time": "2015-10-16 05:38:10" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/compute/logging/index.php b/compute/logging/index.php new file mode 100644 index 0000000000..e7d96e5472 --- /dev/null +++ b/compute/logging/index.php @@ -0,0 +1,22 @@ + $e->getMessage(), + 'serviceContext' => array('service' => 'myapp'), + // ... add more metadata + ); + $logger->post('myapp.errors', $msg); +} + +set_exception_handler('fluentd_exception_handler'); +# [END logging] From 784bbc5d7c4077dad5beda048488f255a7b93cb2 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 14 Apr 2016 17:13:54 -0700 Subject: [PATCH 2/2] adds tests --- .travis.yml | 5 +++ compute/logging/index.php | 3 +- compute/logging/phpunit.xml | 31 ++++++++++++++++++ compute/logging/test/bootstrap.php | 4 +++ compute/logging/test/loggingTest.php | 36 +++++++++++++++++++++ compute/logging/test/mocks/FluentLogger.php | 15 +++++++++ 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 compute/logging/phpunit.xml create mode 100644 compute/logging/test/bootstrap.php create mode 100644 compute/logging/test/loggingTest.php create mode 100644 compute/logging/test/mocks/FluentLogger.php diff --git a/.travis.yml b/.travis.yml index 030975b163..73a49f78ab 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/compute/logging/index.php b/compute/logging/index.php index e7d96e5472..b1a49a8712 100644 --- a/compute/logging/index.php +++ b/compute/logging/index.php @@ -5,11 +5,12 @@ use Fluent\Logger\FluentLogger; -$logger = new FluentLogger('localhost', '24224'); +$GLOBALS['logger'] = new FluentLogger('localhost', '24224'); function fluentd_exception_handler(Exception $e) { global $logger; + $msg = array( 'message' => $e->getMessage(), 'serviceContext' => array('service' => 'myapp'), diff --git a/compute/logging/phpunit.xml b/compute/logging/phpunit.xml new file mode 100644 index 0000000000..76410eaec3 --- /dev/null +++ b/compute/logging/phpunit.xml @@ -0,0 +1,31 @@ + + + + + + test + + + + + + + + index.php + + + diff --git a/compute/logging/test/bootstrap.php b/compute/logging/test/bootstrap.php new file mode 100644 index 0000000000..d7d0417d4b --- /dev/null +++ b/compute/logging/test/bootstrap.php @@ -0,0 +1,4 @@ +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']); + } +} diff --git a/compute/logging/test/mocks/FluentLogger.php b/compute/logging/test/mocks/FluentLogger.php new file mode 100644 index 0000000000..8dbaf61ab9 --- /dev/null +++ b/compute/logging/test/mocks/FluentLogger.php @@ -0,0 +1,15 @@ +prefix = $prefix; + $this->msg = $msg; + } +}