forked from emoncms/emoncms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmonLogger.php
69 lines (59 loc) · 1.94 KB
/
EmonLogger.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
<?php
/*
All Emoncms code is released under the GNU Affero General Public License.
See COPYRIGHT.txt and LICENSE.txt.
---------------------------------------------------------------------
Emoncms - open source energy visualisation
Part of the OpenEnergyMonitor project:
http://openenergymonitor.org
*/
// no direct access
defined('EMONCMS_EXEC') or die('Restricted access');
class EmonLogger
{
private $logfile = "";
private $caller = "";
private $logenabled = false;
public function __construct($clientFileName)
{
global $log_filename, $log_enabled;
if (!$log_enabled) {
$this->logenabled = false;
}
else if ($log_filename) {
$this->logfile = $log_filename;
$this->caller = basename($clientFileName);
if (!file_exists($this->logfile))
{
$fh = @fopen($this->logfile,"a");
@fclose($fh);
}
if (is_writable($this->logfile)) $this->logenabled = true;
}
}
public function info ($message){
$this->write("INFO",$message);
}
public function warn ($message){
$this->write("WARN",$message);
}
public function error ($message){
$this->write("ERROR",$message);
}
private function write($type,$message){
if (!$this->logenabled) return;
$now = microtime(true);
$micro = sprintf("%03d",($now - ($now >> 0)) * 1000);
$now = DateTime::createFromFormat('U', (int)$now); // Only use UTC for logs
$now = $now->format("Y-m-d H:i:s").".$micro";
// Clear log file if more than 256MB (temporary solution)
if (filesize($this->logfile)>(1024*1024*256)) {
$fh = @fopen($this->logfile,"w");
@fclose($fh);
}
if ($fh = @fopen($this->logfile,"a")) {
@fwrite($fh,$now."|$type|$this->caller|".$message."\n");
@fclose($fh);
}
}
}