/system/log/Log.php

https://github.com/botskonet/aspen-framework · PHP · 259 lines · 140 code · 53 blank · 66 comment · 23 complexity · 96c27f6360f8e8e8d5142daad945983b MD5 · raw file

  1. <?php
  2. /**
  3. * @package Aspen_Framework
  4. * @subpackage System
  5. * @author Michael Botsko
  6. * @copyright 2009 Trellis Development, LLC
  7. * @since 1.0
  8. */
  9. /**
  10. * Provides a method of writing to a log file.
  11. * @package Aspen_Framework
  12. */
  13. class Log {
  14. /**
  15. * @var boolean $on Whether or not logging is enabled
  16. * @access private
  17. */
  18. private $on = false;
  19. /**
  20. * @var boolean $dir The directory path to our log files
  21. * @access private
  22. */
  23. private $dir = false;
  24. /**
  25. * @var boolean $full_path Contains the full path to our current log file
  26. * @access private
  27. */
  28. private $full_path = false;
  29. /**
  30. * Sets up the directories and files as necessary
  31. * @access public
  32. */
  33. public function enable(){
  34. $loaded = false;
  35. $this->on = config()->get('enable_logging');
  36. $this->dir = config()->get('log_dir');
  37. $this->level = config()->get('log_verbosity');
  38. if($this->on && $this->dir){
  39. // verify directory exists and is writeable
  40. if(!$this->checkDirectory()){
  41. $this->on = false;
  42. error()->raise(1,
  43. 'Logging is enabled, but directory is not writeable. Dir: ' . $this->dir, __FILE__, __LINE__);
  44. }
  45. // create a log file
  46. if(!$this->createLogFile()){
  47. $this->on = false;
  48. error()->raise(1,
  49. 'Failed creating new log file.', __FILE__, __LINE__);
  50. }
  51. if($this->on){
  52. $loaded = true;
  53. }
  54. }
  55. if($loaded){
  56. $this->write('Logging has been activated at ' . Date::formatMicrotime(Date::microtime(EXECUTION_START)) . '.', 'w');
  57. if($this->level == 1){
  58. $this->logCoreInfo();
  59. }
  60. }
  61. }
  62. /**
  63. * Checks for a valid directory, attempts to create
  64. * @return boolean
  65. * @access private
  66. */
  67. private function checkDirectory(){
  68. $dir_ok = false;
  69. if($this->dir){
  70. if(is_dir($this->dir) && is_writeable($this->dir)){
  71. $dir_ok = true;
  72. } else {
  73. $dir_ok = mkdir($this->dir);
  74. }
  75. }
  76. return $dir_ok;
  77. }
  78. /**
  79. * Uses or creates new log files
  80. * @return boolean
  81. * @access private
  82. */
  83. private function createLogFile(){
  84. $new_filename = 'log';
  85. if(config()->get('timestamp_log_file')){
  86. $new_filename .= '-' . Date::formatMicrotime(Date::microtime(EXECUTION_START));
  87. }
  88. $this->full_path = $this->dir . DS . $new_filename;
  89. if(!$fileexists = file_exists($this->full_path)){
  90. $fileexists = touch($this->full_path);
  91. }
  92. return $fileexists;
  93. }
  94. /**
  95. * Writes a new message to the log file
  96. * @param string $message
  97. * @access public
  98. */
  99. public function write($message = '(empty message)', $mode = 'a'){
  100. if($this->on){
  101. files()->useFile($this->full_path);
  102. if(is_array($message) || is_object($message)){
  103. files()->write( print_r($message, true) . "\n", $mode);
  104. } else {
  105. files()->write( preg_replace('/[\t]+/', '', trim($message)) . "\n", $mode);
  106. }
  107. }
  108. }
  109. /**
  110. * Writes a breaking line
  111. * @access public
  112. */
  113. public function hr(){
  114. $this->write('++======================================================++');
  115. }
  116. /**
  117. * Writes a new section header
  118. * @access public
  119. */
  120. public function section($title = 'Section'){
  121. $this->write('');
  122. $this->write('++======================================================++');
  123. $this->write('++ ' . $title);
  124. $this->write('++======================================================++');
  125. }
  126. /**
  127. * Sets the proper print string for a var
  128. * @param mixed $value
  129. * @return string
  130. * @access private
  131. */
  132. private function logValue($value){
  133. if(is_array($value) || is_object($value)){
  134. return serialize($value);
  135. }
  136. return $value;
  137. }
  138. /**
  139. * Logs all core aspen framework data to the logfile
  140. * @access private
  141. */
  142. private function logCoreInfo(){
  143. if($this->on){
  144. // record all constants
  145. $this->section('Constants');
  146. $defines = get_defined_constants(true);
  147. foreach($defines['user'] as $define => $value){
  148. $this->write('Constant ' . $define . ' was set to a value of: ' . $this->logValue($value));
  149. }
  150. // record all configurations
  151. $this->section('Configurations');
  152. $config = app()->getConfig()->_getConfigArray();
  153. foreach($config as $config => $value){
  154. $this->write('Config ' . $config . ' was set to a value of: ' . $this->logValue($value));
  155. }
  156. $this->section('Loaded System Libraries');
  157. $lib = app()->getLoadedLibraries();
  158. foreach($lib as $class){
  159. $this->write('Library Class ' . $class['classname'] . ' was loaded.');
  160. }
  161. $this->section('Session Data');
  162. $session = app()->params->getRawSource('session');
  163. $this->write('Session_id: ' . session_id());
  164. foreach($session as $key => $value){
  165. $this->write('$_SESSION[\''.$key.'\'] = ' . $this->logValue($value));
  166. }
  167. $this->section('POST Data');
  168. $post = app()->params->getRawSource('post');
  169. foreach($post as $key => $value){
  170. $this->write('$_POST[\''.$key.'\'] = ' . $this->logValue($value));
  171. }
  172. $this->section('GET Data');
  173. $get = app()->params->getRawSource('get');
  174. foreach($get as $key => $value){
  175. $this->write('$_GET[\''.$key.'\'] = ' . $this->logValue($value));
  176. }
  177. $this->section('SERVER Data');
  178. $server = app()->params->getRawSource('server');
  179. foreach($server as $key => $value){
  180. $this->write('$_SERVER[\''.$key.'\'] = ' . $this->logValue($value));
  181. }
  182. $this->section('FILES Data');
  183. $server = app()->params->getRawSource('files');
  184. foreach($server as $key => $value){
  185. $this->write('$_FILES[\''.$key.'\'] = ' . $this->logValue($value));
  186. }
  187. // save all urls/paths to log for debugging
  188. $this->section('Router Urls & Paths');
  189. $this->write('Router::domainUrl set to: ' . router()->domainUrl());
  190. $this->write('Router::appUrl set to: ' . router()->appUrl());
  191. $this->write('Router::getPath set to: ' . router()->getPath());
  192. $this->write('Router::interfaceUrl set to: ' . router()->interfaceUrl());
  193. //$this->write('Router::moduleUrl set to: ' . router()->moduleUrl());
  194. $this->write('Router::staticUrl set to: ' . router()->staticUrl());
  195. $this->write('Router::fullUrl set to: ' . router()->fullUrl());
  196. $this->section('Bootstrap');
  197. $this->write('Installed checks returned ' . (app()->isInstalled() ? 'true' : 'false'));
  198. if(ConfigLoader::checkUserConfigExists()){
  199. $this->write('Found user config file.');
  200. } else {
  201. $this->write('User config was NOT FOUND.');
  202. }
  203. }
  204. }
  205. }
  206. ?>