Logger.php
4.04 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
<?php
/**
* Logger Class
*
* This is an application logging class.
*
* @author
* https://findnerd.com/list/view/Logger-class-in-PHP/1852/
*
* adjustment by irul @ 20240831
*/
define("DATESTRING_FULL", "Y-m-d H:i:s");
class Logger
{
private static $logger = null; // Instance of the logger class when default log file is in use.
private static $user_logger = null; // Instance of the logger class when a log file is specified by the developer.
private $user_handle = false; // Flag to determine which log file to use.
private $log_handle = null; // Handle to the default log file.
private $_user_log_handle = null; // Handle to the developer specified log file.
/**
* This method checks if there is an instance available. If yes it returns its handle otherwise
* create a new instance and return it's handle.
*
* @param string $file
*
* @return Object Current instance of the class.
*
* @access public
*
* @static
* */
public static function get_logger($log_path = null, $log_file = null)
{
if (is_null($log_path)) {
if (!(isset(self::$logger)) || (is_null(self::$logger))) {
self::$logger = new self();
}
return self::$logger;
} else {
if (!(isset(self::$user_logger)) || (is_null(self::$user_logger))) {
self::$user_logger = new self($log_path, $log_file);
}
self::$user_logger->user_handle = true;
return self::$user_logger;
}
}
/*
* This constructor checks if a log file exits with current date, if yes opens it,
* if not create it and then opens its to write logs.
*
* @access private
* */
private function __construct($log_path = null, $log_file = null)
{
if (is_null($log_file)) {
$log_file = date('Y-m-d');
}
if (is_null($log_path)) {
$default_log_path = __DIR__ . '/logs';
if (!is_dir($default_log_path)) {
mkdir($default_log_path);
}
$log_file_name = $default_log_path . '/' . $log_file . '.log';
$this->log_handle = fopen($log_file_name, 'a');
} else {
if (!is_dir($log_path)) {
mkdir($log_path, 0777, true);
}
$log_file_name = $log_path . '/' . $log_file . '.log';
$this->user_log_handle = fopen($log_file_name, 'a');
}
}
/*
* This method writes the log messages to the log file. This method internally calls the
* do_write method to do the actual write operation.
*
* @param string $string The log message to be written in the log file.
*
* $access public
* */
public function log($string)
{
$this->do_write($string);
}
/*
* This method writes the array or dump with a messages to the log file. This method internally
* calls the do_write method to do the actual write operation.
*
* @param mixed $var_name The variable or dump to be written in the log file.
* @param string $string The log message to be written in the log file, defaults to VARDUMP.
*
* $access public
* */
public function dump($var_name, $string = '')
{
$this->do_write((empty($string) ? 'VARDUMP ' : 'Dump of ') . $string . ' => ' . var_export($var_name, true));
}
/*
* This method is always called by the log() or dump() method. It writes the passed string
* to the appropriate log file based on the object it is called upon.
*
* @param string $log_string The log message to be written in the log file.
*
* $access public
* */
public function do_write($log_string)
{
$log_string = sprintf("%s %s", date(DATESTRING_FULL), $log_string);
$log_string = $log_string . PHP_EOL;
if ($this->user_handle) {
fwrite($this->user_log_handle, $log_string);
} else {
fwrite($this->log_handle, $log_string);
}
}
}