Skip to content

Commit

Permalink
Issue #283: log bad API calls to logs/api-errors.log with Monolog
Browse files Browse the repository at this point in the history
- new INSTALL_ROOT/logs folder to store logs
- API errors are logged in api-errors.log
- The general logging file is now at logs/transvision.log
- Monolog library updated to 1.10
- bash scripts no longer create transvision.log because Monolog will create it if it doesn't exist
- logs folder in .gitignore
  • Loading branch information
pascalchevrel committed Jun 24, 2014
1 parent 7e76f62 commit 693bbb3
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -4,7 +4,6 @@ composer.phar
phpDocumentor.phar
stats.json
stats_requests.json
transvision.log
vendor
web/docs
app/config/config.ini
Expand All @@ -15,3 +14,4 @@ web/download/*.tmx
web/download/.htaccess
cache/*.cache
cache/lastdataupdate.txt
logs/*.log
33 changes: 27 additions & 6 deletions app/classes/Transvision/API.php
@@ -1,6 +1,10 @@
<?php
namespace Transvision;

use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\ErrorLogHandler;

/**
* API class
*
Expand Down Expand Up @@ -33,20 +37,36 @@
*/
Class API
{
public $url;
public $parameters;
public $extra_parameters;
public $api_versions = ['v1' => 'stable'];
public $services = ['entity', 'locales', 'repositories', 'search', 'tm', 'versions'];
public $logging = true;
public $error;
public $logging = true;
public $logger;

/**
* The constructor analyzes the URL to extract its parameters
*
* @param string $url URL representing the request to analyze
* @param array $url parsed url
*/
public function __construct($url)
{
$this->url = $url;

// We use the Monolog library to log our events
$this->logger = new Logger('API');

if ($this->logging) {
$this->logger->pushHandler(new StreamHandler(INSTALL_ROOT . 'logs/api-errors.log'));
}

// Also log to error console in Debug mode
if (DEBUG) {
$this->logger->pushHandler(new ErrorLogHandler());
}

$this->parameters = $this->getParameters($url['path']);
$this->extra_parameters = isset($url['query'])
? $this->getExtraParameters($url['query'])
Expand Down Expand Up @@ -332,15 +352,16 @@ private function isValidService()

/**
* Utility function to log API call errors.
* We may want to use Monolog in the future.
*
* @param string $message
* @return boolean True if we can log, False if we can't
* @return boolean True if we logged, False if we didn't log
*/
private function log($message, $option=false)
private function log($message)
{
$this->error = $message;

return $this->logging ? error_log($message) : true;
return $this->logging
? $this->logger->addWarning($message, [$this->url['path']])
: false;
}
}
2 changes: 1 addition & 1 deletion app/inc/init.php
Expand Up @@ -31,7 +31,7 @@

// Logging
$logger = new Logger(VERSION);
$logger->pushHandler(new StreamHandler(INSTALL_ROOT . 'transvision.log', Logger::DEBUG));
$logger->pushHandler(new StreamHandler(INSTALL_ROOT . 'logs/transvision.log', Logger::DEBUG));

// Dispatch urls, use it only in web context
if (php_sapi_name() != 'cli') {
Expand Down
3 changes: 1 addition & 2 deletions app/scripts/dev-setup.sh
Expand Up @@ -37,8 +37,7 @@ echogreen "Installing PHP dependencies with Composer"
php -r "readfile('https://getcomposer.org/installer');" | php
php composer.phar install

echogreen "Add log files"
touch transvision.log
echogreen "Add stats.json file"
touch web/stats.json

echogreen "Launching PHP development server (php -S localhost:8080 in the web folder)"
Expand Down
3 changes: 1 addition & 2 deletions app/scripts/setup.sh
Expand Up @@ -302,6 +302,5 @@ if [ ! -d $install/web/download ]
fi
echo "AddType application/octet-stream .tmx" > $install/web/download/.htaccess

echogreen "Add log files"
touch $install/transvision.log
echogreen "Add stats.json file"
touch $install/web/stats.json
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -2,7 +2,7 @@
"require": {
"atoum/atoum" : "dev-master",
"pascalc/tinyl10n" : "0.6",
"monolog/monolog" : "1.3.0"
"monolog/monolog" : "1.10.0"
},

"require-dev": {
Expand Down
1 change: 1 addition & 0 deletions logs/README
@@ -0,0 +1 @@
This folder contains all the logs for Transvision. We use Monolog, we can create as many log files as we wish.
2 changes: 2 additions & 0 deletions tests/units/bootstrap.php
@@ -1,6 +1,8 @@
<?php
define('TMX', realpath(__DIR__ . '/../testfiles/TMX/') . '/');
define('CACHE_PATH', realpath(__DIR__ . '/../testfiles/cache/') . '/');
define('INSTALL_ROOT', realpath(__DIR__ . '/../') . '/');
const DEBUG = true;
const CACHE_ENABLED = true;
const CACHE_TIME = 19200;

Expand Down

0 comments on commit 693bbb3

Please sign in to comment.