Online PHP editor is going to be changed...

We are going to:

  1. Redesign the site
  2. Work with SSH instead of FTP
  3. Switch to HTTPS
  4. Work without "session" to improve security
  5. And, of course, remain open source

Please consider...

  1. Checking our new PHP blog
  2. Visit the new Facebook page


I'm using SSH and I will continue using online-php.com

<? Online-PHP::Tutorials ?>

« Back

  • Logger class - Manage logs in your applications

     

    Hello All,

    I want to introduce a basic logger class with dynamic logging levels. All the functions are static, so in this case the class has the same role as a namespace. There's no need to create objects for the logger class (some kind of singleton design pattern implenentation).

    The number of arguments each function can receive is unlimited. You can use the function as "logger::info('a=', $a, ', b=', $b);"

    There is an option to limit the output only to a file or nonly to stdout (or both)

    The code was submitted also to `pastebin` - here is the link to my php logger class

    /**

    Logger class - add messages to log.

    Usage

         logger::error('some error message');

         logger::notice('some notice message');

    **/

    class logger {

        

         const OUTPUT_DISABLED   = 0; // 00

         const OUTPUT_TO_FILE    = 1; // 01

         const OUTPUT_TO_SCREEN = 2; // 10

        

         //

         // define dynamic functions call (like 'fatal', 'error', 'info', etc).

         //        logger::error('some error messages'); // the message will be added to 'error's array

         //

         // to get all the messages (array), just call the function without parameters:

         //        $fatal_messages_array = logger::error();

         //

         public static function __callStatic($func, $args) {

              if (empty($args)) {

                  return array_key_exists($func, self::$messages) ? self::$messages[$func] : array();

              } else {

                  self::add_message($func, implode($args));

              }

         }

        

         // possibility to set the output to a file

         public static function set_output($output_type, $filename = null) {

              self::$output_destination = $output_type;

              self::$filename = $filename;

         }

        

         //**--------- PROTECTED -----------**//

        

         // logger output properties

         protected static $filename = null;

         protected static $output_destination = self::OUTPUT_TO_SCREEN;

        

         // logger messages

         protected static $messages = array();

         protected static function add_message($type, $msg) {

              $type = strtolower($type);

        

              // check the messages array for the type

              if (!array_key_exists($type, self::$messages)) {

                  self::$messages[$type] = array();

              }

     

              // add the message to it's type

              self::$messages[$type][] = $msg;

              // output the message to a file / screen / both

              self::_output_message($type, $msg);

         }

        

         // write message to file in the next format:

         // [date] ([type]): [message]

         protected static function _output_message($type, $msg) {

              // generate the log message

              $log_message = self::_get_log_message($type, $msg);

        

              // check that the file name was defined, and there is a permission to write to file

              if (!is_null(self::$filename) && (self::$output_destination & self::OUTPUT_TO_FILE)) {

                  @file_put_contents(self::$filename, $log_message, FILE_APPEND);

              }

             

              // check the permission to output to the screen (stdout)

              if (self::$output_destination & self::OUTPUT_TO_SCREEN) {

                  echo $log_message, '<br/>';

              }

         }

        

         protected static function _get_log_message($type, $msg) {

              return date('d.m.Y H:i:s').' ('.$type.'): '.$msg."\r\n";

         }

    }

     

    Enjoy,
    Gregory.