2018-04-24 20:14:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
|
|
|
|
*
|
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 09:57:22 +00:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2018-06-06 20:40:06 +00:00
|
|
|
* @author Johannes Ernst <jernst@indiecomputing.com>
|
2018-04-24 20:14:00 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2021-06-04 19:52:51 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2018-04-24 20:14:00 +00:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 18:57:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-04-24 20:14:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OC\Log;
|
|
|
|
|
2018-04-25 00:27:43 +00:00
|
|
|
use OC\Log;
|
2018-04-26 21:54:11 +00:00
|
|
|
use OC\SystemConfig;
|
2018-04-25 00:27:43 +00:00
|
|
|
use OCP\ILogger;
|
2018-04-24 20:14:00 +00:00
|
|
|
use OCP\IServerContainer;
|
2018-04-25 00:27:43 +00:00
|
|
|
use OCP\Log\ILogFactory;
|
|
|
|
use OCP\Log\IWriter;
|
2021-02-11 15:03:11 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2018-04-24 20:14:00 +00:00
|
|
|
|
2018-04-25 00:27:43 +00:00
|
|
|
class LogFactory implements ILogFactory {
|
2018-04-24 20:14:00 +00:00
|
|
|
/** @var IServerContainer */
|
|
|
|
private $c;
|
2018-04-26 21:54:11 +00:00
|
|
|
/** @var SystemConfig */
|
|
|
|
private $systemConfig;
|
2018-04-24 20:14:00 +00:00
|
|
|
|
2018-04-26 21:54:11 +00:00
|
|
|
public function __construct(IServerContainer $c, SystemConfig $systemConfig) {
|
2018-04-24 20:14:00 +00:00
|
|
|
$this->c = $c;
|
2018-04-26 21:54:11 +00:00
|
|
|
$this->systemConfig = $systemConfig;
|
2018-04-24 20:14:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \OCP\AppFramework\QueryException
|
|
|
|
*/
|
2018-04-25 00:27:43 +00:00
|
|
|
public function get(string $type):IWriter {
|
2018-04-24 20:14:00 +00:00
|
|
|
switch (strtolower($type)) {
|
|
|
|
case 'errorlog':
|
2022-12-05 18:54:37 +00:00
|
|
|
return new Errorlog($this->systemConfig);
|
2018-04-24 20:14:00 +00:00
|
|
|
case 'syslog':
|
|
|
|
return $this->c->resolve(Syslog::class);
|
2018-06-06 20:40:06 +00:00
|
|
|
case 'systemd':
|
|
|
|
return $this->c->resolve(Systemdlog::class);
|
2018-04-24 20:14:00 +00:00
|
|
|
case 'file':
|
|
|
|
return $this->buildLogFile();
|
|
|
|
|
2023-01-20 10:45:08 +00:00
|
|
|
// Backwards compatibility for old and fallback for unknown log types
|
2018-04-24 20:14:00 +00:00
|
|
|
case 'owncloud':
|
|
|
|
case 'nextcloud':
|
|
|
|
default:
|
2018-04-25 00:27:43 +00:00
|
|
|
return $this->buildLogFile();
|
2018-04-24 20:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-25 00:27:43 +00:00
|
|
|
public function getCustomLogger(string $path):ILogger {
|
|
|
|
$log = $this->buildLogFile($path);
|
2018-04-26 21:54:11 +00:00
|
|
|
return new Log($log, $this->systemConfig);
|
2018-04-25 00:27:43 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 15:15:14 +00:00
|
|
|
protected function createNewLogger(string $type, string $tag, string $path): IWriter {
|
|
|
|
switch (strtolower($type)) {
|
|
|
|
case 'errorlog':
|
2022-12-05 18:54:37 +00:00
|
|
|
return new Errorlog($this->systemConfig, $tag);
|
2022-01-19 15:15:14 +00:00
|
|
|
case 'syslog':
|
|
|
|
return new Syslog($this->systemConfig, $tag);
|
|
|
|
case 'systemd':
|
|
|
|
return new Systemdlog($this->systemConfig, $tag);
|
|
|
|
case 'file':
|
|
|
|
case 'owncloud':
|
|
|
|
case 'nextcloud':
|
|
|
|
default:
|
|
|
|
return $this->buildLogFile($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCustomPsrLogger(string $path, string $type = 'file', string $tag = 'Nextcloud'): LoggerInterface {
|
|
|
|
$log = $this->createNewLogger($type, $tag, $path);
|
2021-02-11 15:03:11 +00:00
|
|
|
return new PsrLoggerAdapter(
|
|
|
|
new Log($log, $this->systemConfig)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-25 00:27:43 +00:00
|
|
|
protected function buildLogFile(string $logFile = ''):File {
|
2018-04-26 21:54:11 +00:00
|
|
|
$defaultLogFile = $this->systemConfig->getValue('datadirectory', \OC::$SERVERROOT.'/data').'/nextcloud.log';
|
2020-04-10 12:19:56 +00:00
|
|
|
if ($logFile === '') {
|
2018-04-26 21:54:11 +00:00
|
|
|
$logFile = $this->systemConfig->getValue('logfile', $defaultLogFile);
|
2018-04-25 00:27:43 +00:00
|
|
|
}
|
2018-04-24 20:14:00 +00:00
|
|
|
$fallback = $defaultLogFile !== $logFile ? $defaultLogFile : '';
|
|
|
|
|
2018-04-26 21:54:11 +00:00
|
|
|
return new File($logFile, $fallback, $this->systemConfig);
|
2018-04-24 20:14:00 +00:00
|
|
|
}
|
|
|
|
}
|