web/wp-content/plugins/social/lib/social/log.php
changeset 196 5e8dcbe22c24
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 /**
       
     3  * Social logger
       
     4  *
       
     5  * @package Social
       
     6  */
       
     7 final class Social_Log {
       
     8 
       
     9 	/**
       
    10 	 * Returns the instance of Social_Log.
       
    11 	 *
       
    12 	 * @static
       
    13 	 * @param  string  $file  file to log to
       
    14 	 * @return Social_Log
       
    15 	 */
       
    16 	public static function factory($file = null) {
       
    17 		if ($file == null) {
       
    18 			$file = Social::$plugins_path.'debug_log.txt';
       
    19 
       
    20 			// Attempt to create the file if it doesn't exist.
       
    21 			if (is_writable(Social::$plugins_path)) {
       
    22 				try {
       
    23 					$fh = fopen($file, 'a');
       
    24 					fclose($fh);
       
    25 				}
       
    26 				catch (Exception $e) {
       
    27 					// Failed to create the file, oh well...
       
    28 					$file = null;
       
    29 				}
       
    30 			}
       
    31 		}
       
    32 		return new Social_Log($file);
       
    33 	}
       
    34 
       
    35 	/**
       
    36 	 * @var  string  log file
       
    37 	 */
       
    38 	private $_file = '';
       
    39 
       
    40 	/**
       
    41 	 * Sets the log pile path.
       
    42 	 *
       
    43 	 * @param  string  $file
       
    44 	 */
       
    45 	public function __construct($file = null) {
       
    46 		$this->_file = $file;
       
    47 	}
       
    48 
       
    49 	/**
       
    50 	 * Add a message to the log.
       
    51 	 *
       
    52 	 * @static
       
    53 	 * @param  string  $message    message to add to the log
       
    54 	 * @param  array   $args       arguments to pass to the writer
       
    55 	 * @param  string  $context    context of the log message
       
    56 	 * @param  bool    $backtrace  show the backtrace
       
    57 	 * @return void
       
    58 	 */
       
    59 	public function write($message, array $args = null, $context = null, $backtrace = false) {
       
    60 		if (!Social::option('debug') and !in_array($context, apply_filters('social_log_contexts', array()))) {
       
    61 			return;
       
    62 		}
       
    63 
       
    64 		if ($args !== null) {
       
    65 			foreach ($args as $key => $value) {
       
    66 				$message = str_replace(':'.$key, $value, $message);
       
    67 			}
       
    68 		}
       
    69 
       
    70 		if ($context !== null) {
       
    71 			$context = '['.strtoupper(str_replace('-', ' ', $context)).'] ';
       
    72 		}
       
    73 
       
    74 		$error_str = $context.'[SOCIAL - '.current_time('mysql', 1).' - '.$_SERVER['REMOTE_ADDR'].'] '.$message;
       
    75 
       
    76 		if ($backtrace) {
       
    77 			ob_start();
       
    78 			debug_print_backtrace();
       
    79 			$trace = ob_get_contents();
       
    80 			ob_end_clean();
       
    81 
       
    82 			$error_str .= "\n\n".$trace."\n\n";
       
    83 		}
       
    84 
       
    85 		if (is_writable($this->_file)) {
       
    86 			error_log($error_str."\n", 3, $this->_file);
       
    87 		}
       
    88 		else {
       
    89 			error_log($error_str);
       
    90 		}
       
    91 	}
       
    92 
       
    93 } // End Social_Log