⬆️ Ajoute Monolog

This commit is contained in:
Clement Desmidt 2017-11-15 22:59:41 +01:00
parent 1da668485f
commit 5b3500e193
3 changed files with 156 additions and 21 deletions

View File

@ -3,7 +3,8 @@
"description": "Manage deals in LBC", "description": "Manage deals in LBC",
"type": "project", "type": "project",
"require": { "require": {
"shikiryu/lbcreposter-api": "0.*" "shikiryu/lbcreposter-api": "0.*",
"monolog/monolog": "^1.23"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^5.7" "phpunit/phpunit": "^5.7"

139
composer.lock generated
View File

@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "1f27833b156f7e100122870f2bf123e3", "hash": "da5cf0fa9c9ce6bd3e2984db7c5f28b8",
"content-hash": "818e49fe8e9ffc7b1f4a8e88edc1f278", "content-hash": "95d6dcaa6784485a4831674141dabf0d",
"packages": [ "packages": [
{ {
"name": "fabpot/goutte", "name": "fabpot/goutte",
@ -237,6 +237,84 @@
], ],
"time": "2017-03-20 17:10:46" "time": "2017-03-20 17:10:46"
}, },
{
"name": "monolog/monolog",
"version": "1.23.0",
"source": {
"type": "git",
"url": "https://github.com/Seldaek/monolog.git",
"reference": "fd8c787753b3a2ad11bc60c063cff1358a32a3b4"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Seldaek/monolog/zipball/fd8c787753b3a2ad11bc60c063cff1358a32a3b4",
"reference": "fd8c787753b3a2ad11bc60c063cff1358a32a3b4",
"shasum": ""
},
"require": {
"php": ">=5.3.0",
"psr/log": "~1.0"
},
"provide": {
"psr/log-implementation": "1.0.0"
},
"require-dev": {
"aws/aws-sdk-php": "^2.4.9 || ^3.0",
"doctrine/couchdb": "~1.0@dev",
"graylog2/gelf-php": "~1.0",
"jakub-onderka/php-parallel-lint": "0.9",
"php-amqplib/php-amqplib": "~2.4",
"php-console/php-console": "^3.1.3",
"phpunit/phpunit": "~4.5",
"phpunit/phpunit-mock-objects": "2.3.0",
"ruflin/elastica": ">=0.90 <3.0",
"sentry/sentry": "^0.13",
"swiftmailer/swiftmailer": "^5.3|^6.0"
},
"suggest": {
"aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB",
"doctrine/couchdb": "Allow sending log messages to a CouchDB server",
"ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)",
"ext-mongo": "Allow sending log messages to a MongoDB server",
"graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server",
"mongodb/mongodb": "Allow sending log messages to a MongoDB server via PHP Driver",
"php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib",
"php-console/php-console": "Allow sending log messages to Google Chrome",
"rollbar/rollbar": "Allow sending log messages to Rollbar",
"ruflin/elastica": "Allow sending log messages to an Elastic Search server",
"sentry/sentry": "Allow sending log messages to a Sentry server"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Monolog\\": "src/Monolog"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jordi Boggiano",
"email": "j.boggiano@seld.be",
"homepage": "http://seld.be"
}
],
"description": "Sends your logs to files, sockets, inboxes, databases and various web services",
"homepage": "http://github.com/Seldaek/monolog",
"keywords": [
"log",
"logging",
"psr-3"
],
"time": "2017-06-19 01:22:40"
},
{ {
"name": "psr/http-message", "name": "psr/http-message",
"version": "1.0.1", "version": "1.0.1",
@ -287,13 +365,60 @@
], ],
"time": "2016-08-06 14:39:51" "time": "2016-08-06 14:39:51"
}, },
{
"name": "psr/log",
"version": "1.0.2",
"source": {
"type": "git",
"url": "https://github.com/php-fig/log.git",
"reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
"reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Log\\": "Psr/Log/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for logging libraries",
"homepage": "https://github.com/php-fig/log",
"keywords": [
"log",
"psr",
"psr-3"
],
"time": "2016-10-10 12:19:37"
},
{ {
"name": "shikiryu/lbcreposter-api", "name": "shikiryu/lbcreposter-api",
"version": "dev-master", "version": "0.1.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.shikiryu.com/Shikiryu/LBCReposter-api.git", "url": "https://git.shikiryu.com/Shikiryu/LBCReposter-api.git",
"reference": "768af6e636c33af05e7a9a28aa5625bee13514de" "reference": "ac21f74c11878955e86e74115f70408e590936f5"
}, },
"require": { "require": {
"fabpot/goutte": "^3.1" "fabpot/goutte": "^3.1"
@ -315,7 +440,7 @@
} }
], ],
"description": "Manage deals in LBC", "description": "Manage deals in LBC",
"time": "2017-09-30 13:42:28" "time": "2017-11-07 20:24:59"
}, },
{ {
"name": "symfony/browser-kit", "name": "symfony/browser-kit",
@ -1860,9 +1985,7 @@
], ],
"aliases": [], "aliases": [],
"minimum-stability": "stable", "minimum-stability": "stable",
"stability-flags": { "stability-flags": [],
"shikiryu/lbcreposter-api": 20
},
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": [], "platform": [],

View File

@ -1,12 +1,22 @@
<?php <?php
include __DIR__ . '/vendor/autoload.php'; include __DIR__ . '/vendor/autoload.php';
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
define('APP_DIR', __DIR__); define('APP_DIR', __DIR__);
define('DEALS_DIR', sprintf('%s/deals', APP_DIR)); define('DEALS_DIR', sprintf('%s/deals', APP_DIR));
define('LOGS_DIR', sprintf('%s/logs', APP_DIR)); define('LOGS_DIR', sprintf('%s/logs', APP_DIR));
define('NEW_DEALS_DIR', sprintf('%s/deals/new', APP_DIR)); define('NEW_DEALS_DIR', sprintf('%s/deals/new', APP_DIR));
define('EXISTING_DEALS_DIR', sprintf('%s/deals/list', APP_DIR)); define('EXISTING_DEALS_DIR', sprintf('%s/deals/list', APP_DIR));
define('BACKUP_DEALS_DIR', sprintf('%s/deals/backup', APP_DIR)); define('BACKUP_DEALS_DIR', sprintf('%s/deals/backup', APP_DIR));
define('LOG_MAIL_TO', '');
define('LOG_MAIL_SUBJECT', '[LBCReposter] erreur cron');
define('LOG_MAIL_FROM', '');
$log = new Logger('lbcreposter');
$log->pushHandler(new StreamHandler(LOGS_DIR.'/cron.log', Logger::WARNING));
$log->pushHandler(new \Monolog\Handler\NativeMailerHandler(LOG_MAIL_TO, LOG_MAIL_SUBJECT, LOG_MAIL_FROM));
$existing_deals = []; $existing_deals = [];
foreach (new DirectoryIterator(EXISTING_DEALS_DIR) as $existing_deal) { foreach (new DirectoryIterator(EXISTING_DEALS_DIR) as $existing_deal) {
@ -36,7 +46,7 @@ function delete_folder($folder) {
return rmdir($folder); return rmdir($folder);
} }
echo sprintf('%u annonces locales, normalement en ligne.%s', count($existing_deals), "\n"); $log->info(sprintf('%u annonces locales, normalement en ligne.', count($existing_deals)));
$script_params = parse_ini_file(sprintf('%s/lbcreposter.ini', DEALS_DIR), true); $script_params = parse_ini_file(sprintf('%s/lbcreposter.ini', DEALS_DIR), true);
$config = new \Shikiryu\LBCReposter\Config($script_params); $config = new \Shikiryu\LBCReposter\Config($script_params);
$account = new \Shikiryu\LBCReposter\Account($config); $account = new \Shikiryu\LBCReposter\Account($config);
@ -53,12 +63,12 @@ if ($actions->connect()) {
foreach ($deals as $i => $deal) { foreach ($deals as $i => $deal) {
// l'annonce existe déjà // l'annonce existe déjà
if (in_array($deal->getId(), array_keys($existing_deals))) { if (in_array($deal->getId(), array_keys($existing_deals))) {
echo sprintf('L\'annonce %s existe déjà, ', $deal->getId()); $log->info(sprintf('L\'annonce %s existe déjà, ', $deal->getId()));
// si elle est vieille, on supprime et on recréé // si elle est vieille, on supprime et on recréé
if ($existing_deals[$deal->getId()]->getDateCreation()->add(new DateInterval('P1W')) <= (new DateTime())) { if ($existing_deals[$deal->getId()]->getDateCreation()->add(new DateInterval('P1W')) <= (new DateTime())) {
// Suppression // Suppression
echo sprintf('elle est vieille (%s), on la supprime. %s', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i'), "\n"); $log->error(sprintf('elle est vieille (%s), on la supprime.', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i')));
$actions->delete($deal); // $actions->delete($deal);
// Backup // Backup
if (!file_exists(BACKUP_DEALS_DIR.'/'.$deal->getId())) { if (!file_exists(BACKUP_DEALS_DIR.'/'.$deal->getId())) {
mkdir(BACKUP_DEALS_DIR . '/' . $deal->getId()); mkdir(BACKUP_DEALS_DIR . '/' . $deal->getId());
@ -68,11 +78,11 @@ if ($actions->connect()) {
rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId()); rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId());
} else { } else {
// sinon on la laisse // sinon on la laisse
echo sprintf('et tout va bien. %s', "\n"); $log->info('et tout va bien.');
} }
unset($deals_to_backup[$deal->getId()]); unset($deals_to_backup[$deal->getId()]);
} else { } else {
echo sprintf('L\'annonce %s n\'existait pas et est sauvegardée. %s', $deal->getSubject(), "\n"); $log->error(sprintf('L\'annonce %s n\'existait pas et est sauvegardée.', $deal->getSubject()));
// Une annonce non sauvegardée ? on la sauve // Une annonce non sauvegardée ? on la sauve
$deal->save(EXISTING_DEALS_DIR); $deal->save(EXISTING_DEALS_DIR);
} }
@ -91,17 +101,18 @@ if ($actions->connect()) {
foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) { foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) {
if ($new_deal->isDir() && !$new_deal->isDot()) { if ($new_deal->isDir() && !$new_deal->isDot()) {
$deal_to_create = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json'); $deal_to_create = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json');
if ($actions->create($deal_to_create)) { /*if ($actions->create($deal_to_create)) {
delete_folder($new_deal->getRealPath()); delete_folder($new_deal->getRealPath());
echo sprintf('L\'annonce %s a bien été créée. %s', $deal_to_create->getSubject(), "\n"); $log->info(sprintf('L\'annonce %s a bien été créée.', $deal_to_create->getSubject()));
break; break;
} else { } else {
echo sprintf('L\'annonce %s n\'a pas bien été créée. %s', $deal_to_create->getSubject(), "\n"); $log->error(sprintf('L\'annonce %s n\'a pas bien été créée. %s', $deal_to_create->getSubject()));
} }*/
sleep(30); sleep(30);
} }
} }
} else { } else {
echo 'not connected'; $log->error('not connected');
} }
$log->info('CRON terminé !');