log_message.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/9/19 0019
  6. * Time: 下午 1:54
  7. */
  8. class log_message
  9. {
  10. public static function info()
  11. {
  12. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
  13. $file = str_replace(LOG_SERVER_URL, '', $trace[0]['file']);
  14. $line = $trace[0]['line'];
  15. if (isset($trace[1]['function'])) {
  16. $function = $trace[1]['function'];
  17. } else {
  18. $function = 'unknow';
  19. }
  20. $message = date(DATE_FORMAT_S, time()) . '[' . $file . ": line: " . $line . ' funtion: ' . $function . ']';
  21. foreach (func_get_args() as $item) {
  22. //$message .= self::toString($item) . ' ';
  23. }
  24. $classname_file = basename($file, '.php') . '.txt';
  25. $file_name = LOG_SERVER_URL . date(DATE_FORMAT_D, time()) . '_' . $classname_file;
  26. if (!$file_name) {
  27. return -1;
  28. }
  29. if (self::isCli()) {
  30. self::echoLine('[INFO]' . date(DATE_FORMAT_S, time()) . ' ' . $message);
  31. } else {
  32. if (error_log($message . "\n", 3, $file_name) == FALSE) {
  33. return -1;
  34. }
  35. }
  36. }
  37. /**
  38. * 是不是简单基础类型(null, boolean , string, numeric)
  39. * @param $object
  40. * @return bool
  41. */
  42. public static function isPrimary($object)
  43. {
  44. return is_null($object) || is_bool($object) || is_string($object) || is_numeric($object);
  45. }
  46. public static function toString($object)
  47. {
  48. if (self::isPrimary($object)) {
  49. return $object;
  50. }
  51. if (is_array($object)) {
  52. return json_encode($object, JSON_UNESCAPED_UNICODE);
  53. }
  54. if (method_exists($object, '__toString')) {
  55. return get_class($object) . '@{' . $object->__toString() . '}';
  56. }
  57. $reflection_object = new ReflectionObject($object);
  58. $properties = $reflection_object->getProperties();
  59. $values = array();
  60. foreach ($properties as $property) {
  61. if ($property->isStatic() || !$property->isPublic()) {
  62. continue;
  63. }
  64. $name = $property->getName();
  65. $value = $property->getValue($object);
  66. if (isPrimary($value)) {
  67. $values[$name] = $value;
  68. } elseif (is_array($value)) {
  69. $values[$name] = json_encode($value, JSON_UNESCAPED_UNICODE);
  70. } else {
  71. $values[$name] = '@' . get_class($value);
  72. }
  73. }
  74. return get_class($object) . '@{' . json_encode($values, JSON_UNESCAPED_UNICODE) . '}';
  75. }
  76. public static function errorInfo()
  77. {
  78. $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
  79. $file = str_replace(SERVER_URL, '', $trace[0]['file']);
  80. $line = $trace[0]['line'];
  81. if (isset($trace[1]['function'])) {
  82. $function = $trace[1]['function'];
  83. } else {
  84. $function = 'unknow';
  85. }
  86. $message = '[' . $file . ":" . $line . ' ' . $function . ']';
  87. foreach (func_get_args() as $item) {
  88. $message .= toString($item) . ' ';
  89. }
  90. return $message;
  91. }
  92. /**
  93. * 方便调试输出
  94. */
  95. public function echoLine()
  96. {
  97. $message = '';
  98. foreach (func_get_args() as $item) {
  99. $message .= self::toString($item) . ' ';
  100. }
  101. $text = '[PID ' . getmypid() . ']' . $message;
  102. if (self::isCli()) {
  103. echo $text . PHP_EOL;
  104. } else {
  105. echo $text . '<br/>';
  106. }
  107. }
  108. public static function isCli()
  109. {
  110. return php_sapi_name() == 'cli';
  111. }
  112. }