Log.class.php 3.6 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 MIT
  7. */
  8. class Log extends Entity {
  9. public $id,$label,$category,$ip;
  10. public $entityLabel = 'Journaux';
  11. protected $fields =
  12. array(
  13. 'id' => array('label'=>'Identifiant', 'type'=>'key'),
  14. 'label' => array('label'=>'Libellé', 'type'=>'wysiwyg'),
  15. 'category' => array('label'=>'Catégorie', 'type'=>'text'),
  16. 'ip' => array('label'=>'Ip publique', 'type'=>'text')
  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. $log = new self();
  25. $log->label = $label;
  26. $log->category = $category;
  27. $log->ip = ip();
  28. $log->save();
  29. }
  30. //Compare deux instances d'une même entité et log
  31. //les differences entre les deux dans un format JSON
  32. public static function compare($old,$new = null,$metafunction=null){
  33. $log = new self();
  34. $log->label = array();
  35. if(!isset($new) || !isset($old->id) || $old->id==0){
  36. $log->label['action'] = 'create';
  37. $log->label['fields'] = is_object($new) ? $new->toArray(): $old->toArray();
  38. }else if (!$new){
  39. $log->label['action'] = 'delete';
  40. $log->label['fields'] = $old->toArray();
  41. }elseif (is_object($new)){
  42. $log->label['action'] = 'update';
  43. $log->label['fields'] = Entity::compare($old,$new);
  44. }
  45. $log->label['entity'] = $old->tableName();
  46. $log->category = $log->label['entity'].'::compare';
  47. $log->ip = ip();
  48. if(isset($metafunction))
  49. $metafunction($log);
  50. $log->label = json_encode($log->label);
  51. $log->save();
  52. }
  53. public static function clear($delay = 1){
  54. $treshold = time() - ($delay * 86400);
  55. self::delete(array('created:<'=>$treshold));
  56. }
  57. //Fonction permettant un benchmark simple et précis.
  58. public static function benchmark($title = ""){
  59. global $lastBench;
  60. $start = microtime(true);
  61. $benfile = __DIR__.SLASH.'..'.SLASH.'benchmark.debug.log';
  62. $bt = debug_backtrace();
  63. $newBench = array(
  64. 'time' => microtime(true),
  65. 'file' => str_replace(__ROOT__,'',$bt[0]['file']),
  66. 'line' => $bt[0]['line'],
  67. );
  68. $label = '';
  69. if(!isset($lastBench)){
  70. $label .= PHP_EOL.PHP_EOL.'['.($title=="" ? "BENCHMARK" : $title ).'] - Début requete > benchmark : '. number_format(microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"],4).' ms'.PHP_EOL;
  71. $label .= PHP_EOL."\t".'=> '.$newBench['file'].PHP_EOL.PHP_EOL;
  72. }else{
  73. if($lastBench['file']!= $newBench['file']) $label .= PHP_EOL."\t".'=> '.$newBench['file'].PHP_EOL;
  74. $time = $newBench['time'] - $lastBench['time'];
  75. $title.= ' L.'.$lastBench['line'].' > L.'.$newBench['line'].' '.$title;
  76. if($time>0.500) $title.=' <!> LENTEUR !!';
  77. $label .= "\t\t".number_format($time,4).' ms | '.$title.PHP_EOL;
  78. }
  79. file_put_contents($benfile, $label,FILE_APPEND);
  80. //on enleve au benchmark le temps d'execution de la fonciton de benchmark elle meme
  81. $newBench['time'] = $newBench['time'] - ( microtime(true) - $start);
  82. $lastBench = $newBench;
  83. }
  84. }