clienttools_syslog.php
4.55 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
class Clienttools_Syslog{
/** The default folder in which to put the log files **/
private static $logFolder='../../var/log/';
private static $debugLogTemplate='[date] | [time] | INFO | [session] | [user] | [location] | [debug_message] | ([debug_data])';
private static $traceLogTemplate='[date] | [time] | LOG | [session] | [user] | [location] | [trace_message]';
private static $errorLogTemplate='[date] | [time] | ERROR | [session] | [user] | [location] | [error_detail] | ([error])';
/**
* Parse an array into a string template
* @param $template The template string - array keys in square brackets : [date] | [time] | ERROR | [session] | [user] | [location] | [error_detail] | ([error])
* @param $data The associative array to parse into template. Keys will replace [keys] in template string.
* @return string The parsed template string
*/
private static function parseTemplate($template=NULL,$data=NULL){
$ret=null;
if(is_array($data)){
$txs=array_keys($data);
foreach($txs as $idx=>$val){
$txs[$idx]='['.$val.']';
}
$txd=array_values($data);
$ret=str_replace($txs,$txd,$template);
};
return $ret;
}
/**
* Return the calculated log file name
* @return void
*/
private static function getLogFile(){
$fileName=self::$logFolder.'kt_clienttools_'.date('Y-m-d').'.log.txt';
return $fileName;
}
/**
* Write a line to the log file.
* @param $line
* @return void
*/
private static function writeLogLine($line=NULL){
if($line){
$fp=fopen(self::getLogFile(),'a');
fwrite($fp,$line."\n");
fclose($fp);
}
}
/**
* Return a boolean indicating whether error logging should be done
* @return boolean
*/
private static function doErrorLogging(){
return KTConfig::getSingleton()->get('explorerCPSettings/debugLevel')=='error' || self::doDebugLogging();
}
/**
* Return a boolean indicating whether debug logging should be done
* @return boolean
*/
private static function doDebugLogging(){
return KTConfig::getSingleton()->get('explorerCPSettings/debugLevel')=='debug';
}
/**
* Store a line in the log file.. the message and a json string containing the data information will be stored
* @param $user The logged in user
* @param $location Information about the location from whence the function was called
* @param $message The descriptive message explaining the debug data that follows
* @param $data The debug data - this will be converted to a json string.
* @return void
*/
public static function logInfo($user,$location,$message,$data){
if(self::doDebugLogging()){
list($usec, $sec) = explode(" ", microtime());
$usec=ceil($usec*1000);
$entry=self::parseTemplate(self::$debugLogTemplate,array(
'date' =>date('Y-m-d'),
'time' =>date('h:i:s').':'.$usec,
'user' =>$user,
'session'=>session_id(),
'location'=>$location,
'debug_message'=>$message,
'debug_data'=>json_encode($data)
));
self::writeLogLine($entry);
}
}
/**
* Store a line in the log file.. A simple string to indicate a point in the software
* @param $user The logged in user
* @param $location Information about the location from whence the function was called
* @param $message A string indicating a point reached in the software
* @return void
*/
public static function logTrace($user,$location,$message){
if(self::doDebugLogging()){
list($usec, $sec) = explode(" ", microtime());
$usec=ceil($usec*1000);
$entry=self::parseTemplate(self::$traceLogTemplate,array(
'date' =>date('Y-m-d'),
'time' =>date('h:i:s').':'.$usec,
'user' =>$user,
'session'=>session_id(),
'location'=>$location,
'trace_message'=>$message,
));
self::writeLogLine($entry);
}
}
/**
* Store a line in the log file.. An Error log
* @param $user The logged in user
* @param $location Information about the location from whence the function was called
* @param $detail A string providing information as to the context of the encountered error
* @param $err The exception object - this will be serialized
* @return void
*/
public static function logError($user=NULL,$location=NULL,$detail=NULL,$err=NULL){
if(self::doErrorLogging()){
list($usec, $sec) = explode(" ", microtime());
$usec=ceil($usec*1000);
$entry=self::parseTemplate(self::$errorLogTemplate,array(
'date' =>date('Y-m-d'),
'time' =>date('h:i:s').':'.$usec,
'user' =>$user,
'session'=>session_id(),
'location'=>$location,
'error_detail'=>json_encode($detail),
'error'=>json_encode($err),
));
self::writeLogLine($entry);
}
}
}
?>