diff options
author | Morris Jobke <morris.jobke@gmail.com> | 2013-08-28 13:17:38 -0700 |
---|---|---|
committer | Morris Jobke <morris.jobke@gmail.com> | 2013-08-28 13:17:38 -0700 |
commit | 125824b78d0e0d5863ad5b6622ab4a3bd4641652 (patch) | |
tree | b03c57dd8b8e2759b93cc3f498a5419771a594ea | |
parent | 584857de696e0035abb561b569588af406d818d7 (diff) | |
parent | d4952bd9df679306ff3e2a8efba1f37ed9d97044 (diff) | |
download | nextcloud-server-125824b78d0e0d5863ad5b6622ab4a3bd4641652.tar.gz nextcloud-server-125824b78d0e0d5863ad5b6622ab4a3bd4641652.zip |
Merge pull request #3961 from owncloud/logrotate
Very simple log rotation
-rw-r--r-- | config/config.sample.php | 15 | ||||
-rw-r--r-- | lib/base.php | 16 | ||||
-rw-r--r-- | lib/log/rotate.php | 35 |
3 files changed, 63 insertions, 3 deletions
diff --git a/config/config.sample.php b/config/config.sample.php index 24ba541ac5c..f5cb33732f8 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -141,10 +141,22 @@ $CONFIG = array( /* Loglevel to start logging at. 0=DEBUG, 1=INFO, 2=WARN, 3=ERROR (default is WARN) */ "loglevel" => "", +/* date format to be used while writing to the owncloud logfile */ +'logdateformat' => 'F d, Y H:i:s', + /* Append all database queries and parameters to the log file. (watch out, this option can increase the size of your log file)*/ "log_query" => false, +/* + * Configure the size in bytes log rotation should happen, 0 or false disables the rotation. + * This rotates the current owncloud logfile to a new name, this way the total log usage + * will stay limited and older entries are available for a while longer. The + * total disk usage is twice the configured size. + * WARNING: When you use this, the log entries will eventually be lost. + */ +'log_rotate_size' => false, // 104857600, // 100 MiB + /* Lifetime of the remember login cookie, default is 15 days */ "remember_login_cookie_lifetime" => 60*60*24*15, @@ -189,7 +201,4 @@ $CONFIG = array( 'customclient_desktop' => '', //http://owncloud.org/sync-clients/ 'customclient_android' => '', //https://play.google.com/store/apps/details?id=com.owncloud.android 'customclient_ios' => '', //https://itunes.apple.com/us/app/owncloud/id543672169?mt=8 - -// date format to be used while writing to the owncloud logfile -'logdateformat' => 'F d, Y H:i:s' ); diff --git a/lib/base.php b/lib/base.php index 0c9fe329b8f..2e6a37c9f4e 100644 --- a/lib/base.php +++ b/lib/base.php @@ -491,6 +491,7 @@ class OC { self::registerCacheHooks(); self::registerFilesystemHooks(); self::registerShareHooks(); + self::registerLogRotate(); //make sure temporary files are cleaned up register_shutdown_function(array('OC_Helper', 'cleanTmp')); @@ -553,6 +554,21 @@ class OC { } /** + * register hooks for the cache + */ + public static function registerLogRotate() { + if (OC_Config::getValue('installed', false) && OC_Config::getValue('log_rotate_size', false)) { + //don't try to do this before we are properly setup + // register cache cleanup jobs + try { //if this is executed before the upgrade to the new backgroundjob system is completed it will throw an exception + \OCP\BackgroundJob::registerJob('OC\Log\Rotate', OC_Config::getValue("datadirectory", OC::$SERVERROOT.'/data').'/owncloud.log'); + } catch (Exception $e) { + + } + } + } + + /** * register hooks for the filesystem */ public static function registerFilesystemHooks() { diff --git a/lib/log/rotate.php b/lib/log/rotate.php new file mode 100644 index 00000000000..bf23ad588b3 --- /dev/null +++ b/lib/log/rotate.php @@ -0,0 +1,35 @@ +<?php +/** + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Log; + +/** + * This rotates the current logfile to a new name, this way the total log usage + * will stay limited and older entries are available for a while longer. + * For more professional log management set the 'logfile' config to a different + * location and manage that with your own tools. + */ +class Rotate extends \OC\BackgroundJob\Job { + private $max_log_size; + public function run($logFile) { + $this->max_log_size = \OC_Config::getValue('log_rotate_size', false); + if ($this->max_log_size) { + $filesize = @filesize($logFile); + if ($filesize >= $this->max_log_size) { + $this->rotate($logFile); + } + } + } + + protected function rotate($logfile) { + $rotatedLogfile = $logfile.'.1'; + rename($logfile, $rotatedLogfile); + $msg = 'Log file "'.$logfile.'" was over '.$this->max_log_size.' bytes, moved to "'.$rotatedLogfile.'"'; + \OC_Log::write('OC\Log\Rotate', $msg, \OC_Log::WARN); + } +} |