|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SA\CpeSdk; |
|
4
|
|
|
|
|
5
|
|
|
use SA\CpeSdk; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Allow formatted logging on STDOUT |
|
9
|
|
|
* Send logs to Syslog for log offloading |
|
10
|
|
|
*/ |
|
11
|
|
|
class CpeLogger |
|
12
|
|
|
{ |
|
13
|
|
|
public $logPath; |
|
14
|
|
|
public $printout; |
|
15
|
|
|
|
|
16
|
|
|
// Exception |
|
17
|
|
|
const LOG_TYPE_ERROR = "LOG_TYPE_ERROR"; |
|
18
|
|
|
const OPENLOG_ERROR = "OPENLOG_ERROR"; |
|
19
|
|
|
|
|
20
|
|
|
// Specify the path where to create the log files |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
$logPath = null, |
|
23
|
|
|
$suffix = null, |
|
24
|
|
|
$printout = false) |
|
25
|
|
|
{ |
|
26
|
|
|
global $argv; |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$this->printout = $printout; |
|
29
|
|
|
$this->logPath = "/var/tmp/logs/cpe/"; |
|
30
|
|
|
|
|
31
|
|
|
if ($logPath) |
|
32
|
|
|
$this->logPath = $logPath; |
|
33
|
|
|
|
|
34
|
|
|
if (!file_exists($this->logPath)) |
|
35
|
|
|
mkdir($this->logPath, 0755, true); |
|
36
|
|
|
|
|
37
|
|
|
$file = basename($argv[0]); |
|
38
|
|
|
if ($suffix) |
|
39
|
|
|
$file .= "-".$suffix; |
|
40
|
|
|
// Append progname to the path |
|
41
|
|
|
$this->logPath .= "/".$file.".log"; |
|
42
|
|
|
|
|
43
|
|
|
$this->log_out( |
|
44
|
|
|
"INFO", |
|
45
|
|
|
"[CPE SDK] ".basename(__FILE__), |
|
46
|
|
|
"Logging to: ".$this->logPath); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Log message to syslog and log file |
|
50
|
|
|
public function log_out( |
|
51
|
|
|
$type, |
|
52
|
|
|
$source, |
|
53
|
|
|
$message, |
|
54
|
|
|
$workflowId = null) |
|
55
|
|
|
{ |
|
56
|
|
|
$log = [ |
|
57
|
|
|
"time" => date("Y-m-d H:i:s", time()), |
|
58
|
|
|
"source" => $source, |
|
59
|
|
|
"type" => $type, |
|
60
|
|
|
"message" => $message |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
if ($workflowId) |
|
64
|
|
|
$log["workflowId"] = $workflowId; |
|
65
|
|
|
|
|
66
|
|
|
// Open Syslog. Use programe name as key |
|
67
|
|
|
if (!openlog (__FILE__, LOG_CONS|LOG_PID, LOG_LOCAL1)) |
|
68
|
|
|
throw new CpeException("Unable to connect to Syslog!", |
|
69
|
|
|
OPENLOG_ERROR); |
|
70
|
|
|
|
|
71
|
|
|
// Change Syslog priority level |
|
72
|
|
|
switch ($type) |
|
73
|
|
|
{ |
|
74
|
|
|
case "INFO": |
|
75
|
|
|
$priority = LOG_INFO; |
|
76
|
|
|
break; |
|
77
|
|
|
case "ERROR": |
|
78
|
|
|
$priority = LOG_ERR; |
|
79
|
|
|
break; |
|
80
|
|
|
case "FATAL": |
|
81
|
|
|
$priority = LOG_ALERT; |
|
82
|
|
|
break; |
|
83
|
|
|
case "WARNING": |
|
84
|
|
|
$priority = LOG_WARNING; |
|
85
|
|
|
break; |
|
86
|
|
|
case "DEBUG": |
|
87
|
|
|
$priority = LOG_DEBUG; |
|
88
|
|
|
break; |
|
89
|
|
|
default: |
|
90
|
|
|
throw new CpeException("Unknown log Type!", |
|
91
|
|
|
LOG_TYPE_ERROR); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Print log in file |
|
95
|
|
|
$this->print_to_file($log, $workflowId); |
|
96
|
|
|
|
|
97
|
|
|
// Encode log message in JSON for better parsing |
|
98
|
|
|
$out = json_encode($log); |
|
99
|
|
|
// Send to syslog |
|
100
|
|
|
syslog($priority, $out); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// Write log in file |
|
104
|
|
|
private function print_to_file($log, $workflowId) |
|
105
|
|
|
{ |
|
106
|
|
|
if (!is_string($log['message'])) |
|
107
|
|
|
$log['message'] = json_encode($log['message']); |
|
108
|
|
|
|
|
109
|
|
|
$toPrint = $log['time'] . " [" . $log['type'] . "] [" . $log['source'] . "] "; |
|
110
|
|
|
// If there is a workflow ID. We append it. |
|
111
|
|
|
if ($workflowId) |
|
112
|
|
|
$toPrint .= "[$workflowId] "; |
|
113
|
|
|
$toPrint .= $log['message'] . "\n"; |
|
114
|
|
|
|
|
115
|
|
|
if ($this->printout) |
|
116
|
|
|
print $toPrint; |
|
117
|
|
|
|
|
118
|
|
|
if (file_put_contents( |
|
119
|
|
|
$this->logPath, |
|
120
|
|
|
$toPrint, |
|
121
|
|
|
FILE_APPEND) === false) |
|
122
|
|
|
print "ERROR: Can't write into log file!\n"; |
|
123
|
|
|
} |
|
124
|
|
|
} |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state