Skip to content

Commit

Permalink
Feature: Added proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
MAZUR Piotr committed Dec 17, 2024
1 parent 4edf4e5 commit 13128a9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
15 changes: 15 additions & 0 deletions config/teams-logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,20 @@
*/
'teams_logging_webhook_emergency' => env('TEAMS_LOGGING_WEBHOOK_EMERGENCY', null),

/**
* Proxy URL - if it's set check if authentication to proxy is needed.
*/
'teams_logging_proxy_url' => env('TEAMS_LOGGING_PROXY_URL', null),

/**
* Proxy User - required if authentication to proxy is needed.
*/
'teams_logging_proxy_user' => env('TEAMS_LOGGING_PROXY_USER', null),

/**
* Proxy Password - required if authentication to proxy is needed.
*/
'teams_logging_proxy_password' => env('TEAMS_LOGGING_PROXY_PASSWORD', null)


];
6 changes: 4 additions & 2 deletions src/Handlers/TeamsLoggingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ class TeamsLoggingHandler extends \Monolog\Handler\AbstractProcessingHandler
public $method;
public $format;
public $webhooks;
public $proxy;

public function __construct(string $lvl = 'debug', string $method = 'default', string $format = 'default', array $webhooks = [])
public function __construct(string $lvl = 'debug', string $method = 'default', string $format = 'default', array $webhooks = [], array $proxy = [])
{
parent::__construct();
$this->lvl = strtolower($lvl);
$this->method = $method;
$this->format = $format;
$this->webhooks = $webhooks;
$this->proxy = $proxy;
}

protected function validateWebhook(string $levelName): string|null
Expand Down Expand Up @@ -68,7 +70,7 @@ protected function write(array $record): void
if($webhookUrl !== null && LevelPriorityHelper::shouldSendMessage($this->lvl, strtolower($record['level_name'])))
{
$message = MessageFormatHelper::getMessage($this->format, $record);
$logging = new TeamsLoggingSender($webhookUrl);
$logging = new TeamsLoggingSender($webhookUrl, $this->proxy);
$logging->send($message);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/TeamsLoggingLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function __invoke(array $config)
return new \Monolog\Logger(
getenv('APP_NAME'),
[
new TeamsLoggingHandler($config['level'], $config['method'], $config['format'], $config['webhooks'])
new TeamsLoggingHandler($config['level'], $config['method'], $config['format'], $config['webhooks'], $config['proxy'])
],
[
new MemoryUsageProcessor(),
Expand Down
12 changes: 11 additions & 1 deletion src/TeamsLoggingSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
class TeamsLoggingSender
{
private $webhookUrl;
private $proxy;

public function __construct(string $webhookUrl = null)
public function __construct(string $webhookUrl = null, array $proxy = [])
{
$this->webhookUrl = $webhookUrl;
$this->proxy = $proxy;
}

public function send($message)
Expand All @@ -25,6 +27,14 @@ public function send($message)
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);

if (!empty($this->proxy['url'])) {
curl_setopt($ch, CURLOPT_PROXY, $this->proxy['url']);

if (!empty($this->proxy['user']) && !empty($this->proxy['password'])) {
$proxyUserPwd = $this->proxy['user'] . ':' . $this->proxy['password'];
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyUserPwd);
}
}

$result = curl_exec($ch);

Expand Down
8 changes: 7 additions & 1 deletion src/TeamsLoggingServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ public function register()
$webhooks['critical'] = getenv('TEAMS_LOGGING_WEBHOOK_CRITICAL') ?: $this->app->config->get('teams-logging.teams_logging_webhook_critical', null);
$webhooks['alert'] = getenv('TEAMS_LOGGING_WEBHOOK_ALERT') ?: $this->app->config->get('teams-logging.teams_logging_webhook_alert', null);
$webhooks['emergency'] = getenv('TEAMS_LOGGING_WEBHOOK_EMERGENCY') ?: $this->app->config->get('teams-logging.teams_logging_webhook_emergency', null);
$teams_logging_proxy_url = getenv('TEAMS_LOGGING_PROXY_URL') ?: $this->app->config->get('teams-logging.teams_logging_proxy_url', null);
$teams_logging_proxy_user = getenv('TEAMS_LOGGING_PROXY_USER') ?: $this->app->config->get('teams-logging.teams_logging_proxy_user', null);
$teams_logging_proxy_password = getenv('TEAMS_LOGGING_PROXY_PASSWORD') ?: $this->app->config->get('teams-logging.teams_logging_proxy_password', null);

return new \Peroxovy\LaravelTeamsLogging\TeamsLoggingSender(
$teams_logging_level,
$teams_logging_method,
$webhooks
$webhooks,
$teams_logging_proxy_url,
$teams_logging_proxy_user,
$teams_logging_proxy_password
);
});

Expand Down

0 comments on commit 13128a9

Please sign in to comment.