From 820cd0fb75bac88b4debe8f3fb28c1118b8587fa Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 16 Oct 2011 20:49:14 +0200 Subject: [PATCH] provide a logging mechanism --- lib/app.php | 2 ++ lib/log.php | 71 ++++++++++++++++++++++++++++++++++++++ settings/log.php | 41 ++++++++++++++++++++++ settings/templates/log.php | 29 ++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 lib/log.php create mode 100644 settings/log.php create mode 100644 settings/templates/log.php diff --git a/lib/app.php b/lib/app.php index b9eea483a5..51dcb8d573 100644 --- a/lib/app.php +++ b/lib/app.php @@ -222,6 +222,8 @@ class OC_App{ $settings[] = array( "id" => "core_users", "order" => 2, "href" => OC_Helper::linkTo( "settings", "users.php" ), "name" => $l->t("Users"), "icon" => OC_Helper::imagePath( "settings", "users.svg" )); // admin apps menu $settings[] = array( "id" => "core_apps", "order" => 3, "href" => OC_Helper::linkTo( "settings", "apps.php?installed" ), "name" => $l->t("Apps"), "icon" => OC_Helper::imagePath( "settings", "apps.svg" )); + // admin log menu + $settings[] = array( "id" => "core_log", "order" => 4, "href" => OC_Helper::linkTo( "settings", "log.php" ), "name" => $l->t("Log"), "icon" => OC_Helper::imagePath( "log", "apps.svg" )); // if there're some admin forms if(!empty(self::$adminForms)) diff --git a/lib/log.php b/lib/log.php new file mode 100644 index 0000000000..a951b67603 --- /dev/null +++ b/lib/log.php @@ -0,0 +1,71 @@ +. + * + */ + +/** + *logging utilities + * + * Log is saved at data/owncloud.log (on default) + */ + +class OC_Log{ + const DEBUG=0; + const INFO=1; + const WARN=2; + const ERROR=3; + const FATAL=4; + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int level + */ + public static function write($app,$message,$level){ + $minLevel=OC_Config::getValue( "loglevel", 2 ); + if($level>$minLevel){ + $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); + $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); + $entry=array('app'=>$app,'message'=>$message,'level'=>$level,'time'=>time()); + $fh=fopen($logFile,'a'); + fwrite($fh,json_encode($entry)."\n"); + fclose($fh); + } + } + + public static function getEntries(){ + $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); + $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); + $entries=array(); + if(!file_exists($logFile)){ + return array(); + } + $fh=fopen($logFile,'r'); + while(!feof($fh)){ + $line=fgets($fh); + if($line){ + $entries[]=json_decode($line); + } + } + fclose($fh); + return $entries; + } +} diff --git a/settings/log.php b/settings/log.php new file mode 100644 index 0000000000..e181a5a496 --- /dev/null +++ b/settings/log.php @@ -0,0 +1,41 @@ +. + * + */ + +require_once('../lib/base.php'); +OC_Util::checkAdminUser(); + +// Load the files we need +OC_Util::addStyle( "settings", "settings" ); +OC_Util::addScript( "settings", "apps" ); +OC_App::setActiveNavigationEntry( "core_log" ); + +$entries=OC_Log::getEntries(); + +function compareEntries($a,$b){ + return $b->time-$a>time; +} +usort($entries, 'compareEntries'); + +$tmpl = new OC_Template( "settings", "log", "user" ); +$tmpl->assign('entries',$entries); + +$tmpl->printPage(); diff --git a/settings/templates/log.php b/settings/templates/log.php new file mode 100644 index 0000000000..467f594186 --- /dev/null +++ b/settings/templates/log.php @@ -0,0 +1,29 @@ + + * This file is licensed under the Affero General Public License version 3 or later. + * See the COPYING-README file. + */ +$levels=array('DEBUG','INFO','WARN','ERROR','FATAL'); +?> + +
+ +
+ + + + + + + + + +
+ level];?> + + app;?> + + message;?> + + l('datetime',$entry->time);?> +
\ No newline at end of file