/framework/support/Logger.php

https://github.com/yonpols/ypframework · PHP · 154 lines · 119 code · 25 blank · 10 comment · 24 complexity · 2f605ae3ad359244f4228a6eae1fabfc MD5 · raw file

  1. <?php
  2. class Logger extends YPFObject {
  3. private static $frameworkLog = array();
  4. private static $frameworkLogFileName = null;
  5. private static $applicationLog = array();
  6. private static $applicationLogFileName = null;
  7. private static $mode = 'development';
  8. private static $active = true;
  9. private static $excludes = null;
  10. private static $colors = array('ERROR' => 31, 'INFO' => 32, 'NOTICE' => 33, 'DEBUG' => 36, 'ROUTE' => 34, 'SQL' => 35);
  11. public static function initialize() {
  12. $basePath = YPFramework::getPaths()->log;
  13. self::$mode = YPFramework::getMode();
  14. self::$active = YPFramework::getSetting('application.log.active', true);
  15. if (!defined('YPF_CMD') || YPFramework::getApplication() !== false) {
  16. self::$frameworkLogFileName = YPFramework::getFileName($basePath, sprintf('ypf-%s-%s.log', self::$mode, date('Y-m-d')));
  17. self::$applicationLogFileName = YPFramework::getFileName($basePath, sprintf('app-%s-%s.log', self::$mode, date('Y-m-d')));
  18. }
  19. self::writeLogs();
  20. }
  21. public static function finalize() {
  22. self::writeLogs();
  23. }
  24. /**
  25. * Logs a framework message
  26. * @param type $type
  27. * @param type $log
  28. */
  29. public static function framework($type, $log) {
  30. if (strpos($type, ':') !== false)
  31. list($type, $subtype) = explode(':', $type);
  32. else
  33. $subtype = 'LOG';
  34. if (self::isExcluded($type))
  35. return;
  36. $text = sprintf("[%s] \x1B[1;%d;1m%s:%s\x1B[0;0;0m %s\n", strftime('%F %T'), self::getColor($type), $type, $subtype, $log);
  37. if (self::$frameworkLogFileName)
  38. {
  39. if (($fd = @fopen(self::$frameworkLogFileName, "a"))) {
  40. fwrite($fd, $text);
  41. fclose($fd);
  42. } else
  43. self::$frameworkLog[] = $text;
  44. } else
  45. self::$frameworkLog[] = $text;
  46. }
  47. /**
  48. * Logs an application message
  49. * @param type $type
  50. * @param type $log
  51. */
  52. public static function application($type, $log) {
  53. if (strpos($type, ':') !== false)
  54. list($type, $subtype) = explode(':', $type);
  55. else
  56. $subtype = 'LOG';
  57. if (self::isExcluded($type))
  58. return;
  59. $text = sprintf("[%s] \x1B[1;%d;1m%s:%s\x1B[0;0;0m %s\n", strftime('%F %T'), self::getColor($type), $type, $subtype, $log);
  60. if (self::$applicationLogFileName)
  61. {
  62. if (($fd = @fopen(self::$applicationLogFileName, "a"))) {
  63. fwrite($fd, $text);
  64. fclose($fd);
  65. } else
  66. self::$applicationLog[] = $text;
  67. } else
  68. self::$applicationLog[] = $text;
  69. }
  70. public static function rotateLogs() {
  71. if (!function_exists('gzencode'))
  72. return false;
  73. $path = YPFramework::getFileName(YPFramework::getPaths()->log, '*.log');
  74. $files = glob($path);
  75. $rotated = 0;
  76. foreach ($files as $file) {
  77. if ($file == self::$applicationLogFileName)
  78. continue;
  79. if ($file == self::$frameworkLogFileName);
  80. continue;
  81. $gzdata = gzencode(file_get_contents($file), 9);
  82. $fp = fopen($file.'.gz', "w");
  83. fwrite($fp, $gzdata);
  84. fclose($fp);
  85. $rotated++;
  86. }
  87. return $rotated;
  88. }
  89. private static function writeLogs() {
  90. if (self::$active && (!defined('YPF_CMD') || YPFramework::getApplication() !== false)) {
  91. if (count(self::$frameworkLog))
  92. {
  93. if (($fd = @fopen(self::$frameworkLogFileName, "a"))) {
  94. foreach(self::$frameworkLog as $log)
  95. fwrite($fd, $log);
  96. fclose($fd);
  97. self::$frameworkLog = array();
  98. }
  99. }
  100. if (count(self::$applicationLog))
  101. {
  102. if (($fd = @fopen(self::$applicationLogFileName, "a"))) {
  103. foreach(self::$applicationLog as $log)
  104. fwrite($fd, $log);
  105. fclose($fd);
  106. self::$applicationLog = array();
  107. }
  108. }
  109. }
  110. }
  111. private static function getColor($type) {
  112. if (isset(self::$colors[$type]))
  113. return self::$colors[$type];
  114. else
  115. return 0;
  116. }
  117. private static function isExcluded($type) {
  118. $excludes = array(
  119. false => array(),
  120. true => array(
  121. 'SQL', 'DEBUG'
  122. )
  123. );
  124. if (self::$excludes === null)
  125. self::$excludes = YPFramework::getSetting('application.log.exclude', $excludes[YPFramework::inProduction()]);
  126. return (array_search($type, self::$excludes) !== false);
  127. }
  128. }
  129. ?>