Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\libs;
- /*
- log::start("testing");
- log::error("device:add:","wrong device ID");
- log::stop();
- */
- class log
- {
- static $dir = "/opt/pulsa/logs/";
- static $suffix = ".log.json";
- static $name = null;
- static $info = [];
- static $logs = null;
- static $times =
- [
- "run_at" => null,
- "benchmark_checkpoint" => [],
- "benchmark_result" => null
- ];
- static $last = null;
- static $pipe = ["state"=>null,"message"=>null,"return"=>[],"debug_backtrace"=>null];
- static function start($logname=null)
- {
- if (self::$times["run_at"]==null) {
- define('FATAL', 'FATAL');
- define('INFO', 'INFO');
- define('ERROR', 'ERROR');
- define('WARN', 'WARN');
- define('TRACE', 'TRACE');
- self::$name = $logname;
- $time = time();
- self::$times["run_at"] = date("Y-m-d H:i:s",$time);
- self::$times["benchmark_checkpoint"]["START"] = self::microtime_float();
- return true;
- }
- else return false;
- }
- static function stop()
- {
- if (self::$times["run_at"]!=null) {
- self::$times["benchmark_checkpoint"]["STOP"] = self::microtime_float();
- self::$times["benchmark_result"] =
- self::$times["benchmark_checkpoint"]["STOP"]-
- self::$times["benchmark_checkpoint"]["START"];
- self::createfile();
- return true;
- }
- else return false;
- }
- static function microtime_float($value='')
- {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
- static function setinfo($info=[])
- {
- self::$info = $info;
- }
- static function set($logname=null,$state=null,$message=null,$return=null,$debug_backtrace=true)
- {
- if ($logname != null) {
- $states = [
- FATAL,
- INFO,
- ERROR,
- WARN,
- TRACE,
- ];
- $ok = false;
- foreach ($states as $value) {
- if ($state == $value) {
- $ok = true;
- break;
- }
- }
- if ($ok == false)
- $state = INFO;
- $modal = self::$pipe;
- $modal["state"] = $state;
- $modal["message"] = $message;
- $modal["return"] = $return;
- $modal["debug_backtrace"] = $debug_backtrace==false?false:debug_backtrace();
- self::$logs[$logname][]=$modal;
- self::$times["benchmark_checkpoint"][$logname][]=self::microtime_float();
- self::$last=$logname;
- return true;
- }
- else return false;
- }
- static function getall($logname=null)
- {
- if (isset(self::$logs[$logname]))
- $return = self::$logs[$logname];
- else
- $return[0] = self::$pipe;
- return $return;
- }
- static function get($logname=null)
- {
- $return = self::$pipe;
- if ($logname==null)
- $logname = self::$last;
- if (isset(self::$logs[$logname])) {
- end(self::$logs[$logname]);
- $key = key(self::$logs[$logname]);
- if (isset(self::$logs[$logname][$key]))
- $return = self::$logs[$logname][$key];
- }
- return $return;
- }
- static function createfile()
- {
- $value =
- [
- "name" => self::$name,
- "info" => self::$info,
- "times" => self::$times,
- "logs" => self::$logs
- ];
- $value = json_encode($value,JSON_PRETTY_PRINT);
- $return = false;
- for ($i=0; $i < 100; $i++) {
- $newfile = self::$dir . self::$times["run_at"] .".". $i.".".self::$name. self::$suffix;
- if (!file_exists($newfile)) {
- touch($newfile);
- file_put_contents($newfile, $value);
- $return = true;
- break;
- }
- }
- return $return;
- }
- static function fatal($logname=null,$message=null,$return=null,$debug_backtrace=true)
- {
- return self::set($logname,FATAL,$message,$return,$debug_backtrace);
- }
- static function info($logname=null,$message=null,$return=null,$debug_backtrace=true)
- {
- return self::set($logname,INFO,$message,$return,$debug_backtrace);
- }
- static function error($logname=null,$message=null,$return=null,$debug_backtrace=true)
- {
- return self::set($logname,ERROR,$message,$return,$debug_backtrace);
- }
- static function warn($logname=null,$message=null,$return=null,$debug_backtrace=true)
- {
- return self::set($logname,WARN,$message,$return,$debug_backtrace);
- }
- static function trace($logname=null,$message=null,$return=null,$debug_backtrace=true)
- {
- return self::set($logname,TRACE,$message,$return,$debug_backtrace);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement