-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.php
113 lines (101 loc) · 3.3 KB
/
Logger.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
<?php
namespace PSRLogger;
/*
* Logger gets all activity
* and outputs it to a file, console window,
* MySQL DB or any other stream.
*
* @author SN
*/
class Logger extends AbstractLogger
{
const DB_CONNECT_INCLUDE_PATH = 'db/db.php';
const DB_CONFIG_INCLUDE_PATH = 'db/db.config.php';
static private $ofStream;
static private $link;
static private $ostreamIsFile;
static private $ostreamIsMySQL;
static private $ostreamIsSTDOUT;
public function __construct(bool $_ostreamIsFile = true, bool $_ostreamIsMySQL = false, bool $_ostreamIsSTDOUT = false)
{
if ($_ostreamIsFile)
{
self::$ostreamIsFile = true;
}
if ($_ostreamIsMySQL)
{
self::$ostreamIsMySQL = true;
}
if ($_ostreamIsSTDOUT)
{
self::$ostreamIsSTDOUT = true;
}
try
{
if (!$_ostreamIsFile && !$_ostreamIsMySQL && !$_ostreamIsSTDOUT)
{
throw new Exception("ATTENTION! You chose no output stream! Logs will not be kept!");
}
}
catch (Exception $ex)
{
// This catch is excessive (not needed). Made for demo purposes
echo "Exception: " . $ex->getMessage() . "\n";
echo "in file: " . $ex->getFile() . "\n";
echo "in line: " . $ex->getLine() . "\n";
}
}
static public function setLogFile(string $fileName, string $path = null)
{
if($path != null && !is_dir($path))
{
return false;
}
self::$ofStream = fopen($path == null ? __DIR__.'/'.$fileName.'.log' : $path.'/'.$fileName,'a+');
return true;
}
static public function selectDB($HOST = null, $LOGIN = null, $PASSWORD = null, $DBNAME = null)
{
if (!$HOST || !$LOGIN || !$DBNAME)
{
require self::DB_CONFIG_INCLUDE_PATH;
}
require self::DB_CONNECT_INCLUDE_PATH;
return self::$link;
}
static public function log($level, $msg, array $context = [])
{
$t = date('Y-m-d H:i:s');
$tWrapped = '['.$t.'] ';
$levelWrapped = '['.$level.']';
$contextMessage = self::interpolate($msg, $context);
$reportStr = $tWrapped.'['.$levelWrapped.']'.$contextMessage."\n";
self::write($reportStr, $t, $level, $contextMessage);
}
static private function interpolate($msg, array $context = [])
{
// Wrapping array "context" keys with {}.
$replace = [];
foreach ($context as $key => $value)
{
$replace['{' . $key . '}'] = $value;
}
return strtr($msg, $replace);
}
static private function write($reportStr, $t, $level, $contextMessage)
{
if (self::$ostreamIsFile)
{
fwrite(self::$ofStream, $reportStr);
}
if (self::$ostreamIsMySQL)
{
$SQL = "INSERT INTO `logs`(`date`, `level`, `msg`) VALUES ('$t', '$level', '$contextMessage')";
mysqli_query(self::$link, $SQL);
}
if (self::$ostreamIsSTDOUT)
{
fwrite(STDOUT, $reportStr);
}
}
}