1+ <?php
2+
3+ trait logging {
4+
5+ protected $ options = array (
6+
7+ // path of the log txt files
8+ 'path ' => '../application/logs ' ,
9+
10+ // reference: http://php.net/manual/en/function.chmod.php
11+ // Read and write for owner, read for everybody else
12+ 'filePermission ' => 0644 ,
13+
14+ );
15+
16+ //The syslog protocol - Severities - RFC 5424, section 6.2.1 table 2.
17+ // https://tools.ietf.org/html/rfc5424
18+
19+ protected $ level = array (
20+ 0 => 'EMERGENCY ' , // system is unusable
21+ 1 => 'ALERT ' , // action must be taken immediately
22+ 2 => 'CRITICAL ' , // critical conditions
23+ 3 => 'ERROR ' , // error conditions
24+ 4 => 'WARNING ' , //warning conditions
25+ 5 => 'NOTICE ' , // normal but significant condition
26+ 6 => 'INFORMATIONAL ' , // informational messages
27+ 7 => 'DEBUG ' , // debug-level messages
28+ );
29+
30+
31+ // alias functions
32+ public function emergency ($ log_msg ){
33+ $ this ->write_log ($ log_msg , 0 );
34+ }
35+
36+ public function alert ($ log_msg ){
37+ $ this ->write_log ($ log_msg , 1 );
38+ }
39+
40+ public function critical ($ log_msg ){
41+ $ this ->write_log ($ log_msg , 2 );
42+ }
43+
44+ public function error ($ log_msg ){
45+ $ this ->write_log ($ log_msg , 3 );
46+ }
47+
48+ public function warning ($ log_msg ){
49+ $ this ->write_log ($ log_msg , 4 );
50+ }
51+
52+ public function notice ($ log_msg ){
53+ $ this ->write_log ($ log_msg , 5 );
54+ }
55+
56+ public function informational ($ log_msg ){
57+ $ this ->write_log ($ log_msg , 6 );
58+ }
59+
60+ public function debug ($ log_msg ){
61+ $ this ->write_log ($ log_msg , 7 );
62+ }
63+
64+
65+ // write the log entry
66+ public function write_log ($ log_msg , $ sev_level ) {
67+
68+ // get severity level name
69+ if (isset ($ this ->level [$ sev_level ])) {
70+ $ sev_level = $ sev_level .' ' .$ this ->level [$ sev_level ];
71+ }
72+
73+ // if file handler doesn't exist, then open logfile
74+ if (!isset ($ this ->fh ) || !is_resource ($ this ->fh )) {
75+ $ this ->open_log ();
76+ }
77+
78+ // set the timezone to asia/colombo
79+ date_default_timezone_set ('Asia/Colombo ' );
80+ $ time = date ('Y-m-d H:i:s ' );
81+
82+ // Get the caller function
83+ $ caller = $ _SERVER ['SERVER_NAME ' ].$ _SERVER ['REQUEST_URI ' ];
84+
85+ // Create new log entry
86+ $ entry = $ time ."\t\t" .$ caller ."\t\t\t\t" .$ log_msg ."\t\t" .$ sev_level .PHP_EOL ;
87+
88+ // write entry to the txt
89+ flock ($ this ->fh , LOCK_EX );
90+ fwrite ($ this ->fh , $ entry ) or
91+ die ('Cannot write to the log file ' .$ this ->file );
92+ flock ($ this ->fh , LOCK_UN );
93+
94+
95+ }
96+
97+
98+ // open logfile
99+ private function open_log () {
100+
101+ // get logfile path
102+ $ this ->path = rtrim ($ this ->options ['path ' ], '\\/ ' );
103+
104+ // set logfile name
105+ $ this ->file = $ this ->path .'/ ' .date ('Y-m-d ' ).'.txt ' ;
106+
107+ // create a log file, if it does not exist
108+ if (!file_exists ($ this ->file )) {
109+
110+ $ this ->fh = fopen ($ this ->file , 'w ' )
111+ or die ('Cannot open log file to write. ' .$ this ->file );
112+ fclose ($ this ->fh );
113+
114+ //change permissions if it's not writtable
115+ if (!is_writable ($ this ->file )) {
116+ chmod ($ this ->file , $ this ->options ['filePermission ' ]);
117+ }
118+
119+ }
120+
121+ // if file exist, open then appenddd
122+ $ this ->fh = fopen ($ this ->file , 'a ' )
123+ or die ('Cannot open log file to append. ' .$ this ->file );
124+ }
125+
126+ // close logfile
127+ public function __destruct (){
128+ if ($ this ->fh ) {
129+ fclose ($ this ->fh );
130+ }
131+ }
132+
133+
134+ }
0 commit comments