1
+ <?php
2
+ /**
3
+ * Logger.php
4
+ * data-api
5
+ * @author: Matthew License, B023339
6
+ * @date: 2013/10
7
+ */
8
+
9
+ namespace Hugo \Data \Application ;
10
+
11
+ use Psr \Log \LogLevel ,
12
+ Psr \Log \LoggerInterface ,
13
+ Hugo \Data \Model \LogItem ,
14
+ Hugo \Data \Storage \DataSource ,
15
+ Hugo \Data \Storage \FileSystem ,
16
+ Psr \Log \InvalidArgumentException ;
17
+
18
+ class Logger implements LoggerInterface
19
+ {
20
+
21
+ /**
22
+ * @var \Hugo\Data\Storage\DataSource
23
+ */
24
+ protected $ store ;
25
+
26
+ /**
27
+ * @var array
28
+ */
29
+ protected $ logs = [];
30
+
31
+ public function __construct (DataSource $ store = null )
32
+ {
33
+ if (null === $ store ) {
34
+ // if no DataSource is declared, we'll use a FileSystem
35
+ $ store = new FileSystem ('/media/vagrant/www/api.hugowolferton.co.uk/logs/api.log ' );
36
+ }
37
+ $ this ->store = $ store ;
38
+ }
39
+
40
+ public function log ($ level , $ message , array $ context = [])
41
+ {
42
+ switch ($ level ) {
43
+ case LogLevel::EMERGENCY :
44
+ case LogLevel::CRITICAL :
45
+ case LogLevel::ERROR :
46
+ case LogLevel::ALERT :
47
+ case LogLevel::WARNING :
48
+ case LogLevel::INFO :
49
+ case LogLevel::NOTICE :
50
+ case LogLevel::DEBUG :
51
+ return $ this ->_log ($ level , $ message , $ context );
52
+ break ;
53
+ default :
54
+ throw new InvalidArgumentException ("Logging level {$ level } doesn't exist " );
55
+ }
56
+ }
57
+
58
+ public function getLogs ()
59
+ {
60
+ return $ this ->logs ;
61
+ }
62
+
63
+ public function resetLogs ()
64
+ {
65
+ $ this ->logs = [];
66
+ }
67
+
68
+ private function _log ($ level , $ message , array $ context = [])
69
+ {
70
+ $ log = new LogItem ($ this ->store );
71
+ $ log ->set (['level ' => $ level , 'message ' => $ message , 'context ' => $ context ]);
72
+ $ this ->logs [] = trim ((string )($ log ));
73
+ if ($ log ->save ()) {
74
+ return $ log ;
75
+ } else {
76
+ return false ;
77
+ }
78
+ }
79
+
80
+ public function emergency ($ message , array $ context = [])
81
+ {
82
+ $ this ->_log (LogLevel::EMERGENCY , $ message , $ context );
83
+ }
84
+
85
+ public function alert ($ message , array $ context = [])
86
+ {
87
+ $ this ->_log (LogLevel::ALERT , $ message , $ context );
88
+ }
89
+
90
+ public function critical ($ message , array $ context = [])
91
+ {
92
+ $ this ->_log (LogLevel::CRITICAL , $ message , $ context );
93
+ }
94
+
95
+ public function error ($ message , array $ context = [])
96
+ {
97
+ $ this ->_log (LogLevel::ERROR , $ message , $ context );
98
+ }
99
+
100
+ public function warning ($ message , array $ context = [])
101
+ {
102
+ $ this ->_log (LogLevel::WARNING , $ message , $ context );
103
+ }
104
+
105
+ public function notice ($ message , array $ context = [])
106
+ {
107
+ $ this ->_log (LogLevel::NOTICE , $ message , $ context );
108
+ }
109
+
110
+ public function info ($ message , array $ context = [])
111
+ {
112
+ $ this ->_log (LogLevel::INFO , $ message , $ context );
113
+ }
114
+
115
+ public function debug ($ message , array $ context = [])
116
+ {
117
+ $ this ->_log (LogLevel::DEBUG , $ message , $ context );
118
+ }
119
+
120
+ }
0 commit comments