Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\Log;
|
||||
|
||||
use Xentral\Core\DependencyInjection\ContainerInterface;
|
||||
use Xentral\Core\DependencyInjection\ServiceContainer;
|
||||
use Xentral\Modules\Log\Service\DatabaseLogGateway;
|
||||
use Xentral\Modules\Log\Service\DatabaseLogService;
|
||||
use Xentral\Modules\Log\Service\LoggerConfigService;
|
||||
use Xentral\Modules\Log\Wrapper\CompanyConfigWrapper;
|
||||
|
||||
final class Bootstrap
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public static function registerServices(): array
|
||||
{
|
||||
return [
|
||||
'DatabaseLogService' => 'onInitDatabaseLogService',
|
||||
'DatabaseLogGateway' => 'onInitDatabaseLogGateway',
|
||||
'LoggerConfigService' => 'onInitLoggerConfigService',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServiceContainer $container
|
||||
*
|
||||
* @return DatabaseLogService
|
||||
*/
|
||||
public static function onInitDatabaseLogService(ServiceContainer $container): DatabaseLogService
|
||||
{
|
||||
return new DatabaseLogService($container->get('Database'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServiceContainer $container
|
||||
*
|
||||
* @return DatabaseLogGateway
|
||||
*/
|
||||
public static function onInitDatabaseLogGateway(ServiceContainer $container): DatabaseLogGateway
|
||||
{
|
||||
return new DatabaseLogGateway($container->get('Database'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServiceContainer $container
|
||||
*
|
||||
* @return LoggerConfigService
|
||||
*/
|
||||
public static function onInitLoggerConfigService(ServiceContainer $container): LoggerConfigService
|
||||
{
|
||||
return new LoggerConfigService(self::onInitCompanyConfigWrapper($container));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @return CompanyConfigWrapper
|
||||
*/
|
||||
private static function onInitCompanyConfigWrapper(ContainerInterface $container): CompanyConfigWrapper
|
||||
{
|
||||
/** @var \ApplicationCore $app */
|
||||
$app = $container->get('LegacyApplication');
|
||||
|
||||
return new CompanyConfigWrapper($app->erp);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\Log\Data;
|
||||
|
||||
use DateTime;
|
||||
use Exception;
|
||||
|
||||
final class LogData
|
||||
{
|
||||
/** @var int|null $id */
|
||||
private $id;
|
||||
|
||||
/** @var DateTime|null $logTime */
|
||||
private $logTime;
|
||||
|
||||
/** @var string|null $level */
|
||||
private $level;
|
||||
|
||||
/** @var string|null $message */
|
||||
private $message;
|
||||
|
||||
/** @var string|null $class */
|
||||
private $class;
|
||||
|
||||
/** @var string|null $method */
|
||||
private $method;
|
||||
|
||||
/** @var int|null $line */
|
||||
private $line;
|
||||
|
||||
/** @var string|null $originType */
|
||||
private $originType;
|
||||
|
||||
/** @var string|null $originDetail */
|
||||
private $originDetail;
|
||||
|
||||
/** @var string|null $dump */
|
||||
private $dump;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return LogData
|
||||
*/
|
||||
public static function fromDbState(array $data): LogData
|
||||
{
|
||||
$logData = new LogData();
|
||||
|
||||
if (isset($data['id'])) {
|
||||
$logData->id = (int)$data['id'];
|
||||
}
|
||||
|
||||
if (isset($data['log_time'])) {
|
||||
try {
|
||||
$logData->logTime = new DateTime($data['log_time']);
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($data['level'])) {
|
||||
$logData->level = (string)$data['level'];
|
||||
}
|
||||
|
||||
if (isset($data['message'])) {
|
||||
$logData->message = (string)$data['message'];
|
||||
}
|
||||
|
||||
if (isset($data['class'])) {
|
||||
$logData->class = (string)$data['class'];
|
||||
}
|
||||
|
||||
if (isset($data['method'])) {
|
||||
$logData->method = (string)$data['method'];
|
||||
}
|
||||
|
||||
if (isset($data['line'])) {
|
||||
$logData->line = (int)$data['line'];
|
||||
}
|
||||
|
||||
if (isset($data['origin_type'])) {
|
||||
$logData->originType = (string)$data['origin_type'];
|
||||
}
|
||||
|
||||
if (isset($data['origin_detail'])) {
|
||||
$logData->originDetail = (string)$data['origin_detail'];
|
||||
}
|
||||
|
||||
if (isset($data['dump'])) {
|
||||
$logData->dump = (string)$data['dump'];
|
||||
}
|
||||
|
||||
return $logData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getId(): ?int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DateTime|null
|
||||
*/
|
||||
public function getLogTime(): ?DateTime
|
||||
{
|
||||
return $this->logTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getLevel(): ?string
|
||||
{
|
||||
return $this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMessage(): ?string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getClass(): ?string
|
||||
{
|
||||
return $this->class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getMethod(): ?string
|
||||
{
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
*/
|
||||
public function getLine(): ?int
|
||||
{
|
||||
return $this->line;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOriginType(): ?string
|
||||
{
|
||||
return $this->originType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOriginDetail(): ?string
|
||||
{
|
||||
return $this->originDetail;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDump(): ?string
|
||||
{
|
||||
return $this->dump;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Log\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidArgumentException extends RuntimeException implements LogExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Log\Exception;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class InvalidLoglevelException extends RuntimeException implements LogExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Log\Exception;
|
||||
|
||||
use Xentral\Core\Exception\ModuleExceptionInterface;
|
||||
|
||||
interface LogExceptionInterface extends ModuleExceptionInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\Log\Service;
|
||||
|
||||
use Xentral\Components\Database\Database;
|
||||
use Xentral\Modules\Log\Data\LogData;
|
||||
|
||||
final class DatabaseLogGateway
|
||||
{
|
||||
/** @var Database $db */
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* @param Database $database
|
||||
*/
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->db = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $logId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function tryGetLogDump(int $logId): ?string
|
||||
{
|
||||
$sql = 'SELECT l.dump FROM `log` AS `l` WHERE l.id = :log_id';
|
||||
$value = $this->db->fetchValue($sql, ['log_id' => $logId]);
|
||||
if (!is_string($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $cronFileName
|
||||
*
|
||||
* @return array|LogData[]
|
||||
*/
|
||||
public function findLogsByCronjob(string $cronFileName): array
|
||||
{
|
||||
$originDetail = 'job=' . $cronFileName;
|
||||
|
||||
$sql =
|
||||
'SELECT
|
||||
l.id,
|
||||
l.log_time,
|
||||
l.level,
|
||||
l.message,
|
||||
l.class,
|
||||
l.method,
|
||||
l.line,
|
||||
l.origin_type,
|
||||
l.origin_detail,
|
||||
l.dump
|
||||
FROM `log` AS `l`
|
||||
WHERE l.origin_detail = :origin_detail
|
||||
ORDER BY l.id';
|
||||
|
||||
$logs = $this->db->fetchAll($sql, ['origin_detail' => $originDetail]);
|
||||
|
||||
$result = [];
|
||||
if (!empty($logs)) {
|
||||
foreach ($logs as $log) {
|
||||
$result[] = LogData::fromDbState($log);
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\Log\Service;
|
||||
|
||||
use Xentral\Components\Database\Database;
|
||||
use Xentral\Modules\Log\Exception\InvalidArgumentException;
|
||||
|
||||
final class DatabaseLogService
|
||||
{
|
||||
/** @var Database $db */
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* @param Database $database
|
||||
*/
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->db = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all log entries.
|
||||
*
|
||||
* @return int Amount of deleted log entries
|
||||
*/
|
||||
public function removeAllLogs(): int
|
||||
{
|
||||
$sql = 'DELETE FROM `log` WHERE 1';
|
||||
|
||||
return $this->db->fetchAffected($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes log entries that are older than specified amount of days.
|
||||
*
|
||||
* @param int $days
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*
|
||||
* @return int Amount of deleted log entries
|
||||
*/
|
||||
public function removeLogsOlderThan(int $days): int
|
||||
{
|
||||
if ($days < 1) {
|
||||
throw new InvalidArgumentException('Negative days not acceptable.');
|
||||
}
|
||||
$sql = 'DELETE FROM `log` WHERE DATE_SUB(CURDATE(), INTERVAL :days DAY) >= log_time';
|
||||
|
||||
return $this->db->fetchAffected($sql, ['days' => $days]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\Log\Service;
|
||||
|
||||
use Xentral\Components\Logger\LogLevel;
|
||||
use Xentral\Modules\Log\Exception\InvalidArgumentException;
|
||||
use Xentral\Modules\Log\Exception\InvalidLoglevelException;
|
||||
use Xentral\Modules\Log\Wrapper\CompanyConfigWrapper;
|
||||
|
||||
final class LoggerConfigService
|
||||
{
|
||||
/** @var string CONFIG_KEY_LEVEL */
|
||||
private const CONFIG_KEY_LEVEL = 'logfile_logging_level';
|
||||
|
||||
/** @var CompanyConfigWrapper $db */
|
||||
private $companyConfig;
|
||||
|
||||
/**
|
||||
* @param CompanyConfigWrapper $companyConfig
|
||||
*/
|
||||
public function __construct(CompanyConfigWrapper $companyConfig)
|
||||
{
|
||||
$this->companyConfig = $companyConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidLoglevelException
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getLogLevel(): string
|
||||
{
|
||||
$level = (string)$this->companyConfig->get(self::CONFIG_KEY_LEVEL);
|
||||
$level = strtolower($level);
|
||||
if (!$this->isAllowedLogLevel($level)) {
|
||||
throw new InvalidLoglevelException(sprintf('Unrecognized Loglevel "%s".', $level));
|
||||
}
|
||||
|
||||
return $level;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $level
|
||||
*
|
||||
* @throws InvalidLoglevelException
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setLogLevel(string $level): void
|
||||
{
|
||||
if (!$this->isAllowedLogLevel($level)) {
|
||||
throw new InvalidArgumentException(sprintf('Unrecognised Loglevel "%s"', $level));
|
||||
}
|
||||
$this->companyConfig->set(self::CONFIG_KEY_LEVEL, $level);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $level
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isAllowedLogLevel(string $level): bool
|
||||
{
|
||||
return (
|
||||
$level === LogLevel::DEBUG
|
||||
|| $level === LogLevel::INFO
|
||||
|| $level === LogLevel::NOTICE
|
||||
|| $level === LogLevel::WARNING
|
||||
|| $level === LogLevel::ERROR
|
||||
|| $level === LogLevel::CRITICAL
|
||||
|| $level === LogLevel::ALERT
|
||||
|| $level === LogLevel::EMERGENCY
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\Log\Wrapper;
|
||||
|
||||
use erpAPI;
|
||||
|
||||
/**
|
||||
* Anti-Corruption-Layer für erp::GetKonfiguration und erp::SetKonfigurationValue
|
||||
*/
|
||||
final class CompanyConfigWrapper
|
||||
{
|
||||
/** @var erpAPI $erp */
|
||||
private $erp;
|
||||
|
||||
/**
|
||||
* @param erpAPI $erp
|
||||
*/
|
||||
public function __construct(erpAPI $erp)
|
||||
{
|
||||
$this->erp = $erp;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name)
|
||||
{
|
||||
return $this->erp->GetKonfiguration($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $name, $value)
|
||||
{
|
||||
$this->erp->SetKonfigurationValue($name, $value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Confirm and delete all logs
|
||||
*/
|
||||
var DeleteLogs = (function ($) {
|
||||
'use strict';
|
||||
|
||||
var me = {
|
||||
isInitialized: false,
|
||||
|
||||
url: {
|
||||
ajaxDeleteLogs: 'index.php?module=log&action=deleteall'
|
||||
},
|
||||
|
||||
selector: {
|
||||
deleteButton: '#btn_delete'
|
||||
},
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
init: function () {
|
||||
if (me.isInitialized === true) {
|
||||
return;
|
||||
}
|
||||
if ($(me.selector.deleteButton).length === 0) {
|
||||
return;
|
||||
}
|
||||
me.registerEvents();
|
||||
me.isInitialized = true;
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {void}
|
||||
*/
|
||||
registerEvents: function () {
|
||||
$(me.selector.deleteButton).on('click', function (event) {
|
||||
event.preventDefault();
|
||||
me.dialogDelete();
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {void}
|
||||
*/
|
||||
dialogDelete: function () {
|
||||
var confirmValue = confirm('Alle Log-Einträge löschen?');
|
||||
if (confirmValue === false) {
|
||||
return;
|
||||
}
|
||||
me.ajaxDeleteLogs();
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {void}
|
||||
*/
|
||||
ajaxDeleteLogs: function() {
|
||||
$.ajax({
|
||||
url: me.url.ajaxDeleteLogs,
|
||||
data:{
|
||||
delete: true
|
||||
},
|
||||
dataType: 'json',
|
||||
method: 'post',
|
||||
beforeSend: function () {
|
||||
App.loading.open();
|
||||
},
|
||||
success: function (data) {
|
||||
App.loading.close();
|
||||
alert('Log-Einträge wurden erfolgreich gelöscht.');
|
||||
},
|
||||
error: function (xhr, status, httpStatus) {
|
||||
console.log('Fehler: ' + httpStatus);
|
||||
alert('Log-Einträge konnten nicht gelöscht werden.');
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
init: me.init
|
||||
};
|
||||
|
||||
})(jQuery);
|
||||
|
||||
$(document).ready(function () {
|
||||
DeleteLogs.init();
|
||||
});
|
||||
Reference in New Issue
Block a user