forked from GithubBackups/vichan
context.php: add log driver
This commit is contained in:
parent
f05c290b67
commit
55cb6bc400
@ -1,24 +1,53 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Vichan;
|
namespace Vichan;
|
||||||
|
|
||||||
defined('TINYBOARD') or exit;
|
use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers};
|
||||||
|
|
||||||
use Vichan\Driver\{HttpDriver, HttpDrivers};
|
defined('TINYBOARD') or exit;
|
||||||
|
|
||||||
|
|
||||||
interface Context {
|
interface Context {
|
||||||
|
public function getLog(): Log;
|
||||||
public function getHttpDriver(): HttpDriver;
|
public function getHttpDriver(): HttpDriver;
|
||||||
}
|
}
|
||||||
|
|
||||||
class AppContext implements Context {
|
class AppContext implements Context {
|
||||||
private array $config;
|
private array $config;
|
||||||
|
private ?Log $log;
|
||||||
private ?HttpDriver $http;
|
private ?HttpDriver $http;
|
||||||
|
|
||||||
|
|
||||||
|
private function initLogDriver(): Log {
|
||||||
|
$name = $this->config['log_system']['name'];
|
||||||
|
$level = $this->config['debug'] ? Log::DEBUG : Log::NOTICE;
|
||||||
|
$backend = $this->config['log_system']['type'];
|
||||||
|
|
||||||
|
// Check 'syslog' for backwards compatibility.
|
||||||
|
if ((isset($this->config['syslog']) && $this->config['syslog']) || $backend === 'syslog') {
|
||||||
|
return LogDrivers::syslog($name, $level, $this->config['log_system']['syslog_stderr']);
|
||||||
|
} elseif ($backend === 'file') {
|
||||||
|
return LogDrivers::file($name, $level, $this->config['log_system']['file_path']);
|
||||||
|
} elseif ($backend === 'stderr') {
|
||||||
|
return LogDrivers::stderr($name, $level);
|
||||||
|
} elseif ($backend === 'none') {
|
||||||
|
return LogDrivers::none();
|
||||||
|
} else {
|
||||||
|
return LogDrivers::error_log($name, $level);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function __construct(array $config) {
|
public function __construct(array $config) {
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getLog(): Log {
|
||||||
|
if (is_null($this->log)) {
|
||||||
|
$this->log = $this->initLogDriver();
|
||||||
|
}
|
||||||
|
return $this->log;
|
||||||
|
}
|
||||||
|
|
||||||
public function getHttpDriver(): HttpDriver {
|
public function getHttpDriver(): HttpDriver {
|
||||||
if (is_null($this->http)) {
|
if (is_null($this->http)) {
|
||||||
$this->http = HttpDrivers::getHttpDriver($this->config['upload_by_url_timeout'], $this->config['max_filesize']);
|
$this->http = HttpDrivers::getHttpDriver($this->config['upload_by_url_timeout'], $this->config['max_filesize']);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user