Log.class.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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)) $metafunction($log);
  49. $log->label = json_encode($log->label);
  50. $log->save();
  51. }
  52. public static function clear($delay = 1){
  53. $treshold = time() - ($delay * 86400);
  54. self::delete(array('created:<'=>$treshold));
  55. }
  56. //Fonction permettant un benchmark simple et précis.
  57. public static function benchmark($title = ""){
  58. global $lastBench;
  59. $start = microtime(true);
  60. $benfile = __DIR__.SLASH.'..'.SLASH.'benchmark.debug.log';
  61. $bt = debug_backtrace();
  62. $newBench = array(
  63. 'time' => microtime(true),
  64. 'file' => str_replace(__ROOT__,'',$bt[0]['file']),
  65. 'line' => $bt[0]['line'],
  66. );
  67. $label = '';
  68. if(!isset($lastBench)){
  69. $label .= PHP_EOL.PHP_EOL.'['.($title=="" ? "BENCHMARK" : $title ).'] - Début requete > benchmark : '. number_format(microtime(true)-$_SERVER["REQUEST_TIME_FLOAT"],4).' ms'.PHP_EOL;
  70. $label .= PHP_EOL."\t".'=> '.$newBench['file'].PHP_EOL.PHP_EOL;
  71. } else {
  72. if($lastBench['file']!= $newBench['file']) $label .= PHP_EOL."\t".'=> '.$newBench['file'].PHP_EOL;
  73. $time = $newBench['time'] - $lastBench['time'];
  74. $title.= ' L.'.$lastBench['line'].' > L.'.$newBench['line'].' '.$title;
  75. if($time>0.500) $title.=' <!> LENTEUR !!';
  76. $label .= "\t\t".number_format($time,4).' ms | '.$title.PHP_EOL;
  77. }
  78. file_put_contents($benfile, $label,FILE_APPEND);
  79. //on enleve au benchmark le temps d'execution de la fonciton de benchmark elle meme
  80. $newBench['time'] = $newBench['time'] - ( microtime(true) - $start);
  81. $lastBench = $newBench;
  82. }
  83. }