Skip to content

Commit

Permalink
[TASK] Add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoBlack committed Jun 12, 2019
1 parent 7f6e550 commit 4f29a10
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ DB_HOST=127.0.0.1
DB_USER=root
DB_PASS=
DB_DBNAME=t3bot

# Logger
##################
LOG_FILE="/var/log/botty.log"
# DEBUG=100, INFO=200, NOTICE=250, WARNING=300, ERROR=400, CRITICAL=500, ALERT=550, EMERGENCY=600
LOG_LEVEL=400
4 changes: 4 additions & 0 deletions Classes/Controller/GerritHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

use T3Bot\Slack\Message;
use T3Bot\Traits\GerritTrait;
use T3Bot\Traits\LoggerTrait;

/**
* Class GerritHookController.
*/
class GerritHookController extends AbstractHookController
{
use GerritTrait;
use LoggerTrait;

/**
* public method to start processing the request.
Expand All @@ -26,9 +28,11 @@ class GerritHookController extends AbstractHookController
* @param string $input
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Exception
*/
public function process($hook, $input = 'php://input')
{
$this->getLogger()->debug(file_get_contents($input));
$json = json_decode(file_get_contents($input));

if (
Expand Down
6 changes: 6 additions & 0 deletions Classes/Controller/WebHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
namespace T3Bot\Controller;

use T3Bot\Slack\Message;
use T3Bot\Traits\LoggerTrait;

/**
* Class WebHookController.
*/
class WebHookController extends AbstractHookController
{
use LoggerTrait;

/**
* @var array
*/
Expand All @@ -42,10 +45,13 @@ class WebHookController extends AbstractHookController
* }
*
* @throws \Doctrine\DBAL\DBALException
* @throws \Exception
*/
public function process($hook, $input = 'php://input')
{
$entityBody = file_get_contents($input);
$this->getLogger()->debug($entityBody);

$json = json_decode($entityBody);
$hookConfiguration = $this->configuration['webhook'][$hook] ?? [];

Expand Down
32 changes: 32 additions & 0 deletions Classes/Traits/LoggerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* T3Bot.
*
* @author Frank Nägler <[email protected]>
*
* @link http://www.t3bot.de
* @link http://wiki.typo3.org/T3Bot
*/
namespace T3Bot\Traits;

use Exception;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

trait LoggerTrait
{
protected $logger;

/**
* @return Logger
* @throws Exception
*/
protected function getLogger(): Logger
{
if ($this->logger === null) {
$this->logger = new Logger('application');
$this->logger->pushHandler(new StreamHandler($GLOBALS['config']['log']['file'], (int)$GLOBALS['config']['log']['level']));
}
return $this->logger;
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"coderstephen/slack-client": "^0.3.0",
"doctrine/migrations": "^1.5",
"doctrine/dbal": "^2.5",
"vlucas/phpdotenv": "^2.4"
"vlucas/phpdotenv": "^2.4",
"monolog/monolog": "^1.24"
},
"require-dev": {
"phpunit/phpunit": "^4.8.0"
Expand Down
86 changes: 83 additions & 3 deletions composer.lock

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

4 changes: 4 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
'host' => getenv('DB_HOST'),
'driver' => 'pdo_mysql',
],
'log' => [
'file' => getenv('LOG_FILE'),
'level' => getenv('LOG_LEVEL'),
]
];

// change avatar of bot from 1.12. to 26.12.
Expand Down

0 comments on commit 4f29a10

Please sign in to comment.