-
Notifications
You must be signed in to change notification settings - Fork 3
/
Module.php
114 lines (91 loc) · 3.13 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Logger;
use Zend\Http\PhpEnvironment\RemoteAddress;
use Zend\EventManager\StaticEventManager;
use Zend\Mvc\MvcEvent;
use Zend\Log\Writer\Db as DbWriter;
use Zend\Log\Writer\Stream as StreamWriter;
use Zend\Log\Logger;
use Zend\Log\Filter\Priority as PriorityFilter;
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php'
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__
)
)
);
}
public function onBootstrap(MvcEvent $e)
{
$events = StaticEventManager::getInstance();
$serviceManager = $e->getApplication()->getServiceManager();
$appConfig = $serviceManager->get('Config');
$logger = new Logger();
if(!isset($appConfig['logger'])) {
throw new \RuntimeException("Logger not properly configured");
}
if(!isset($appConfig['logger']['priority_filter'])) {
throw new \RuntimeException("You must specify a 'priority_filter' config param");
}
$logFilter = new PriorityFilter($appConfig['logger']['priority_filter']);
if(!is_null($appConfig['logger']['db_adapter'])) {
if((empty($appConfig['logger']['logger_table']))) {
throw new \RuntimeException("You must specify a 'logger_table' config param");
}
$dbAdapter = $serviceManager->get($appConfig['logger']['db_adapter']);
if(!$dbAdapter instanceof \Zend\Db\Adapter\Adapter) {
throw new \RuntimeException("Failed to load database adapter for logger");
}
$tableMapping = array(
'timestamp' => 'event_date',
'priorityName' => 'priority',
'message' => 'event',
'extra' => array(
'source' => 'source',
'uri' => 'uri',
'ip' => 'ip',
'session_id' => 'session_id'
)
);
$logWriter = new DbWriter($dbAdapter, $appConfig['logger']['logger_table'], $tableMapping);
$logWriter->addFilter($logFilter);
$logger->addWriter($logWriter);
}
if(isset($appConfig['logger']['log_file']) && !is_null($appConfig['logger']['log_file'])) {
$streamWriter = new StreamWriter($appConfig['logger']['log_file']);
$streamWriter->addFilter($logFilter);
$logger->addWriter($streamWriter);
}
$request = $e->getApplication()->getRequest();
$remoteAddress = new RemoteAddress();
Logger::registerErrorHandler($logger, true);
Logger::registerExceptionHandler($logger);
$events->attach("*", 'log', function(\Zend\EventManager\Event $e) use ($logger, $request, $remoteAddress) {
$targetClass = get_class($e->getTarget());
$message = $e->getParam('message', "[No Message Provided]");
$priority = $e->getParam('priority', Logger::INFO);
$extras = array(
'source' => $targetClass,
'uri' => $request->getUriString(),
'ip' => $remoteAddress->getIpAddress(),
'session_id' => session_id()
);
$logger->log($priority, $message, $extras);
});
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getServiceConfig()
{
return array();
}
}