1919
2020namespace Phalcon \Logger \Adapter ;
2121
22- use Phalcon \Db \Column ;
2322use Phalcon \Logger \Exception ;
23+ use Phalcon \Logger \Formatter \FormatterInterface ;
24+ use Phalcon \Logger \Item ;
25+ use Phalcon \Db \Adapter \AdapterInterface as DbAdapterInterface ;
2426use Phalcon \Logger \Formatter \Line as LineFormatter ;
25- use Phalcon \Logger \Adapter as LoggerAdapter ;
26- use Phalcon \Logger \AdapterInterface ;
27- use Phalcon \Db \AdapterInterface as DbAdapterInterface ;
27+ use Phalcon \Db \Column ;
2828
2929/**
30- * Database Logger
30+ * Phalcon\ Logger\Adapter\Database
3131 *
3232 * Adapter to store logs in a database table
33- *
34- * @package Phalcon\Logger\Adapter
3533 */
36- class Database extends LoggerAdapter implements AdapterInterface
34+ class Database extends AbstractAdapter
3735{
36+ /**
37+ * Database connection
38+ *
39+ * @var DbAdapterInterface
40+ */
41+ protected $ db ;
42+
43+ /**
44+ * Table name
45+ *
46+ * @var string
47+ */
48+ protected $ table = "log " ;
49+
3850 /**
3951 * Name
4052 * @var string
4153 */
4254 protected $ name = 'phalcon ' ;
4355
4456 /**
45- * Adapter options
46- * @var array
57+ * @var \Phalcon\Logger\Formatter\AbstractFormatter
4758 */
48- protected $ options = [] ;
59+ protected $ _formatter ;
4960
5061 /**
51- * @var \Phalcon\Db\AdapterInterface
62+ * Adapter options
63+ * @var array
5264 */
53- protected $ db ;
65+ protected $ options = [] ;
5466
5567 /**
56- * Class constructor.
57- *
58- * @param string $name
59- * @param array $options
60- * @throws \Phalcon\Logger\Exception
68+ * Constructor. Accepts the name and some options
6169 */
62- public function __construct ($ name = 'phalcon ' , array $ options = [])
70+ public function __construct (string $ name = 'phalcon ' , array $ options = [])
6371 {
6472 if (!isset ($ options ['db ' ])) {
6573 throw new Exception ("Parameter 'db' is required " );
@@ -76,6 +84,7 @@ public function __construct($name = 'phalcon', array $options = [])
7684 }
7785
7886 $ this ->db = $ options ['db ' ];
87+ $ this ->table = $ options ['table ' ];
7988
8089 if ($ name ) {
8190 $ this ->name = $ name ;
@@ -87,64 +96,22 @@ public function __construct($name = 'phalcon', array $options = [])
8796 /**
8897 * Sets database connection
8998 *
90- * @param AdapterInterface $db
99+ * @param DbAdapterInterface $db
91100 * @return $this
92101 */
93- public function setDb (AdapterInterface $ db )
102+ public function setDb (DbAdapterInterface $ db )
94103 {
95104 $ this ->db = $ db ;
96105
97106 return $ this ;
98107 }
99108
100- /**
101- * {@inheritdoc}
102- *
103- * @return \Phalcon\Logger\FormatterInterface
104- */
105- public function getFormatter ()
106- {
107- if (!is_object ($ this ->_formatter )) {
108- $ this ->_formatter = new LineFormatter ('%message% ' );
109- }
110-
111- return $ this ->_formatter ;
112- }
113-
114- /**
115- * Writes the log to the file itself
116- *
117- * @param string $message
118- * @param integer $type
119- * @param integer $time
120- * @param array $context
121- * @return bool
122- */
123- public function logInternal ($ message , $ type , $ time , $ context = [])
124- {
125- return $ this ->db ->execute (
126- 'INSERT INTO ' . $ this ->options ['table ' ] . ' VALUES (null, ?, ?, ?, ?) ' ,
127- [
128- $ this ->name ,
129- $ type ,
130- $ this ->getFormatter ()->format ($ message , $ type , $ time , $ context ),
131- $ time ,
132- ],
133- [
134- Column::BIND_PARAM_STR ,
135- Column::BIND_PARAM_INT ,
136- Column::BIND_PARAM_STR ,
137- Column::BIND_PARAM_INT ,
138- ]
139- );
140- }
141-
142109 /**
143110 * {@inheritdoc}
144111 *
145112 * @return boolean
146113 */
147- public function close ()
114+ public function close (): bool
148115 {
149116 if ($ this ->db ->isUnderTransaction ()) {
150117 $ this ->db ->commit ();
@@ -160,7 +127,7 @@ public function close()
160127 *
161128 * @return $this
162129 */
163- public function begin ()
130+ public function begin (): AdapterInterface
164131 {
165132 $ this ->db ->begin ();
166133
@@ -172,7 +139,7 @@ public function begin()
172139 *
173140 * @return $this
174141 */
175- public function commit ()
142+ public function commit (): AdapterInterface
176143 {
177144 $ this ->db ->commit ();
178145
@@ -185,10 +152,46 @@ public function commit()
185152 *
186153 * @return $this
187154 */
188- public function rollback ()
155+ public function rollback (): AdapterInterface
189156 {
190157 $ this ->db ->rollback ();
191158
192159 return $ this ;
193160 }
194- }
161+
162+ /**
163+ * {@inheritdoc}
164+ *
165+ * @return FormatterInterface
166+ */
167+ public function getFormatter (): FormatterInterface
168+ {
169+ if (!is_object ($ this ->_formatter )) {
170+ $ this ->_formatter = new LineFormatter ('%message% ' );
171+ }
172+
173+ return $ this ->_formatter ;
174+ }
175+
176+ /**
177+ * Processes the message i.e. writes it to the file
178+ */
179+ public function process (Item $ item ): void
180+ {
181+ $ this ->db ->execute (
182+ 'INSERT INTO ' . $ this ->table . ' VALUES (null, ?, ?, ?, ?) ' ,
183+ [
184+ $ this ->name ,
185+ $ item ->getType (),
186+ $ this ->getFormatter ()->format ($ item ),
187+ $ item ->getTime (),
188+ ],
189+ [
190+ Column::BIND_PARAM_STR ,
191+ Column::BIND_PARAM_INT ,
192+ Column::BIND_PARAM_STR ,
193+ Column::BIND_PARAM_INT ,
194+ ]
195+ );
196+ }
197+ }
0 commit comments