7 const LOG_ERROR
= 'error';
8 const LOG_DEPRECATED
= 'deprecated';
9 const LOG_DEBUG
= 'debug';
12 static protected $instances;
14 /** @var string what kind of log is this */
17 protected $isLogging = true;
22 * @param string $facility The type of log
24 protected function __construct($facility)
27 $this->facility
= $facility;
29 // Should logging be disabled for this facility?
30 $dontlog = explode(',', $conf['dontlog']);
31 $dontlog = array_map('trim', $dontlog);
32 if (in_array($facility, $dontlog)) $this->isLogging
= false;
36 * Return a Logger instance for the given facility
38 * @param string $facility The type of log
41 static public function getInstance($facility = self
::LOG_ERROR
)
43 if (empty(self
::$instances[$facility])) {
44 self
::$instances[$facility] = new Logger($facility);
46 return self
::$instances[$facility];
50 * Convenience method to directly log to the error log
52 * @param string $message The log message
53 * @param mixed $details Any details that should be added to the log entry
54 * @param string $file A source filename if this is related to a source position
55 * @param int $line A line number for the above file
56 * @return bool has a log been written?
58 static public function error($message, $details = null, $file = '', $line = 0)
60 return self
::getInstance(self
::LOG_ERROR
)->log(
61 $message, $details, $file, $line
66 * Convenience method to directly log to the debug log
68 * @param string $message The log message
69 * @param mixed $details Any details that should be added to the log entry
70 * @param string $file A source filename if this is related to a source position
71 * @param int $line A line number for the above file
72 * @return bool has a log been written?
74 static public function debug($message, $details = null, $file = '', $line = 0)
76 return self
::getInstance(self
::LOG_DEBUG
)->log(
77 $message, $details, $file, $line
82 * Convenience method to directly log to the deprecation log
84 * @param string $message The log message
85 * @param mixed $details Any details that should be added to the log entry
86 * @param string $file A source filename if this is related to a source position
87 * @param int $line A line number for the above file
88 * @return bool has a log been written?
90 static public function deprecated($message, $details = null, $file = '', $line = 0)
92 return self
::getInstance(self
::LOG_DEPRECATED
)->log(
93 $message, $details, $file, $line
98 * Log a message to the facility log
100 * @param string $message The log message
101 * @param mixed $details Any details that should be added to the log entry
102 * @param string $file A source filename if this is related to a source position
103 * @param int $line A line number for the above file
104 * @return bool has a log been written?
106 public function log($message, $details = null, $file = '', $line = 0)
108 if(!$this->isLogging
) return false;
110 // details are logged indented
112 if (!is_string($details)) {
113 $details = json_encode($details, JSON_PRETTY_PRINT
);
115 $details = explode("\n", $details);
116 $loglines = array_map(function ($line) {
119 } elseif ($details) {
120 $loglines = [$details];
125 // datetime, fileline, message
126 $logline = gmdate('Y-m-d H:i:s') . "\t";
129 if ($line) $logline .= "($line)";
131 $logline .= "\t" . $message;
133 array_unshift($loglines, $logline);
134 return $this->writeLogLines($loglines);
138 * Construct the log file for the given day
140 * @param false|string|int $date Date to access, false for today
143 public function getLogfile($date = false)
147 if ($date !== null) $date = strtotime($date);
148 if (!$date) $date = time();
150 return $conf['logdir'] . '/' . $this->facility
. '/' . date('Y-m-d', $date) . '.log';
154 * Write the given lines to today's facility log
156 * @param string[] $lines the raw lines to append to the log
157 * @return bool true if the log was written
159 protected function writeLogLines($lines)
161 $logfile = $this->getLogfile();
162 return io_saveFile($logfile, join("\n", $lines) . "\n", true);