Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

Commit d460a6b

Browse files
author
Stefano Kowalke
committed
Logging with the new logging API
Resolves: #30
1 parent 8bd7223 commit d460a6b

File tree

5 files changed

+253
-43
lines changed

5 files changed

+253
-43
lines changed

Classes/Command/BackendApiCommandController.php

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,55 @@
3232
* API Command Controller
3333
*/
3434
class BackendApiCommandController extends CommandController {
35+
36+
/**
37+
* @var \TYPO3\CMS\Core\Log\LogManager $logManager
38+
*/
39+
protected $logManager;
40+
41+
/**
42+
* @var \TYPO3\CMS\Core\Log\Logger $logger
43+
*/
44+
protected $logger;
45+
46+
/**
47+
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
48+
*
49+
* @return void
50+
*/
51+
public function injectLogManager(\TYPO3\CMS\Core\Log\LogManager $logManager) {
52+
$this->logManager = $logManager;
53+
}
54+
55+
/**
56+
* Initialize the object
57+
*/
58+
public function initializeObject() {
59+
$this->logger = $this->objectManager->get('\TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
60+
}
61+
3562
/**
3663
* Locks backend access for all users by writing a lock file that is checked when the backend is accessed.
3764
*
3865
* @param string $redirectUrl URL to redirect to when the backend is accessed
3966
*/
4067
public function lockCommand($redirectUrl = NULL) {
4168
if (@is_file((PATH_typo3conf . 'LOCK_BACKEND'))) {
42-
$this->outputLine('A lockfile already exists. Overwriting it...');
69+
$message = 'A lockfile already exists. Overwriting it...';
70+
$this->outputLine($message);
71+
$this->logger->info($message);
4372
}
4473

4574
\TYPO3\CMS\Core\Utility\GeneralUtility::writeFile(PATH_typo3conf . 'LOCK_BACKEND', (string)$redirectUrl);
4675

4776
if ($redirectUrl === NULL) {
48-
$this->outputLine('Wrote lock file to \'typo3conf/LOCK_BACKEND\'');
77+
$message = 'Wrote lock file to \'typo3conf/LOCK_BACKEND\'';
78+
$this->outputLine($message);
79+
$this->logger->info($message);
4980
} else {
50-
$this->outputLine('Wrote lock file to \'typo3conf/LOCK_BACKEND\' with instruction to redirect to: \'' . $redirectUrl . '\'');
81+
$message = 'Wrote lock file to \'typo3conf/LOCK_BACKEND\' with instruction to redirect to: \'' . $redirectUrl . '\'';
82+
$this->outputLine($message);
83+
$this->logger->info($message);
5184
}
5285
}
5386

@@ -58,13 +91,19 @@ public function unlockCommand() {
5891
if (@is_file((PATH_typo3conf . 'LOCK_BACKEND'))) {
5992
unlink(PATH_typo3conf . 'LOCK_BACKEND');
6093
if (@is_file((PATH_typo3conf . 'LOCK_BACKEND'))) {
61-
$this->outputLine('ERROR: Could not remove lock file \'typo3conf/LOCK_BACKEND\'!');
94+
$message = 'ERROR: Could not remove lock file \'typo3conf/LOCK_BACKEND\'!';
95+
$this->outputLine($message);
96+
$this->logger->error($message);
6297
$this->quit(1);
6398
} else {
64-
$this->outputLine('Removed lock file \'typo3conf/LOCK_BACKEND\'');
99+
$message = 'Removed lock file \'typo3conf/LOCK_BACKEND\'';
100+
$this->outputLine($message);
101+
$this->logger->info($message);
65102
}
66103
} else {
67-
$this->outputLine('No lock file \'typo3conf/LOCK_BACKEND\' was found, hence no lock could be removed.');
104+
$message = 'No lock file \'typo3conf/LOCK_BACKEND\' was found, hence no lock could be removed.';
105+
$this->outputLine($message);
106+
$this->logger->info($message);
68107
$this->quit(1);
69108
}
70109
}

Classes/Command/CacheApiCommandController.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@
3434
* @package Etobi\CoreAPI\Service\SiteApiService
3535
*/
3636
class CacheApiCommandController extends CommandController {
37+
/**
38+
* @var \TYPO3\CMS\Core\Log\LogManager $logManager
39+
*/
40+
protected $logManager;
41+
42+
/**
43+
* @var \TYPO3\CMS\Core\Log\Logger $logger
44+
*/
45+
protected $logger;
46+
47+
/**
48+
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
49+
*
50+
* @return void
51+
*/
52+
public function injectLogManager(\TYPO3\CMS\Core\Log\LogManager $logManager) {
53+
$this->logManager = $logManager;
54+
}
55+
56+
/**
57+
* Initialize the object
58+
*/
59+
public function initializeObject() {
60+
$this->logger = $this->objectManager->get('\TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
61+
}
3762

3863
/**
3964
* @var \Etobi\CoreAPI\Service\CacheApiService
@@ -56,7 +81,9 @@ public function injectCacheApiService(\Etobi\CoreAPI\Service\CacheApiService $ca
5681
*/
5782
public function clearAllCachesCommand() {
5883
$this->cacheApiService->clearAllCaches();
59-
$this->outputLine('All caches have been cleared.');
84+
$message = 'All caches have been cleared.';
85+
$this->logger->info($message);
86+
$this->outputLine($message);
6087
}
6188

6289
/**
@@ -66,7 +93,9 @@ public function clearAllCachesCommand() {
6693
*/
6794
public function clearSystemCacheCommand() {
6895
$this->cacheApiService->clearSystemCache();
69-
$this->outputLine('System cache has been cleared');
96+
$message = 'System cache has been cleared';
97+
$this->logger->info($message);
98+
$this->outputLine($message);
7099
}
71100

72101
/**
@@ -81,9 +110,13 @@ public function clearAllActiveOpcodeCacheCommand($fileAbsPath = NULL) {
81110
$this->cacheApiService->clearAllActiveOpcodeCache($fileAbsPath);
82111

83112
if ($fileAbsPath !== NULL) {
84-
$this->outputLine(sprintf('The opcode cache for the file %s has been cleared', $fileAbsPath));
113+
$message = sprintf('The opcode cache for the file %s has been cleared', $fileAbsPath);
114+
$this->outputLine($message);
115+
$this->logger->info($message);
85116
} else {
86-
$this->outputLine('The complete opcode cache has been cleared');
117+
$message = 'The complete opcode cache has been cleared';
118+
$this->outputLine($message);
119+
$this->logger->info($message);
87120
}
88121
}
89122

@@ -94,7 +127,9 @@ public function clearAllActiveOpcodeCacheCommand($fileAbsPath = NULL) {
94127
*/
95128
public function clearConfigurationCacheCommand() {
96129
$this->cacheApiService->clearConfigurationCache();
97-
$this->outputLine('Configuration cache has been cleared.');
130+
$message = 'Configuration cache has been cleared.';
131+
$this->logger->info($message);
132+
$this->outputLine($message);
98133
}
99134

100135
/**
@@ -104,7 +139,9 @@ public function clearConfigurationCacheCommand() {
104139
*/
105140
public function clearPageCacheCommand() {
106141
$this->cacheApiService->clearPageCache();
107-
$this->outputLine('Page cache has been cleared.');
142+
$message = 'Page cache has been cleared.';
143+
$this->logger->info($message);
144+
$this->outputLine($message);
108145
}
109146

110147
/**
@@ -115,6 +152,8 @@ public function clearPageCacheCommand() {
115152
*/
116153
public function clearAllExceptPageCacheCommand() {
117154
$clearedCaches = $this->cacheApiService->clearAllExceptPageCache();
118-
$this->outputLine('Cleared caches: ' . implode(', ', $clearedCaches));
155+
$message = 'Cleared caches: ' . implode(', ', $clearedCaches);
156+
$this->logger->info($message);
157+
$this->outputLine($message);
119158
}
120159
}

Classes/Command/DatabaseApiCommandController.php

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,48 @@
3535
*/
3636
class DatabaseApiCommandController extends CommandController {
3737

38+
/**
39+
* @var \TYPO3\CMS\Core\Log\LogManager $logManager
40+
*/
41+
protected $logManager;
42+
43+
/**
44+
* @var \TYPO3\CMS\Core\Log\Logger $logger
45+
*/
46+
protected $logger;
47+
48+
/**
49+
* @param \TYPO3\CMS\Core\Log\LogManager $logManager
50+
*
51+
* @return void
52+
*/
53+
public function injectLogManager(\TYPO3\CMS\Core\Log\LogManager $logManager) {
54+
$this->logManager = $logManager;
55+
}
56+
57+
/**
58+
* Initialize the object
59+
*/
60+
public function initializeObject() {
61+
$this->logger = $this->objectManager->get('\TYPO3\CMS\Core\Log\LogManager')->getLogger(__CLASS__);
62+
}
63+
64+
/**
65+
* @var \Etobi\CoreAPI\Service\DatabaseApiService $databaseApiService
66+
*/
67+
protected $databaseApiService;
68+
69+
/**
70+
* Injects the DatabaseApiService object
71+
*
72+
* @param \Etobi\CoreAPI\Service\DatabaseApiService $databaseApiService
73+
*
74+
* @return void
75+
*/
76+
public function injectDatabaseApiService(\Etobi\CoreAPI\Service\DatabaseApiService $databaseApiService) {
77+
$this->databaseApiService = $databaseApiService;
78+
}
79+
3880
/**
3981
* Database compare.
4082
* Leave the argument 'actions' empty or use "help" to see the available ones
@@ -43,44 +85,41 @@ class DatabaseApiCommandController extends CommandController {
4385
* @param bool $dry
4486
*/
4587
public function databaseCompareCommand($actions = '', $dry = FALSE) {
46-
$service = $this->getService();
47-
4888
if ($actions === 'help' || strlen($actions) === 0) {
49-
$actions = $service->databaseCompareAvailableActions();
89+
$actions = $this->databaseApiService->databaseCompareAvailableActions();
5090
foreach ($actions as $number => $action) {
5191
$this->outputLine(' - ' . $action . ' => ' . $number);
5292
}
5393
$this->quit();
5494
}
5595

56-
$result = $service->databaseCompare($actions, $dry);
96+
$result = $this->databaseApiService->databaseCompare($actions, $dry);
5797

5898
if ($dry) {
5999
$this->outputLine('DB compare would execute the following queries:');
60100
foreach($result as $key => $set) {
61101
$this->outputLine(sprintf('### Action: %s ###', $key));
62102
$this->outputLine('===================================');
103+
$this->logger->info(sprintf('### Action: %s ###', $key));
104+
$this->logger->info('===================================');
63105
foreach($set as $line) {
64106
$this->outputLine($line);
107+
$this->logger->info($line);
65108
}
66109
$this->outputLine(LF);
67110
}
111+
$this->logger->info('DB compare executed in dry mode');
68112
} else {
69113
if (empty($result)) {
70-
$this->outputLine('DB has been compared');
114+
$message = 'DB has been compared';
115+
$this->outputLine($message);
116+
$this->logger->info($message);
71117
} else {
72-
$this->outputLine('DB could not be compared, Error(s): %s', array(LF . implode(LF, $result)));
118+
$message = sprintf('DB could not be compared, Error(s): %s', array(LF . implode(LF, $result)));
119+
$this->outputLine($message);
120+
$this->logger->error($message);
73121
$this->quit(1);
74122
}
75123
}
76124
}
77-
78-
/**
79-
* Returns the service object.
80-
*
81-
* @return \Etobi\CoreAPI\Service\DatabaseApiService object
82-
*/
83-
private function getService() {
84-
return $this->objectManager->get('Etobi\\CoreAPI\\Service\\DatabaseApiService');
85-
}
86125
}

0 commit comments

Comments
 (0)