Posts Tagged logging

Simple Logging Utility Class for Symfony

Here’s a little gem I’ve been using for quite some time. Sometimes during your development process you simply want to see the contents of a variable while it’s being used or want to know when a certain bit of code is firing. In my PHP programming infancy I would simply dump such data on screen with functions like echo and print_r. This was an ugly and brutish way of doing it.

Since one of the nice features Symfony sports is it’s logging features, I started dumping such data to the log. I quickly found out that depending on what part of the code I was working on, the syntax of my logging was different and became quite cumbersome in some cases.

In comes the following class, the Logging class, or L class for short.  It simplifies logging and dumping a variable to the log.  I also found it helpful when I was first learning Symfony to know what methods a class had. I’ve also included that function as well.

Here are some examples:

        L::log('Something happened.');
 
        $array = array('1', '2', 'Tom' => 'Jones', 'Richard' => 'Smith',
            'Harrold' => 'Jackson');
        L::dump($array, 'Example Array', 'info');
 
        $config = new sfConfig();
        L::methods($config);

Output:

Jul DD HH:MM:SS symfony [debug]
***********************
* Something happened. *
***********************

Jul DD HH:MM:SS symfony [info]
*******************************
* Variable Dump Example Array *
*******************************
array (
 0 => '1',
 1 => '2',
 'Tom' => 'Jones',
 'Richard' => 'Smith',
 'Harrold' => 'Jackson',
)
Jul DD HH:MM:SS symfony [debug]
************************
* Methods for sfConfig *
************************
sfConfig::get
sfConfig::has
sfConfig::set
sfConfig::add
sfConfig::getAll
sfConfig::clear

Here is the code. Simply drop it into a file in your project’s lib directory (I placed it into a util subdirectory) and clear your cache to start using it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
 * Pretty Simple Logger
 *
 * @package util
 * @author Phazeon Phoenix <phoenix@phazeon.com>
 * @copyright 2009 Phazeon.com
 * @version 1
 */
class L {
 
    static public $allowedMessageLevels = array('notice', 'emerg', 'alert',
                         'crit', 'info', 'debug', 'err', 'warning');
    /**
     * Log a message.
     *
     * @param mixed $message
     * @param string $level
     * @param boolean $pretty Does not add the star header if false.
     * @return void
     */
    static public function log($message, $level = 'debug', $pretty = true)
    {
        if (sfConfig::get('sf_logging_enabled')) {
            if ( ! in_array($level, self::$allowedMessageLevels))
                $level = 'debug';
            if ($pretty)
                $output = self::prettyHeader($message);
            else
                $output = $message;
            sfContext::getInstance()->getLogger()->$level($output);
        }
    }
 
    /**
     * Dump a variable to the log.
     *
     * Known Limitation: Variables that are too large, such as many of the
     * Symfony core classes, will cause this function to fail. This is a
     * limitation of var_export(). If you need to dump such a variable, use
     * print_r(). It's ugly but it'll work no matter how large the variable is.
     *
     * @param mixed $var
     * @param mixed $message
     * @param string $level
     * @return void
     */
    static public function dump($var, $message = null,  $level = 'debug')
    {
        if (sfConfig::get('sf_logging_enabled')) {
            $output = self::prettyHeader("Variable Dump $message");
            $output .= var_export($var, true);
            L::log($output, $level, false);
        }
    }
 
    /**
     * Output all publicly accessable methods of a class.
     *
     * @param mixed $class
     * @param mixed $message
     * @param string $level
     * @return void
     */
    static public function methods($class, $message = null,  $level = 'debug')
    {
        if (sfConfig::get('sf_logging_enabled')) {
            $class = get_class($class);
            $output = self::prettyHeader("Methods for $class $message");
            $methods = get_class_methods($class);
            foreach($methods as $method) {
                $output .= "$class::$method\n";
            }
            L::log($output, $level, false);
        }
    }
 
    /**
     * Build a string padded and surrounded by stars.
     *
     * @param mixed $text
     * @return string
     */
    static public function prettyHeader($text) {
        $stars = '';
        $text = "* " . trim($text) . " *";
        $stars = str_repeat('*', strlen($text));
        return "\n" . $stars . "\n" . $text . "\n" . $stars . "\n";
    }
}

, , ,

No Comments