1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* small factory to get a logger class |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\ImportExport\Util; |
7
|
|
|
|
8
|
|
|
use Monolog\Formatter\LineFormatter; |
9
|
|
|
use Monolog\Handler\RavenHandler; |
10
|
|
|
use Monolog\Handler\StreamHandler; |
11
|
|
|
use Monolog\Logger; |
12
|
|
|
use Raven_Client; |
13
|
|
|
use Raven_ErrorHandler; |
14
|
|
|
use Symfony\Bridge\Monolog\Handler\ConsoleHandler; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author List of contributors <https://github.com/libgraviton/import-export/graphs/contributors> |
19
|
|
|
* @license https://opensource.org/licenses/MIT MIT License |
20
|
|
|
* @link http://swisscom.ch |
21
|
|
|
*/ |
22
|
|
|
class LoggerFactory |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* gets a logger instance |
27
|
|
|
* |
28
|
|
|
* @param OutputInterface|null $output output interface |
29
|
|
|
* |
30
|
|
|
* @return Logger a logger |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
6 |
|
public static function getInstance(OutputInterface $output = null) |
34
|
|
|
{ |
35
|
6 |
|
$output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
|
|
|
|
36
|
6 |
|
$sentryClient = new Raven_Client( |
37
|
6 |
|
null, |
38
|
|
|
[ |
39
|
6 |
|
'app_path' => __DIR__.'/../../', |
40
|
|
|
'tags' => ['application' => 'import-export'] |
41
|
|
|
] |
42
|
|
|
); |
43
|
|
|
|
44
|
6 |
|
$monologFormatter = new LineFormatter(null, 'Y-m-d H:i:sO'); |
45
|
|
|
|
46
|
6 |
|
if ($output instanceof OutputInterface) { |
47
|
6 |
|
$mainHandler = new ConsoleHandler($output, Logger::INFO); |
48
|
|
|
} else { |
49
|
|
|
$mainHandler = new StreamHandler('php://stdout', Logger::INFO); |
50
|
|
|
} |
51
|
|
|
|
52
|
6 |
|
$mainHandler->setFormatter($monologFormatter); |
53
|
|
|
|
54
|
6 |
|
$logger = new Logger('app'); |
55
|
6 |
|
$logger->pushHandler($mainHandler); |
56
|
6 |
|
$logger->pushHandler(new RavenHandler($sentryClient, Logger::WARNING)); |
57
|
|
|
|
58
|
6 |
|
$errorHandler = new Raven_ErrorHandler($sentryClient); |
59
|
6 |
|
$errorHandler->registerExceptionHandler(); |
60
|
6 |
|
$errorHandler->registerErrorHandler(); |
61
|
6 |
|
$errorHandler->registerShutdownFunction(); |
62
|
|
|
|
63
|
6 |
|
return $logger; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: