├── .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 |

MyLogPHP

2 | 3 | MyLogPHP is a single PHP class to easily keep log files in CSV format. 4 | 5 |

Install the latest version

6 | 7 |

Direct download (just include this file in your script)

8 | MyLogPHP.class.php (1.2.19) 9 |
10 |

Or you can use Composer

11 | Edit your project's **composer.json** and add the required package: **"llagerlof/mylogphp": "dev-master"** 12 | 13 | Eg: 14 | 15 | { 16 | "name": "your/project", 17 | "require": { 18 | "llagerlof/mylogphp": "dev-master" 19 | } 20 | } 21 |

How to read the generated log file

22 | Any CSV reader can be used to open the generated logs, but If you need a recomendation, try the CSVFileView or LogExpert. Both can parse/show the CSV columns and watch realtime changes in log file (like tail on Linux). 23 | 24 |

Features

25 | 26 | * One single file to rule them all! 27 | * Super easy start. 28 | * Two extra columns are automatically added in the CSV: the number of execution line and the path of host script where the log was called. 29 | * Minimum PHP version: 5.3 30 | 31 |

Want more features? I am listening!

32 | 33 | Create an issue in https://github.com/llagerlof/MyLogPHP/issues/new 34 | 35 |

Quick start

36 | 37 | Include in your script the file "MyLogPHP.class.php" (not needed if you are using the composer version). 38 | 39 | ```php 40 | include('MyLogPHP.class.php'); 41 | ``` 42 | 43 | Instantiate the object. Optionally you can pass the log file name and the separator as a parameter. Default log file name is "`_`MyLogPHP-1.2.log.csv" in current folder, and default separator is comma (,). 44 | 45 | ```php 46 | $log = new MyLogPHP\MyLogPHP('./log/debug.log.csv'); 47 | ``` 48 | 49 | Make sure the directory where the log will be created is writable. 50 | 51 | Call method "info", "warning", "error" or "debug" to write the messages. 52 | The first parameter is the message, the optional second parameter is a free tag at your choice to help you filter the log when opened by an spreadsheet software, like `OpenOffice Calc` or `Microsoft Excel`. 53 | 54 | ```php 55 | $log->info('This message will be logged in the file debug.log.csv','TIP'); 56 | ``` 57 | 58 | That's it! 59 | 60 |

Examples

61 | 62 | ```php 63 | $log = new MyLogPHP\MyLogPHP(); 64 | 65 | $log->info('The program starts here.'); 66 | 67 | $log->warning('This problem can affect the program logic'); 68 | 69 | $log->warning('Use this software at your own risk!'); 70 | 71 | $log->info('Lawrence Lagerlof','AUTHOR'); 72 | 73 | $log->info('Asimov rulez','FACT'); 74 | 75 | $log->error('Everything crash and burn','IE'); 76 | 77 | $log->debug("select * from table",'DB'); 78 | 79 | ``` 80 | 81 | Writing a variable content to file "_OUT_MyLogPHP.txt" 82 | 83 | ``` 84 | \MyLogPHP\MyLogPHP::out($data); 85 | 86 | \MyLogPHP\MyLogPHP::out($data, array('LABEL' => 'variable $data'); 87 | 88 | \MyLogPHP\MyLogPHP::out($data, 'variable $data'); // same as above 89 | 90 | \MyLogPHP\MyLogPHP::out($data, array('LABEL' => 'variable $data', 'WRITE_MODE' => MyLogPHP::OVERWRITE)); 91 | 92 | \MyLogPHP\MyLogPHP::out($data, array('LABEL' => 'variable $data', 'WRITE_MODE' => MyLogPHP::APPEND)); // WRITE_MODE can be OVERWRITE or APPEND (APPEND is default) 93 | 94 | ``` 95 | 96 |

Changelog

97 | 98 | **1.2.19** 99 | * Read the configuration file only once per instance. 100 | 101 | **1.2.18** 102 | * Namespace added. 103 | 104 | **1.2.17** 105 | * Fixed a debug_backtrace problem when used in Laravel application. 106 | 107 | **1.2.16** 108 | * Put an optional config file (MyLogPHP.conf in the same directory of the class) with one line containing "enabled=false" to disable logging. 109 | 110 | **1.2.15** 111 | * Changed output format of out() method. 112 | 113 | **1.2.14** 114 | * Added method name to output of the out() method. 115 | 116 | **1.2.13** 117 | * Added the file and execution line info to output of the out() method. 118 | 119 | **1.2.12** 120 | * You can define a $_SESSION variable to set where the file will be created by the out() method. 121 | 122 | **1.2.11** 123 | * out() method is now static. 124 | 125 | **1.2.10** 126 | * Can pass a string to act as LABEL to the second parameter of out() method. 127 | 128 | **1.2.9** 129 | * Removed supressed warnings from variable $headers and date() functions. 130 | 131 | **1.2.8** 132 | * Add more options and verbosity to out() method. 133 | 134 | **1.2.7** 135 | * New method out() allow append a variable value to the end of the file _OUT_MyLogPHP.txt 136 | 137 | **1.2.6** 138 | * Class updated for PHP 7 (thanks [@clintre](https://github.com/clintre)). 139 | 140 | **1.2.5** 141 | * Solved a bug that was preventing the replacement of consecutives spaces and tabs for one space. 142 | 143 | **1.2.4** 144 | * All consecutives spaces and tabs are removed from output. 145 | 146 | **1.2.3** 147 | * PHPDOC comment style applied to the class. 148 | * An issue with the newer version of PHP and the date() function was solved. 149 | 150 | **1.2.2** 151 | * Line breaks of VALUE field are converted to spaces to prevent some CSV readers wrongly interpret line breaks as new CSV lines. 152 | * The VALUE field now is trimmed before output. 153 | 154 | **1.2.1** 155 | * Disable a warning message if an internal variable is not set. 156 | 157 | **1.2** 158 | * Two columns added in CSV output: LINE and FILE. 159 | 160 | **1.1** 161 | * Added support to choose the field separator. Comma is still the default. 162 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "llagerlof/mylogphp", 3 | "description": "MyLogPHP is a single PHP class to easily keep log files in CSV format.", 4 | "license": "BSD-3-Clause", 5 | "version": "1.2.6", 6 | "homepage": "https://github.com/llagerlof/MyLogPHP", 7 | "keywords": [ 8 | "php", "log", "logger", "logging", "csv" 9 | ], 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/llagerlof/mylogphp.git" 13 | }, 14 | "authors": [ 15 | { 16 | "name": "Lawrence Lagerlof", 17 | "email": "llagerlof@gmail.com", 18 | "homepage": "http://google.com/+LawrenceLagerlof", 19 | "role": "Developer" 20 | } 21 | ], 22 | "autoload": { 23 | "classmap": ["MyLogPHP.class.php"] 24 | } 25 | } 26 | --------------------------------------------------------------------------------