├── .gitattributes ├── .gitignore ├── MyLogPHP.class.php ├── MyLogPHP.conf ├── README.md └── composer.json /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # ========================= 18 | # Operating System Files 19 | # ========================= 20 | 21 | # OSX 22 | # ========================= 23 | 24 | .DS_Store 25 | .AppleDouble 26 | .LSOverride 27 | 28 | # Icon must end with two \r 29 | Icon 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear on external disk 35 | .Spotlight-V100 36 | .Trashes 37 | 38 | # Directories potentially created on remote AFP share 39 | .AppleDB 40 | .AppleDesktop 41 | Network Trash Folder 42 | Temporary Items 43 | .apdisk 44 | -------------------------------------------------------------------------------- /MyLogPHP.class.php: -------------------------------------------------------------------------------- 1 | 10 | * @copyright 2014 Lawrence Lagerlof 11 | * @link http://github.com/llagerlof/MyLogPHP 12 | * @license http://opensource.org/licenses/BSD-3-Clause New BSD License 13 | */ 14 | 15 | namespace MyLogPHP; 16 | 17 | class MyLogPHP { 18 | 19 | /** 20 | * Name of the file where the message logs will be appended. 21 | * @access private 22 | */ 23 | private $LOGFILENAME; 24 | 25 | /** 26 | * Define the separator for the fields. Default is comma (,). 27 | * @access private 28 | */ 29 | private $SEPARATOR; 30 | 31 | /** 32 | * The first line of the log file. 33 | * @access private 34 | */ 35 | private $HEADERS; 36 | 37 | /** 38 | * To log or not to log, that's the question. 39 | * @access private 40 | */ 41 | private $LOG_ENABLED = null; 42 | 43 | /* @const Default tag. */ 44 | const DEFAULT_TAG = '--'; 45 | 46 | /* @const overwrite file in out() method. */ 47 | const OVERWRITE = null; 48 | 49 | /* @const append to file in out() method. */ 50 | const APPEND = FILE_APPEND; 51 | 52 | /** 53 | * Constructor 54 | * @param string $logfilename Path and name of the file log. 55 | * @param string $separator Character used for separate the field values. 56 | */ 57 | public function __construct($logfilename = './_MyLogPHP-1.2.log.csv', $separator = ',') { 58 | 59 | $this->LOGFILENAME = $logfilename; 60 | $this->SEPARATOR = $separator; 61 | $this->HEADERS = 62 | 'DATETIME' . $this->SEPARATOR . 63 | 'ERRORLEVEL' . $this->SEPARATOR . 64 | 'TAG' . $this->SEPARATOR . 65 | 'VALUE' . $this->SEPARATOR . 66 | 'LINE' . $this->SEPARATOR . 67 | 'FILE'; 68 | } 69 | 70 | /** 71 | * Private method that will write the text messages into the log file. 72 | * 73 | * @param string $errorlevel There are 4 possible levels: INFO, WARNING, DEBUG, ERROR 74 | * @param string $value The value that will be recorded on log file. 75 | * @param string $tag Any possible tag to help the developer to find specific log messages. 76 | */ 77 | private function log($errorlevel = 'INFO', $value = '', $tag) { 78 | 79 | if ($this->enabled()) { 80 | $datetime = date("Y-m-d H:i:s"); 81 | if (!file_exists($this->LOGFILENAME)) { 82 | $headers = $this->HEADERS . "\n"; 83 | } 84 | 85 | $fd = fopen($this->LOGFILENAME, "a"); 86 | 87 | if (!empty($headers)) { 88 | fwrite($fd, $headers); 89 | } 90 | 91 | $debugBacktrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); 92 | $line = $debugBacktrace[1]['line']; 93 | $file = $debugBacktrace[1]['file']; 94 | 95 | $value = preg_replace('/\s+/', ' ', trim($value)); 96 | 97 | $entry = array($datetime,$errorlevel,$tag,$value,$line,$file); 98 | 99 | fputcsv($fd, $entry, $this->SEPARATOR); 100 | 101 | fclose($fd); 102 | } 103 | } 104 | 105 | /** 106 | * Function to write non INFOrmation messages that will be written into $LOGFILENAME. 107 | * 108 | * @param string $value 109 | * @param string $tag 110 | */ 111 | public function info($value = '', $tag = self::DEFAULT_TAG) { 112 | 113 | self::log('INFO', $value, $tag); 114 | } 115 | 116 | /** 117 | * Function to write WARNING messages that will be written into $LOGFILENAME. 118 | * 119 | * Warning messages are for non-fatal errors, so, the script will work properly even 120 | * if WARNING errors appears, but this is a thing that you must ponderate about. 121 | * 122 | * @param string $value 123 | * @param string $tag 124 | */ 125 | public function warning($value = '', $tag = self::DEFAULT_TAG) { 126 | 127 | self::log('WARNING', $value, $tag); 128 | } 129 | 130 | /** 131 | * Function to write ERROR messages that will be written into $LOGFILENAME. 132 | * 133 | * These messages are for fatal errors. Your script will NOT work properly if an ERROR happens, right? 134 | * 135 | * @param string $value 136 | * @param string $tag 137 | */ 138 | public function error($value = '', $tag = self::DEFAULT_TAG) { 139 | 140 | self::log('ERROR', $value, $tag); 141 | } 142 | 143 | /** 144 | * Function to write DEBUG messages that will be written into $LOGFILENAME. 145 | * 146 | * DEBUG messages are for variable values and other technical issues. 147 | * 148 | * @param string $value 149 | * @param string $tag 150 | */ 151 | public function debug($value = '', $tag = self::DEFAULT_TAG) { 152 | 153 | self::log('DEBUG', $value, $tag); 154 | } 155 | 156 | /** 157 | * Function to write the print_r return value to a file. 158 | * 159 | * Allow to append a variable value to the end of the file _OUT_MyLogPHP.txt 160 | * 161 | * @param mixed $variable_to_output 162 | * @param mixed $options 163 | */ 164 | public static function out($variable_to_output, $options = null) { 165 | if ($this->enabled()) { 166 | // $options = array('OUTPUT_PATH' => '', 'WRITE_MODE' => APPEND, 'LABEL' => 'Label'); 167 | // If OUTPUT_PATH is not defined, $_SESSION['MYLOGPHP_OUT_PATH'] is used instead. If the last is not defined too, current directory is used. 168 | if (empty($options)) { 169 | $write_mode = FILE_APPEND; 170 | $output_path = (!empty($_SESSION['MYLOGPHP_OUT_PATH'])) ? $_SESSION['MYLOGPHP_OUT_PATH'] : ''; 171 | $label = null; 172 | } elseif (is_string($options)) { 173 | $write_mode = FILE_APPEND; 174 | $output_path = (!empty($_SESSION['MYLOGPHP_OUT_PATH'])) ? $_SESSION['MYLOGPHP_OUT_PATH'] : ''; 175 | $label = $options; 176 | } elseif (is_array($options)) { 177 | $output_path = array_key_exists('OUTPUT_PATH', $options) ? $options['OUTPUT_PATH'] : ( (!empty($_SESSION['MYLOGPHP_OUT_PATH'])) ? $_SESSION['MYLOGPHP_OUT_PATH'] : '' ); 178 | $write_mode = array_key_exists('WRITE_MODE', $options) ? $options['WRITE_MODE'] : FILE_APPEND; 179 | if ($write_mode !== FILE_APPEND) { 180 | $write_mode = null; 181 | } 182 | $label = array_key_exists('LABEL', $options) ? $options['LABEL'] : null; 183 | } 184 | 185 | $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); 186 | $backtrace_text = print_r($backtrace, true); 187 | $filename_full = $backtrace[0]['file']; 188 | $line_call = ' (' . $backtrace[0]['line'] . ')'; 189 | $method_call = (!empty($backtrace[1]['function'])) ? $backtrace[1]['function'] . '() :: ' : ''; 190 | $arr_file_name = explode('(', basename($filename_full)); 191 | $file_name = str_repeat(' ', 80 - strlen($method_call . $arr_file_name[0] . $line_call)) . $method_call . $arr_file_name[0] . $line_call; 192 | 193 | $print_r_variable_to_output = print_r($variable_to_output, true) . "\n\n" . str_repeat('-', 80) . "\n"; 194 | 195 | $variable_type = gettype($variable_to_output); 196 | 197 | $datetime = date("H:i:s Y-m-d"); 198 | 199 | if (!empty($label)) { 200 | $datetime_span_position = 80 - strlen($datetime); 201 | $print_r_variable_to_output = $file_name . "\n" . str_repeat(' ', $datetime_span_position) . $datetime . "\n" . '[' . $label . '] ' . $variable_type . "\n\n" . $print_r_variable_to_output; 202 | } else { 203 | $datetime_span_position = 80 - strlen($datetime); 204 | $print_r_variable_to_output = $file_name . "\n" . $datetime . "\n" . $variable_type . "\n\n" . $print_r_variable_to_output; 205 | } 206 | 207 | file_put_contents($output_path . '_OUT_MyLogPHP.txt', $print_r_variable_to_output, $write_mode); 208 | } 209 | } 210 | 211 | private function enabled() { 212 | if (!is_null($this->LOG_ENABLED)) { 213 | 214 | return $this->LOG_ENABLED; 215 | } 216 | 217 | if (file_exists(dirname(__FILE__) . '/MyLogPHP.conf')) { 218 | $contents = file(dirname(__FILE__) . '/MyLogPHP.conf'); 219 | foreach ($contents as $line) { 220 | $config_value = explode('=', $line); 221 | if (($config_value[0] == "enabled") && (trim($config_value[1]) == "false")) { 222 | $this->LOG_ENABLED = false; 223 | 224 | return false; 225 | } 226 | } 227 | } 228 | 229 | $this->LOG_ENABLED = true; 230 | 231 | return true; 232 | } 233 | } 234 | ?> -------------------------------------------------------------------------------- /MyLogPHP.conf: -------------------------------------------------------------------------------- 1 | enabled=true -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |