Skip to content

Commit

Permalink
Changed logtype to file instead of owncloud.
Browse files Browse the repository at this point in the history
- Updated the config sample to point to log_type='file'
- Renamed the Class for logfile logging to File in namespace 'OC\Log\'.
  Changed the occurrences of 'OC\Log\Owncloud' to 'OC\Log\File'.
- Renamed the Class for log:file command to File in namespace 'OC\Core\Command\Log\File'.
  Changed registration of the command to use 'OC\Core\Command\Log\File'.
- Changed default Syslog tag to Nextcloud
- Retained backwards compatibility for configs with 'logtype' => 'owncloud'

- Adjusted tests for the new file log.

Closes nextcloud#490.
  • Loading branch information
Thomas Pulzer committed Jul 22, 2016
1 parent 4b4990c commit ba3f4f1
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@
* Setting this parameter to ``errorlog`` will use the PHP error_log function
* for logging.
*/
'log_type' => 'owncloud',
'log_type' => 'file',

/**
* Log file path for the Nextcloud logging type.
Expand Down
14 changes: 8 additions & 6 deletions core/Command/Log/OwnCloud.php → core/Command/Log/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class OwnCloud extends Command {
class File extends Command {

/** @var IConfig */
protected $config;
Expand All @@ -43,8 +43,8 @@ public function __construct(IConfig $config) {

protected function configure() {
$this
->setName('log:owncloud')
->setDescription('manipulate ownCloud logging backend')
->setName('log:file')
->setDescription('manipulate logging backend')
->addOption(
'enable',
null,
Expand All @@ -70,7 +70,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$toBeSet = [];

if ($input->getOption('enable')) {
$toBeSet['log_type'] = 'owncloud';
$toBeSet['log_type'] = 'file';
}

if ($file = $input->getOption('file')) {
Expand All @@ -89,12 +89,14 @@ protected function execute(InputInterface $input, OutputInterface $output) {
}

// display config
if ($this->config->getSystemValue('log_type', 'owncloud') === 'owncloud') {
// TODO: Drop backwards compatibility for config in the future
$logType = $this->config->getSystemValue('log_type', 'file');
if ($logType === 'file' || $logType === 'owncloud') {
$enabledText = 'enabled';
} else {
$enabledText = 'disabled';
}
$output->writeln('Log backend ownCloud: '.$enabledText);
$output->writeln('Log backend file: '.$enabledText);

$dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data');
$defaultLogFile = rtrim($dataDir, '/').'/nextcloud.log';
Expand Down
4 changes: 2 additions & 2 deletions core/Command/Log/Manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

class Manage extends Command {

const DEFAULT_BACKEND = 'owncloud';
const DEFAULT_BACKEND = 'file';
const DEFAULT_LOG_LEVEL = 2;
const DEFAULT_TIMEZONE = 'UTC';

Expand All @@ -54,7 +54,7 @@ protected function configure() {
'backend',
null,
InputOption::VALUE_REQUIRED,
'set the logging backend [owncloud, syslog, errorlog]'
'set the logging backend [file, syslog, errorlog]'
)
->addOption(
'level',
Expand Down
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
);

$application->add(new OC\Core\Command\Log\Manage(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Log\OwnCloud(\OC::$server->getConfig()));
$application->add(new OC\Core\Command\Log\File(\OC::$server->getConfig()));

$view = new \OC\Files\View();
$util = new \OC\Encryption\Util(
Expand Down
4 changes: 3 additions & 1 deletion lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ public function __construct($logger=null, SystemConfig $config=null, $normalizer

// FIXME: Add this for backwards compatibility, should be fixed at some point probably
if($logger === null) {
$this->logger = 'OC\\Log\\'.ucfirst($this->config->getValue('log_type', 'owncloud'));
// TODO: Drop backwards compatibility for config in the future
$logType = $this->config->getValue('log_type', 'file');
$this->logger = 'OC\\Log\\'.ucfirst($logType=='owncloud' ? 'file' : $logType);
call_user_func(array($this->logger, 'init'));
} else {
$this->logger = $logger;
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Log/Owncloud.php → lib/private/Log/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* Log is saved at data/nextcloud.log (on default)
*/

class Owncloud {
class File {
static protected $logFile;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Log/Syslog.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Syslog {
* Init class data
*/
public static function init() {
openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "ownCloud"), LOG_PID | LOG_CONS, LOG_USER);
openlog(\OC::$server->getSystemConfig()->getValue("syslog_tag", "Nextcloud"), LOG_PID | LOG_CONS, LOG_USER);
// Close at shutdown
register_shutdown_function('closelog');
}
Expand Down
5 changes: 3 additions & 2 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,9 @@ public function __construct($webRoot, \OC\Config $config) {
);
});
$this->registerService('Logger', function (Server $c) {
$logClass = $c->query('AllConfig')->getSystemValue('log_type', 'owncloud');
$logger = 'OC\\Log\\' . ucfirst($logClass);
$logClass = $c->query('AllConfig')->getSystemValue('log_type', 'file');
// TODO: Drop backwards compatibility for config in the future
$logger = 'OC\\Log\\' . ucfirst($logClass=='owncloud' ? 'file' : $logClass);
call_user_func(array($logger, 'init'));

return new Log($logger);
Expand Down
6 changes: 3 additions & 3 deletions settings/Controller/LogSettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public function setLogLevel($level) {
*/
public function getEntries($count=50, $offset=0) {
return new JSONResponse([
'data' => \OC\Log\Owncloud::getEntries($count, $offset),
'remain' => count(\OC\Log\Owncloud::getEntries(1, $offset + $count)) !== 0,
'data' => \OC\Log\File::getEntries($count, $offset),
'remain' => count(\OC\Log\File::getEntries(1, $offset + $count)) !== 0,
]);
}

Expand All @@ -106,7 +106,7 @@ public function getEntries($count=50, $offset=0) {
* @return StreamResponse
*/
public function download() {
$resp = new StreamResponse(\OC\Log\Owncloud::getLogFilePath());
$resp = new StreamResponse(\OC\Log\File::getLogFilePath());
$resp->addHeader('Content-Type', 'application/octet-stream');
$resp->addHeader('Content-Disposition', 'attachment; filename="nextcloud.log"');
return $resp;
Expand Down
7 changes: 4 additions & 3 deletions settings/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@

\OC::$server->getEventDispatcher()->dispatch('OC\Settings\Admin::loadAdditionalScripts');

$showLog = (\OC::$server->getConfig()->getSystemValue('log_type', 'owncloud') === 'owncloud');
$logType = \OC::$server->getConfig()->getSystemValue('log_type', 'file');
$showLog = ($logType === 'file' || $logType === 'owncloud');
$numEntriesToLoad = 3;
$entries = \OC\Log\Owncloud::getEntries($numEntriesToLoad + 1);
$entries = \OC\Log\File::getEntries($numEntriesToLoad + 1);
$entriesRemaining = count($entries) > $numEntriesToLoad;
$entries = array_slice($entries, 0, $numEntriesToLoad);
$logFilePath = \OC\Log\Owncloud::getLogFilePath();
$logFilePath = \OC\Log\File::getLogFilePath();
$doesLogFileExist = file_exists($logFilePath);
$logFileSize = 0;
if($doesLogFileExist) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Core/Command/Log/ManageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function testConvertLevelNumberInvalid() {
public function testGetConfiguration() {
$this->config->expects($this->at(0))
->method('getSystemValue')
->with('log_type', 'owncloud')
->with('log_type', 'file')
->willReturn('log_type_value');
$this->config->expects($this->at(1))
->method('getSystemValue')
Expand Down
10 changes: 5 additions & 5 deletions tests/Core/Command/Log/OwnCloudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
namespace Tests\Core\Command\Log;


use OC\Core\Command\Log\OwnCloud;
use OC\Core\Command\Log\File;
use Test\TestCase;

class OwnCloudTest extends TestCase {
Expand All @@ -45,7 +45,7 @@ protected function setUp() {
$this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
$this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');

$this->command = new OwnCloud($config);
$this->command = new File($config);
}

public function testEnable() {
Expand All @@ -55,7 +55,7 @@ public function testEnable() {
]));
$this->config->expects($this->once())
->method('setSystemValue')
->with('log_type', 'owncloud');
->with('log_type', 'file');

self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
}
Expand Down Expand Up @@ -99,15 +99,15 @@ public function testChangeRotateSize($optionValue, $configValue) {
public function testGetConfiguration() {
$this->config->method('getSystemValue')
->will($this->returnValueMap([
['log_type', 'owncloud', 'log_type_value'],
['log_type', 'file', 'log_type_value'],
['datadirectory', \OC::$SERVERROOT.'/data', '/data/directory/'],
['logfile', '/data/directory/nextcloud.log', '/var/log/nextcloud.log'],
['log_rotate_size', 0, 5 * 1024 * 1024],
]));

$this->consoleOutput->expects($this->at(0))
->method('writeln')
->with('Log backend ownCloud: disabled');
->with('Log backend file: disabled');
$this->consoleOutput->expects($this->at(1))
->method('writeln')
->with('Log file: /var/log/nextcloud.log');
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/Log/OwncloudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

namespace Test\Log;

use OC\Log\Owncloud;
use OC\Log\File;
use Test\TestCase;

/**
Expand All @@ -37,7 +37,7 @@ protected function setUp() {
$this->restore_logdateformat = $config->getSystemValue('logdateformat');

$config->setSystemValue("logfile", $config->getSystemValue('datadirectory') . "/logtest");
Owncloud::init();
File::init();
}
protected function tearDown() {
$config = \OC::$server->getConfig();
Expand All @@ -51,7 +51,7 @@ protected function tearDown() {
} else {
$config->deleteSystemValue("restore_logdateformat");
}
Owncloud::init();
File::init();
parent::tearDown();
}

Expand All @@ -62,7 +62,7 @@ public function testMicrosecondsLogTimestamp() {

# set format & write log line
$config->setSystemValue('logdateformat', 'u');
Owncloud::write('test', 'message', \OCP\Util::ERROR);
File::write('test', 'message', \OCP\Util::ERROR);

# read log line
$handle = @fopen($config->getSystemValue('logfile'), 'r');
Expand Down

0 comments on commit ba3f4f1

Please sign in to comment.