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
| <?php
trait logging {
private static $LOG_ERROR = 100;
private static $LOG_WARNING = 50;
private static $LOG_NOTICE = 10;
protected $log_location = 'output.log';
protected function log($level, $msg) {
$output = [];
$output[] = "Class: ".__CLASS__.' | ';
$output[] = "Level: {$level} | ";
$output = array_merge($output, (array)$msg, ["\n"]);
file_put_contents($this->log_location, $output, FILE_APPEND);
}
}
class User {
use logging;
public function __construct() {
$this->log(self:LOG_NOTICE, 'New User Created');
}
}
class DB {
use logging;
private $db = 'localhost:8080';
public function connect() {
// ... attempt to connect and fail:
$this->log(self:LOG_ERROR, ['Connection Failed-', $this->db]);
}
}
|