2014-06-17 22:15:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace ManiaControl;
|
|
|
|
|
2014-06-20 16:51:08 +02:00
|
|
|
use ManiaControl\Files\FileUtil;
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
2014-08-05 00:55:32 +02:00
|
|
|
use ManiaControl\Utils\Formatter;
|
2014-06-20 16:51:08 +02:00
|
|
|
|
2014-06-17 22:15:35 +02:00
|
|
|
/**
|
|
|
|
* ManiaControl Logger Class
|
|
|
|
*
|
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2018-03-27 20:11:40 +02:00
|
|
|
* @copyright 2014-2018 ManiaControl Team
|
2014-06-17 22:15:35 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
abstract class Logger implements UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
2014-06-17 22:15:35 +02:00
|
|
|
|
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Setup the logging mechanism
|
2014-06-17 22:15:35 +02:00
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Set the error log file name
|
2014-06-20 14:57:02 +02:00
|
|
|
*/
|
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';
|
2014-08-05 01:54:50 +02:00
|
|
|
if (!defined('LOG_NAME_USE_DATE') || LOG_NAME_USE_DATE) {
|
2014-06-20 14:57:02 +02:00
|
|
|
$logFileName .= '_' . date('Y-m-d');
|
|
|
|
}
|
2014-08-05 01:54:50 +02:00
|
|
|
if (!defined('LOG_NAME_USE_PID') || LOG_NAME_USE_PID) {
|
2014-06-20 14:57:02 +02:00
|
|
|
$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-08-05 02:00:18 +02:00
|
|
|
* Get the logs folder and create it if necessary
|
2014-06-20 14:57:02 +02:00
|
|
|
*
|
2014-08-05 02:00:18 +02:00
|
|
|
* @return string
|
2014-06-20 14:57:02 +02:00
|
|
|
*/
|
2014-06-20 15:17:52 +02:00
|
|
|
public static function getLogsFolder() {
|
2014-08-08 14:41:58 +02:00
|
|
|
$logsFolder = MANIACONTROL_PATH . 'logs' . DIRECTORY_SEPARATOR;
|
2014-06-20 15:17:52 +02:00
|
|
|
if (!is_dir($logsFolder) && !mkdir($logsFolder)) {
|
2014-08-03 13:29:54 +02:00
|
|
|
self::logError("Couldn't create the logs folder!");
|
2014-08-05 02:00:18 +02:00
|
|
|
return null;
|
2014-06-17 22:15:35 +02:00
|
|
|
}
|
2014-06-20 15:17:52 +02:00
|
|
|
if (!is_writeable($logsFolder)) {
|
2014-08-03 13:29:54 +02:00
|
|
|
self::logError("ManiaControl doesn't have the necessary write rights for the logs folder!");
|
2014-08-05 02:00:18 +02:00
|
|
|
return null;
|
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
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Delete old ManiaControl log files
|
2014-06-20 16:51:08 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private static function cleanLogsFolder() {
|
|
|
|
$logsFolderPath = self::getLogsFolder();
|
|
|
|
return FileUtil::cleanDirectory($logsFolderPath);
|
|
|
|
}
|
|
|
|
|
2014-06-17 22:37:34 +02:00
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Log and output the given Error message
|
2014-06-17 22:37:34 +02:00
|
|
|
*
|
|
|
|
* @param string $message
|
2014-08-05 00:55:32 +02:00
|
|
|
* @param bool $stripCodes
|
2014-08-05 02:00:18 +02:00
|
|
|
* @param bool $eol
|
2014-06-17 22:37:34 +02:00
|
|
|
*/
|
2014-08-05 02:00:18 +02:00
|
|
|
public static function logError($message, $stripCodes = false, $eol = true) {
|
2014-06-17 22:37:34 +02:00
|
|
|
$message = '[ERROR] ' . $message;
|
2014-08-05 02:00:18 +02:00
|
|
|
self::log($message, $stripCodes, $eol);
|
2014-06-17 22:37:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Log and output the given message
|
2014-06-17 22:37:34 +02:00
|
|
|
*
|
|
|
|
* @param string $message
|
2014-08-05 00:55:32 +02:00
|
|
|
* @param bool $stripCodes
|
2014-08-05 02:00:18 +02:00
|
|
|
* @param bool $eol
|
2014-06-17 22:37:34 +02:00
|
|
|
*/
|
2014-08-05 02:00:18 +02:00
|
|
|
public static function log($message, $stripCodes = false, $eol = true) {
|
2014-08-05 00:55:32 +02:00
|
|
|
if ($stripCodes) {
|
|
|
|
$message = Formatter::stripCodes($message);
|
|
|
|
}
|
2014-06-17 22:37:34 +02:00
|
|
|
error_log($message);
|
2014-08-05 01:51:59 +02:00
|
|
|
self::output($message, $eol);
|
2014-06-17 22:37:34 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 15:17:52 +02:00
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Echo the given message
|
2014-06-20 15:17:52 +02:00
|
|
|
*
|
|
|
|
* @param string $message
|
|
|
|
* @param bool $eol
|
|
|
|
*/
|
2014-08-05 01:51:59 +02:00
|
|
|
private static function output($message, $eol = true) {
|
2014-06-20 15:17:52 +02:00
|
|
|
if ($eol) {
|
|
|
|
$message = '[' . date('d-M-Y H:i:s e') . '] ' . $message . PHP_EOL;
|
|
|
|
}
|
|
|
|
echo $message;
|
|
|
|
}
|
|
|
|
|
2014-06-20 10:56:15 +02:00
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Log and output the given Info message
|
2014-06-20 10:56:15 +02:00
|
|
|
*
|
|
|
|
* @param string $message
|
2014-08-05 00:55:32 +02:00
|
|
|
* @param bool $stripCodes
|
2014-08-05 02:00:18 +02:00
|
|
|
* @param bool $eol
|
2014-06-20 10:56:15 +02:00
|
|
|
*/
|
2014-08-05 02:00:18 +02:00
|
|
|
public static function logInfo($message, $stripCodes = false, $eol = true) {
|
2014-06-20 10:56:15 +02:00
|
|
|
$message = '[INFO] ' . $message;
|
2014-08-05 02:00:18 +02:00
|
|
|
self::log($message, $stripCodes, $eol);
|
2014-06-20 10:56:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-05 02:00:18 +02:00
|
|
|
* Log and output the given Warning message
|
2014-06-20 10:56:15 +02:00
|
|
|
*
|
|
|
|
* @param string $message
|
2014-08-05 00:55:32 +02:00
|
|
|
* @param bool $stripCodes
|
2014-08-05 02:00:18 +02:00
|
|
|
* @param bool $eol
|
2014-06-20 10:56:15 +02:00
|
|
|
*/
|
2014-08-05 02:00:18 +02:00
|
|
|
public static function logWarning($message, $stripCodes = false, $eol = true) {
|
2014-06-20 10:56:15 +02:00
|
|
|
$message = '[WARNING] ' . $message;
|
2014-08-05 02:00:18 +02:00
|
|
|
self::log($message, $stripCodes, $eol);
|
2014-06-20 10:56:15 +02:00
|
|
|
}
|
2014-06-17 22:15:35 +02:00
|
|
|
}
|