فهرست منبع

added slack-logs test connection to api and settings
added slackLogLevel config item
added setChannel and setUsername functions to Logger

CauseFX 3 سال پیش
والد
کامیت
a27fedada7

+ 3 - 1
api/classes/organizr.class.php

@@ -2351,7 +2351,9 @@ class Organizr
 				$this->settingsOption('switch', 'sendLogsToSlack', ['label' => 'Send Logs to Slack', 'help' => 'Send Logs to Slack as well']),
 				$this->settingsOption('select', 'slackLogLevel', ['label' => 'Slack Log Level', 'options' => $this->logLevels()]),
 				$this->settingsOption('url', 'slackLogWebhook', ['label' => 'Slack Webhook URL', 'help' => 'If using Discord make sure to end the URL with /slack']),
-				$this->settingsOption('input', 'slackLogWebHookChannel', ['label' => 'Slack Channel for Webhook', 'help' => 'Channel ID for webhook']),
+				$this->settingsOption('input', 'slackLogWebHookChannel', ['label' => 'Slack Channel for Webhook', 'help' => 'Channel ID for webhook - Not needed for Discord']),
+				$this->settingsOption('blank'),
+				$this->settingsOption('test', 'slack-logs', ['label' => 'Test Slack', 'text' => 'Test Slack', 'help' => 'Test only sends a warning message so make sure Slack Log Level is Warning when testing']),
 			],
 			'Cron' => [
 				$this->settingsOption('cron-file'),

+ 1 - 1
api/config/default.php

@@ -661,7 +661,7 @@ return [
 	'autoUpdateCronSchedule' => '@weekly',
 	'useRandomMediaImage' => false,
 	'sendLogsToSlack' => false,
-	'slackLogLevel' => 'ERROR',
+	'slackLogLevel' => 'WARNING',
 	'slackLogWebhook' => '',
 	'slackLogWebHookChannel' => ''
 ];

+ 30 - 9
api/functions/log-functions.php

@@ -210,30 +210,29 @@ trait LogFunctions
 	public function setLoggerChannel($channel = 'Organizr', $username = null)
 	{
 		if ($this->hasDB()) {
-			$setLogger = false;
+			$channel = $channel ?: 'Organizr';
+			//$setLogger = false;
 			if ($username) {
 				$username = $this->sanitizeUserString($username);
 			}
 			if ($this->logger) {
 				if ($channel) {
 					if (strtolower($this->logger->getChannel()) !== strtolower($channel)) {
-						$setLogger = true;
+						$this->logger->setChannel($channel);
+						//$setLogger = true;
 					}
 				}
 				if ($username) {
 					$currentUsername = $this->logger->getTraceId() !== '' ? strtolower($this->logger->getTraceId()) : '';
 					if ($currentUsername !== strtolower($username)) {
-						$setLogger = true;
+						$this->logger->setUsername($username);
+						//$setLogger = true;
 					}
 				}
+				return $this->logger;
 			} else {
-				$setLogger = true;
-			}
-			if ($setLogger) {
-				$channel = $channel ?: 'Organizr';
+				//$setLogger = true;
 				return $this->setupLogger($channel, $username);
-			} else {
-				return $this->logger;
 			}
 		}
 	}
@@ -445,4 +444,26 @@ trait LogFunctions
 		$dropdownItems .= '<li class="divider"></li><li><a href="javascript:toggleLogFilter(\'NONE\')"><span lang="en">None</span></a></li>';
 		return '<button aria-expanded="false" data-toggle="dropdown" class="btn btn-inverse dropdown-toggle waves-effect waves-light pull-right m-r-5 hidden-xs" type="button"> <span class="log-filter-text m-r-5" lang="en">NONE</span><i class="fa fa-filter m-r-5"></i></button><ul role="menu" class="dropdown-menu log-filter-dropdown pull-right">' . $dropdownItems . '</ul>';
 	}
+
+	public function testConnectionSlackLogs()
+	{
+		if (!$this->config['sendLogsToSlack']) {
+			$this->setResponse(409, 'sendLogsToSlack is disabled');
+			return false;
+		}
+		if ($this->config['slackLogWebhook'] == '') {
+			$this->setResponse(409, 'slackLogWebhook is empty');
+			return false;
+		}
+		if ($this->config['slackLogWebHookChannel'] == '' && stripos($this->config['slackLogWebhook'], 'discord') === false) {
+			$this->setResponse(409, 'slackLogWebhook is empty');
+			return false;
+		}
+		$context = [
+			'test' => 'success',
+		];
+		$this->setupLogger('Slack Tester', $this->user['username'])->warning('Warning Test', $context);
+		$this->setResponse(200, 'Slack test connection completed - Please check Slack/Discord Channel');
+		return true;
+	}
 }

+ 22 - 0
api/v2/routes/connectionTester.php

@@ -666,4 +666,26 @@ $app->post('/test/jackett', function ($request, $response, $args) {
 	return $response
 		->withHeader('Content-Type', 'application/json;charset=UTF-8')
 		->withStatus($GLOBALS['responseCode']);
+});
+$app->post('/test/slack-logs', function ($request, $response, $args) {
+	/**
+	 * @OA\Post(
+	 *     security={{ "api_key":{} }},
+	 *     tags={"test connection"},
+	 *     path="/api/v2/test/slack-logs",
+	 *     summary="Test connection to Slack/Discord",
+	 *     @OA\Response(response="200",description="Success",@OA\JsonContent(ref="#/components/schemas/success-message")),
+	 *     @OA\Response(response="401",description="Unauthorized",@OA\JsonContent(ref="#/components/schemas/unauthorized-message")),
+	 *     @OA\Response(response="422",description="Error",@OA\JsonContent(ref="#/components/schemas/error-message")),
+	 *     @OA\Response(response="500",description="Error",@OA\JsonContent(ref="#/components/schemas/error-message")),
+	 * )
+	 */
+	$Organizr = ($request->getAttribute('Organizr')) ?? new Organizr();
+	if ($Organizr->qualifyRequest(1, true)) {
+		$Organizr->testConnectionSlackLogs();
+	}
+	$response->getBody()->write(jsonE($GLOBALS['api']));
+	return $response
+		->withHeader('Content-Type', 'application/json;charset=UTF-8')
+		->withStatus($GLOBALS['responseCode']);
 });

+ 10 - 0
api/vendor/nekonomokochan/php-json-logger/src/PhpJsonLogger/Logger.php

@@ -173,6 +173,11 @@ class Logger extends \Monolog\Logger
         return $this->traceId;
     }
 
+	public function setUsername(string $username)
+	{
+		$this->traceId = $username;
+	}
+
     /**
      * @return string
      */
@@ -181,6 +186,11 @@ class Logger extends \Monolog\Logger
         return $this->channel;
     }
 
+	public function setChannel(string $channel)
+	{
+		$this->channel = $channel;
+	}
+
     /**
      * @return int
      */