/framework/support/Logger.php
https://github.com/yonpols/ypframework · PHP · 154 lines · 119 code · 25 blank · 10 comment · 24 complexity · 2f605ae3ad359244f4228a6eae1fabfc MD5 · raw file
- <?php
- class Logger extends YPFObject {
- private static $frameworkLog = array();
- private static $frameworkLogFileName = null;
- private static $applicationLog = array();
- private static $applicationLogFileName = null;
- private static $mode = 'development';
- private static $active = true;
- private static $excludes = null;
- private static $colors = array('ERROR' => 31, 'INFO' => 32, 'NOTICE' => 33, 'DEBUG' => 36, 'ROUTE' => 34, 'SQL' => 35);
- public static function initialize() {
- $basePath = YPFramework::getPaths()->log;
- self::$mode = YPFramework::getMode();
- self::$active = YPFramework::getSetting('application.log.active', true);
- if (!defined('YPF_CMD') || YPFramework::getApplication() !== false) {
- self::$frameworkLogFileName = YPFramework::getFileName($basePath, sprintf('ypf-%s-%s.log', self::$mode, date('Y-m-d')));
- self::$applicationLogFileName = YPFramework::getFileName($basePath, sprintf('app-%s-%s.log', self::$mode, date('Y-m-d')));
- }
- self::writeLogs();
- }
- public static function finalize() {
- self::writeLogs();
- }
- /**
- * Logs a framework message
- * @param type $type
- * @param type $log
- */
- public static function framework($type, $log) {
- if (strpos($type, ':') !== false)
- list($type, $subtype) = explode(':', $type);
- else
- $subtype = 'LOG';
- if (self::isExcluded($type))
- return;
- $text = sprintf("[%s] \x1B[1;%d;1m%s:%s\x1B[0;0;0m %s\n", strftime('%F %T'), self::getColor($type), $type, $subtype, $log);
- if (self::$frameworkLogFileName)
- {
- if (($fd = @fopen(self::$frameworkLogFileName, "a"))) {
- fwrite($fd, $text);
- fclose($fd);
- } else
- self::$frameworkLog[] = $text;
- } else
- self::$frameworkLog[] = $text;
- }
- /**
- * Logs an application message
- * @param type $type
- * @param type $log
- */
- public static function application($type, $log) {
- if (strpos($type, ':') !== false)
- list($type, $subtype) = explode(':', $type);
- else
- $subtype = 'LOG';
- if (self::isExcluded($type))
- return;
- $text = sprintf("[%s] \x1B[1;%d;1m%s:%s\x1B[0;0;0m %s\n", strftime('%F %T'), self::getColor($type), $type, $subtype, $log);
- if (self::$applicationLogFileName)
- {
- if (($fd = @fopen(self::$applicationLogFileName, "a"))) {
- fwrite($fd, $text);
- fclose($fd);
- } else
- self::$applicationLog[] = $text;
- } else
- self::$applicationLog[] = $text;
- }
- public static function rotateLogs() {
- if (!function_exists('gzencode'))
- return false;
- $path = YPFramework::getFileName(YPFramework::getPaths()->log, '*.log');
- $files = glob($path);
- $rotated = 0;
- foreach ($files as $file) {
- if ($file == self::$applicationLogFileName)
- continue;
- if ($file == self::$frameworkLogFileName);
- continue;
- $gzdata = gzencode(file_get_contents($file), 9);
- $fp = fopen($file.'.gz', "w");
- fwrite($fp, $gzdata);
- fclose($fp);
- $rotated++;
- }
- return $rotated;
- }
- private static function writeLogs() {
- if (self::$active && (!defined('YPF_CMD') || YPFramework::getApplication() !== false)) {
- if (count(self::$frameworkLog))
- {
- if (($fd = @fopen(self::$frameworkLogFileName, "a"))) {
- foreach(self::$frameworkLog as $log)
- fwrite($fd, $log);
- fclose($fd);
- self::$frameworkLog = array();
- }
- }
- if (count(self::$applicationLog))
- {
- if (($fd = @fopen(self::$applicationLogFileName, "a"))) {
- foreach(self::$applicationLog as $log)
- fwrite($fd, $log);
- fclose($fd);
- self::$applicationLog = array();
- }
- }
- }
- }
- private static function getColor($type) {
- if (isset(self::$colors[$type]))
- return self::$colors[$type];
- else
- return 0;
- }
- private static function isExcluded($type) {
- $excludes = array(
- false => array(),
- true => array(
- 'SQL', 'DEBUG'
- )
- );
- if (self::$excludes === null)
- self::$excludes = YPFramework::getSetting('application.log.exclude', $excludes[YPFramework::inProduction()]);
- return (array_search($type, self::$excludes) !== false);
- }
- }
- ?>