Colorea los logs de Symfony
Si eres uno de esos desarrolladores a los que nos gusta hacer un "tail -f" y ver los logs de tus aplicaciones vía shell, esta clase te puede ayudar con tus logs de Symfony.
Se trata de una clase con un par de métodos estáticos que colorearán los mensajes que vayas a añadir al log en función de su tipo o severidad. Está implementado de una manera muy guarrilla funcional y seguro que se puede modificar fácilmente para extender Symfony como dios manda usando un poco de herencia.
No la he probado aun con Symfony 1.3 ó 1.4, pero no creo que falle y si os causa algún problema solo hay que corregir una línea (teóricamente), por lo que lo publico tal cual. La única pega que tiene esta clase es que ensuciará un poco los logs cuando los veáis a través del debug web ya que veréis los códigos ANSI. Como he dicho antes, esta clase es sobre todo para los que inspeccionamos los logs en shell.
Logger.class.php:
/** * This class will use Symfony's logging system adding some ANSI color codes * depending on the severity/type of the message. The goal is to provide a quick * visual recognition of log lines on shell. * * TODO: * Actually do some inheritance here and extend Symonfy intself * * WARNING: * By using this class you will find some weird codes in the web debug interface. * * @author Guillermo Gutiérrez guille@nianoniano.com */ class Logger { // These are the log levels defined in Symfony const EMERG = 0; // System is unusable const ALERT = 1; // Immediate action required const CRIT = 2; // Critical conditions const ERR = 3; // Error conditions const WARNING = 4; // Warning conditions const NOTICE = 5; // Normal but significant const INFO = 6; // Informational const DEBUG = 7; // Debug-level messages const ANSI_END = "\033[0m"; /** * Adds a message to the log * * @param string $msg The message to be added * @param string $module The module that is producing this message. Default: _DEBUG_ * @param string $severity The severity of the message. Must be one of the class constants */ public static function log($msg, $module = '_DEBUG_', $severity = self::DEBUG) { // I'm sure that there is a more elegant way to do this if (!in_array($severity, array( self::EMERG, self::ALERT, self::CRIT, self::ERR, self::WARNING, self::NOTICE, self::INFO, self::DEBUG ))) { throw new Exception(__METHOD__ . " requires a valid severity code"); } switch ($severity) { case self::EMERG: case self::ALERT: $style = "\033[0;49;31;1m"; break; case self::CRIT: case self::ERR: $style = "\033[0;49;31m"; break; case self::WARNING: $style = "\033[0;49;33;1m"; break; case self::NOTICE: case self::INFO: $style = "\033[0;49;32m"; break; case self::DEBUG: $style = "\033[0;49;36m"; break; } $msg = $style."{".$module."} ".$msg.self::ANSI_END; try { sfContext::getInstance()->getLogger()->log($msg, $severity); } catch (Exception $e) { // Replace this with something else in production environments! echo $msg."\n"; } } /** * This method is a shortcut for self::log(print_r($someArray, true), $module, $severity); * @param Array $array Array to be logged * @param string $module The module that is producing this message. Default: _DEBUG_ * @param string $severity The severity of the message. Must be one of the class constants */ public static function printR($array, $module = '_DEBUG_', $severity = self::DEBUG) { self::log(print_r($array, true), $module, $severity); } }
¿Te gustó este artículo?
Aún no hay trackbacks.
