1: <?php
2:
3: namespace Charcoal\App\ServiceProvider;
4:
5:
6: use Pimple\ServiceProviderInterface;
7: use Pimple\Container;
8:
9:
10: use Psr\Log\NullLogger;
11:
12:
13: use Monolog\Logger;
14: use Monolog\Processor\MemoryUsageProcessor;
15: use Monolog\Processor\UidProcessor;
16: use Monolog\Handler\BrowserConsoleHandler;
17: use Monolog\Handler\StreamHandler;
18:
19:
20: use Charcoal\Factory\GenericFactory;
21:
22:
23: use Charcoal\App\Config\LoggerConfig;
24:
25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36: class LoggerServiceProvider implements ServiceProviderInterface
37: {
38: 39: 40: 41: 42: 43: 44: 45: 46:
47: public function register(Container $container)
48: {
49: 50: 51: 52:
53: $container['logger/config'] = function (Container $container) {
54: $config = $container['config'];
55:
56: $loggerConfig = new LoggerConfig($config['logger']);
57: return $loggerConfig;
58: };
59:
60: 61: 62:
63: $container['logger/processor/factory'] = function () {
64: return new GenericFactory([
65: 'map' => [
66: 'memory-usage' => MemoryUsageProcessor::class,
67: 'uid' => UidProcessor::class
68: ]
69: ]);
70: };
71:
72: 73: 74:
75: $container['logger/handler/stream'] = function (Container $container) {
76: $loggerConfig = $container['logger/config'];
77: $handlerConfig = $loggerConfig['handlers.stream'];
78: if ($handlerConfig['active'] !== true) {
79: return null;
80: }
81:
82: $level = $handlerConfig['level'] ?: $loggerConfig['level'];
83: return new StreamHandler($handlerConfig['stream'], $level);
84: };
85:
86: 87: 88:
89: $container['logger/handler/browser-console'] = function (Container $container) {
90: $loggerConfig = $container['logger/config'];
91: $handlerConfig = $loggerConfig['handlers.console'];
92: if ($handlerConfig['active'] !== true) {
93: return null;
94: }
95: $level = $handlerConfig['level'] ?: $loggerConfig['level'];
96: return new BrowserConsoleHandler($level);
97: };
98:
99: 100: 101:
102: $container['logger/handlers'] = function (Container $container) {
103: $loggerConfig = $container['logger/config'];
104:
105: $handlersConfig = $loggerConfig['handlers'];
106: $handlers = new Container();
107: $handlerFactory = $container['logger/handler/factory'];
108: foreach ($handlersConfig as $h) {
109: $handlers[$h['type']] = function () use ($h, $handlerFactory) {
110: $type = $h['type'];
111: $handler = $handlerFactory->create($type);
112: return $handler;
113: };
114: }
115: return $handlers;
116: };
117:
118: 119: 120: 121: 122: 123:
124: $container['logger'] = function (Container $container) {
125:
126: $loggerConfig = $container['logger/config'];
127:
128: if ($loggerConfig['active'] !== true) {
129: return new NullLogger();
130: }
131:
132: $logger = new Logger('Charcoal');
133:
134: $memProcessor = new MemoryUsageProcessor();
135: $logger->pushProcessor($memProcessor);
136:
137: $uidProcessor = new UidProcessor();
138: $logger->pushProcessor($uidProcessor);
139:
140: $consoleHandler = $container['logger/handler/browser-console'];
141: if ($consoleHandler) {
142: $logger->pushHandler($consoleHandler);
143: }
144:
145: $streamHandler = $container['logger/handler/stream'];
146: if ($streamHandler) {
147: $logger->pushHandler($streamHandler);
148: }
149: return $logger;
150: };
151: }
152: }
153: