Source for file Logger.php

Documentation is available at Logger.php

  1. <?php
  2. /*
  3. * This file is part of Sylar.
  4. *
  5. * Sylar is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Sylar is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with Sylar. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * @copyright Copyright Sylar Development Team
  19. * @license http://www.gnu.org/licenses/ GNU Public License V2.0
  20. * @see https://launchpad.net/sylar/
  21. * @see http://www.giano-solutions.com
  22. */
  23.  
  24. import('sylar.common.system.ConfigBox');
  25. import('sylar.common.db.DataBaseManager');
  26.  
  27. /**
  28. * Gestione Log del sistema.
  29. * La classe estende DataBase e dunque dipende da essa
  30. * ma dipende anche da Sessione infatti viene usato un oggetto globale $SS che
  31. * deve essere dichiarato. Nel sistema GVisit tale oggetto è dichiarato nel
  32. * file di configurazione generale.
  33. *
  34. * @see Sylar_SqlLogger
  35. *
  36. * @package Sylar
  37. * @version 1.0
  38. * @since 12-2004
  39. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  40. * @copyright Sylar Development Team
  41. */
  42. class Sylar_Logger{
  43. /**
  44. * define the log method
  45. * It can assume the value of:
  46. * - db
  47. * - file
  48. * - xml
  49. */
  50. private $logSaveMethod;
  51. /**
  52. * Log levels
  53. * it conteins all possible levels
  54. */
  55. private $logLevels;
  56. /**
  57. * The level set for logger
  58. *
  59. * @var integer
  60. */
  61. private $logLevel;
  62. /**
  63. * DataBase Configuration for log
  64. *
  65. * @var Sylar_DataBaseConfiguration
  66. */
  67. private $oDataBaseConfig;
  68.  
  69. function __construct( $logLevel=SYLAR_LOG_LEVEL ){
  70. $this->setLogSaveMethod();
  71. $this->loadLogLevels();
  72. $this->setLogLevel($logLevel);
  73. }
  74. function __destruct() {
  75. # nothing to do
  76. }
  77.  
  78. /**
  79. * Set Logging into Db
  80. * Set the class to log information into specified DataBase, if not
  81. * specified it try to log into default Sylar DataBase
  82. *
  83. * @return void
  84. * @param Sylar_DataBaseConfiguration $dbConfiguration
  85. */
  86. public function setDataBaseConfig(Sylar_DataBaseConfiguration $dbConfiguration=null){
  87. if($dbConfiguration == null){
  88. try{
  89. $this->oDataBaseConfig = Sylar_DataBaseManager::getDefaultDbConfiguration();
  90. }catch (ExceptionInSylar $ex){
  91. throw new ExceptionInSylar("Wrong Db Configuration. Check the application configuration file.", 1 );
  92. }
  93. }else{
  94. $this->oDataBaseConfig = $dbConfiguration;
  95. }
  96. }
  97. /**
  98. * Return Db Configuration for log
  99. * it return the object with all database information
  100. *
  101. * @return Sylar_DataBaseConfiguration
  102. */
  103. public function getDataBaseConfig(){
  104. return $this->oDataBaseConfig;
  105. }
  106. /**
  107. * save event in log
  108. *
  109. * @since 2-2008
  110. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  111. *
  112. * @param string $event codice breve della sezione a cui appartiene l'evento
  113. * @param string $level il codice di livello di importanza dell'evento
  114. * @return boolean
  115. */
  116. public function logEvent($event, $level){
  117. #
  118. # verify if the event sould be log
  119. #
  120. if( $this->getLoggerLevelValue() > $this->getLevelValue($level) ) return true;
  121. switch ($this->getLogSaveMethod()) {
  122. case 'db':
  123. $this->logEventInDb($event, $level);
  124. break;
  125. case 'file':
  126. $this->logEventInFile($event, $level);
  127. break;
  128. default:
  129. $this->logEventInDb($event, $level);
  130. break;
  131. }
  132. }
  133. /**
  134. * save log into file
  135. *
  136. * @todo to implement
  137. *
  138. * @since 12-2004
  139. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  140. *
  141. * @param string $event codice breve della sezione a cui appartiene l'evento
  142. * @param string $level il codice di livello di importanza dell'evento
  143. * @return boolean
  144. */
  145. private function logEventInFile($event, $level){
  146. return true;
  147. }
  148. /**
  149. * Log di un evento.
  150. * Logga l'azione nella categoria $sezione e con tipo log $tipo. Il livello
  151. * è in ordine decrescente.
  152. *
  153. * @todo move SQL in Sql Space!
  154. *
  155. * @since 12-2004
  156. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  157. *
  158. * @return void
  159. * @param string $event codice breve della sezione a cui appartiene l'evento
  160. * @param string $level il codice di livello di importanza dell'evento
  161. */
  162. private function logEventInDb($event, $level){
  163.  
  164. try{
  165. $oDbMngr = new Sylar_DataBaseManager();
  166. // Import the correct Db Driver for Logger
  167. //
  168. import( $oDbMngr->getDriverClassPath($this->getDataBaseConfig()).".system.SqlLogger" );
  169. $sqlLogger = new Sylar_SqlLogger();
  170. $sqlArray = array(
  171. $this->getLevelValue($level),
  172. $level,
  173. $_SERVER["REMOTE_ADDR"],
  174. $event,
  175. $this->formatWebPageInfo(),
  176. $this->formatExtraInfo()
  177. );
  178. $sqlLogger->logEventInDb($sqlArray);
  179. }catch (ExceptionInSylar $ex){
  180. throw $ex;
  181. }
  182. }
  183.  
  184. /**
  185. * Informazioni Extra.
  186. * Formatta le informazioni extra da inserire nel campo info_extra della
  187. * tabella di LOG.
  188. *
  189. * @todo Da implementare
  190. *
  191. * @since 12-2004
  192. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  193. *
  194. * @return string
  195. */
  196. private function formatExtraInfo(){
  197. return "Extra info utente ecc...";
  198. }
  199.  
  200. /**
  201. * Informazioni Pagina.
  202. * Recupera le informazioni relative al file che ha scatenato l'evento da
  203. * Loggare
  204. *
  205. * @since 12-2004
  206. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  207. *
  208. * @return string
  209. *
  210. * @todo Da implementare e migliorare
  211. */
  212. private function formatWebPageInfo(){
  213. return "Web Info da fare";
  214. }
  215. /**
  216. * Ottieni Livello.
  217. * Ritorna il valore numerico del livello fornito, es. DEBUG=50, NORMAL=150 ecc...
  218. *
  219. * @since 12-2004
  220. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  221. *
  222. * @return integer
  223. * @param string $levelName il codice di livello di log, es. DEBUG, NORMAL, ecc...
  224. */
  225. private function getLevelValue($levelName){
  226. if(!$this->logLevels[$levelName]){
  227. return 44; # Security
  228. }else{
  229. return $this->logLevels[$levelName];
  230. }
  231. }
  232. /**
  233. * Set the log level
  234. *
  235. * @since 16/feb/08
  236. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  237. *
  238. * @param string $levelName the name ov the log level
  239. */
  240. public function setLogLevel($levelName=SYLAR_LOG_LEVEL){
  241. $this->logLevel = $this->getLevelValue($levelName);
  242. }
  243. /**
  244. * Return the numeric log lovel
  245. *
  246. * @since 16/feb/08
  247. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  248. *
  249. * @return integer
  250. */
  251. public function getLoggerLevelValue(){
  252. return $this->logLevel;
  253. }
  254. public function setLogSaveMethod($logMethod='db'){
  255. $this->logSaveMethod = $logMethod;
  256. // if log use db then set the DataBase Default Config...
  257. if($logMethod == 'db'){
  258. $this->setDataBaseConfig();
  259. }
  260. }
  261. public function getLogSaveMethod(){
  262. return $this->logSaveMethod;
  263. }
  264. /**
  265. * Load all possible log value
  266. *
  267. * @since 16/feb/08
  268. * @author Gianluca Giusti [brdp] <g.giusti@giano-solutions.com>
  269. *
  270. * @return void
  271. */
  272. private function loadLogLevels(){
  273.  
  274. /** All possible Log levels */
  275. $this->logLevels = array( 'DEBUG_NO_LIVELLO'=>44,
  276. 'DEBUG'=>50,
  277. 'VERBOSE'=>100,
  278. 'NORMAL'=>150,
  279. 'WARNING'=>200,
  280. 'FATAL'=>250,
  281. 'NO_LOG'=>300
  282. );
  283. }
  284. }
  285.  
  286. ?>

Documentation generated on Thu, 24 Apr 2008 16:14:17 +0200 by phpDocumentor 1.3.0RC3