Log.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * Log selected action in database with ip, datetime and optional logged user.
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license copyright
  7. */
  8. class Log extends Entity
  9. {
  10. public $id,$label,$category,$ip;
  11. protected $fields =
  12. array(
  13. 'id' => 'key',
  14. 'label' => 'longstring',
  15. 'category' => 'string',
  16. 'ip' => 'string',
  17. );
  18. public function label(){
  19. return preg_replace_callback('|^\[([^\]]*)\](.*)|i', function($matches){
  20. return '<span class="badge badge-info">'.$matches[1].'</span>'.$matches[2];
  21. }, $this->label);
  22. }
  23. public static function put($label,$category = 'Général') {
  24. global $myUser;
  25. $log = new self();
  26. $log->label = $label;
  27. $log->category = $category;
  28. $log->ip = ip();
  29. $log->save();
  30. }
  31. //Compare deux instances d'une même entité et log
  32. //les differences entre les deux dans un format JSON
  33. public static function compare($old,$new = null,$metafunction=null){
  34. global $myUser;
  35. $log = new self();
  36. $log->label = array();
  37. if(!isset($new) || !isset($old->id) || $old->id==0){
  38. $log->label['action'] = 'create';
  39. $log->label['fields'] = is_object($new) ? $new->toArray(): $old->toArray();
  40. }else if (!$new){
  41. $log->label['action'] = 'delete';
  42. $log->label['fields'] = $old->toArray();
  43. }elseif (is_object($new)){
  44. $log->label['action'] = 'update';
  45. $log->label['fields'] = Entity::compare($old,$new);
  46. }
  47. $log->label['entity'] = $old->tableName();
  48. $log->category = $log->label['entity'].'::compare';
  49. $log->ip = ip();
  50. if(isset($metafunction))
  51. $metafunction($log);
  52. $log->label = json_encode($log->label);
  53. $log->save();
  54. }
  55. public static function clear($delay = 1){
  56. $treshold = time() - ($delay * 86400);
  57. self::delete(array('created:<'=>$treshold));
  58. }
  59. //Fonction permettant un benchmark simple et précis.
  60. public static function benchmark($title = ""){
  61. global $lastBench;
  62. $start = microtime(true);
  63. $benfile = __DIR__.SLASH.'..'.SLASH.'benchmark.debug.log';
  64. $bt = debug_backtrace();
  65. $newBench = array(
  66. 'time' => microtime(true),
  67. 'file' => str_replace(__ROOT__,'',$bt[0]['file']),
  68. 'line' => $bt[0]['line'],
  69. );
  70. $label = '';
  71. if(!isset($lastBench)){
  72. $label .= PHP_EOL.PHP_EOL.'['.($title=="" ? "BENCHMARK" : $title ).'] - Début requete > benchmark : '. number_format(microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"],4).' ms'.PHP_EOL;
  73. $label .= PHP_EOL."\t".'=> '.$newBench['file'].PHP_EOL.PHP_EOL;
  74. }else{
  75. if($lastBench['file']!= $newBench['file']) $label .= PHP_EOL."\t".'=> '.$newBench['file'].PHP_EOL;
  76. $time = $newBench['time'] - $lastBench['time'];
  77. $title.= ' L.'.$lastBench['line'].' > L.'.$newBench['line'].' '.$title;
  78. if($time>0.500) $title.=' <!> LENTEUR !!';
  79. $label .= "\t\t".number_format($time,4).' ms | '.$title.PHP_EOL;
  80. }
  81. file_put_contents($benfile, $label,FILE_APPEND);
  82. //on enleve au benchmark le temps d'execution de la fonciton de benchmark elle meme
  83. $newBench['time'] = $newBench['time'] - ( microtime(true) - $start);
  84. $lastBench = $newBench;
  85. }
  86. }