2014-06-17 22:15:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
|
|
|
|
2014-06-20 16:51:08 +02:00
|
|
|
use ManiaControl\Files\FileUtil;
|
|
|
|
|
2014-06-17 22:15:35 +02:00
|
|
|
/**
|
|
|
|
* ManiaControl Logger Class
|
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
|
|
|
* @copyright 2014 ManiaControl Team
|
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2014-07-25 15:47:44 +02:00
|
|
|
abstract class Logger {
|
2014-06-17 22:15:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the Logging Mechanism
|
|
|
|
*/
|
|
|
|
public static function setup() {
|
2014-06-20 14:57:02 +02:00
|
|
|
self::setupErrorLogFileName();
|
2014-06-20 16:51:08 +02:00
|
|
|
self::cleanLogsFolder();
|
2014-06-20 14:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the Error Log File Name
|
|
|
|
*/
|
2014-06-20 15:00:09 +02:00
|
|
|
private static function setupErrorLogFileName() {
|
2014-06-20 16:20:03 +02:00
|
|
|
$logsFolder = self::getLogsFolder();
|
2014-06-20 14:57:02 +02:00
|
|
|
if ($logsFolder) {
|
|
|
|
$logFileName = $logsFolder . 'ManiaControl';
|
|
|
|
if (LOG_NAME_USE_DATE) {
|
|
|
|
$logFileName .= '_' . date('Y-m-d');
|
|
|
|
}
|
|
|
|
if (LOG_NAME_USE_PID) {
|
|
|
|
$logFileName .= '_' . getmypid();
|
|
|
|
}
|
|
|
|
$logFileName .= '.log';
|
|
|
|
ini_set('error_log', $logFileName);
|
2014-06-17 22:15:35 +02:00
|
|
|
}
|
2014-06-20 14:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-20 15:17:52 +02:00
|
|
|
* Get the Logs Folder and create it if necessary
|
2014-06-20 14:57:02 +02:00
|
|
|
*
|
2014-06-20 15:17:52 +02:00
|
|
|
* @return string|bool
|
2014-06-20 14:57:02 +02:00
|
|
|
*/
|
2014-06-20 15:17:52 +02:00
|
|
|
public static function getLogsFolder() {
|
|
|
|
$logsFolder = ManiaControlDir . 'logs' . DIRECTORY_SEPARATOR;
|
|
|
|
if (!is_dir($logsFolder) && !mkdir($logsFolder)) {
|
|
|
|
trigger_error("Couldn't create the logs folder!");
|
2014-06-20 14:57:02 +02:00
|
|
|
return false;
|
2014-06-17 22:15:35 +02:00
|
|
|
}
|
2014-06-20 15:17:52 +02:00
|
|
|
if (!is_writeable($logsFolder)) {
|
|
|
|
trigger_error("ManiaControl doesn't have the necessary write rights for the logs folder!");
|
|
|
|
return false;
|
2014-06-17 22:15:35 +02:00
|
|
|
}
|
2014-06-20 15:17:52 +02:00
|
|
|
return $logsFolder;
|
2014-06-17 22:15:35 +02:00
|
|
|
}
|
2014-06-17 22:37:34 +02:00
|
|
|
|
2014-06-20 16:51:08 +02:00
|
|
|
/**
|
|
|
|
* Delete old ManiaControl Log Files
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function cleanLogsFolder() {
|
|
|
|
$logsFolderPath = self::getLogsFolder();
|
|
|
|
return FileUtil::cleanDirectory($logsFolderPath);
|
|
|
|
}
|
|
|
|
|
2014-06-17 22:37:34 +02:00
|
|
|
/**
|
|
|
|
* Log and echo the given Error Message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param bool $eol
|
|
|
|
* @param bool $output
|
|
|
|
*/
|
|
|
|
public static function logError($message, $eol = true, $output = true) {
|
|
|
|
$message = '[ERROR] ' . $message;
|
|
|
|
self::log($message, $eol, $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log and output the given Message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param bool $eol
|
|
|
|
* @param bool $output
|
|
|
|
*/
|
|
|
|
public static function log($message, $eol = true, $output = true) {
|
|
|
|
error_log($message);
|
|
|
|
if ($output) {
|
|
|
|
self::output($message, $eol);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-20 15:17:52 +02:00
|
|
|
/**
|
|
|
|
* Echo the given Message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param bool $eol
|
|
|
|
*/
|
|
|
|
public static function output($message, $eol = true) {
|
|
|
|
if ($eol) {
|
|
|
|
$message = '[' . date('d-M-Y H:i:s e') . '] ' . $message . PHP_EOL;
|
|
|
|
}
|
|
|
|
echo $message;
|
|
|
|
}
|
|
|
|
|
2014-06-20 10:56:15 +02:00
|
|
|
/**
|
|
|
|
* Log and echo the given Info Message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param bool $eol
|
|
|
|
* @param bool $output
|
|
|
|
*/
|
|
|
|
public static function logInfo($message, $eol = true, $output = true) {
|
|
|
|
$message = '[INFO] ' . $message;
|
|
|
|
self::log($message, $eol, $output);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log and echo the given Warning Message
|
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param bool $eol
|
|
|
|
* @param bool $output
|
|
|
|
*/
|
|
|
|
public static function logWarning($message, $eol = true, $output = true) {
|
|
|
|
$message = '[WARNING] ' . $message;
|
|
|
|
self::log($message, $eol, $output);
|
|
|
|
}
|
2014-06-17 22:15:35 +02:00
|
|
|
}
|