人人商城

logging.func.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. define('LOGGING_ERROR', 'error');
  8. define('LOGGING_TRACE', 'trace');
  9. define('LOGGING_WARNING', 'warning');
  10. define('LOGGING_INFO', 'info');
  11. function logging_run($log, $type = 'trace', $filename = 'run') {
  12. global $_W;
  13. $filename = IA_ROOT . '/data/logs/' . $filename . '_' . date('Ymd') . '.log';
  14. load()->func('file');
  15. mkdirs(dirname($filename));
  16. $logFormat = "%date %type %user %url %context";
  17. if (!empty($GLOBALS['_POST'])) {
  18. $context[] = logging_implode($GLOBALS['_POST']);
  19. }
  20. if (is_array($log)) {
  21. $context[] = logging_implode($log);
  22. } else {
  23. $context[] = preg_replace('/[ \t\r\n]+/', ' ', $log);
  24. }
  25. $log = str_replace(explode(' ', $logFormat), array(
  26. '[' . date('Y-m-d H:i:s', $_W['timestamp']) . ']',
  27. $type,
  28. $_W['username'],
  29. $_SERVER["PHP_SELF"] . "?" . $_SERVER["QUERY_STRING"],
  30. implode("\n", $context),
  31. ), $logFormat);
  32. file_put_contents($filename, $log . "\r\n", FILE_APPEND);
  33. return true;
  34. }
  35. function logging_implode($array, $skip = array()) {
  36. $return = '';
  37. if (is_array($array) && !empty($array)) {
  38. foreach ($array as $key => $value) {
  39. if (empty($skip) || !in_array($key, $skip, true)) {
  40. if (is_array($value)) {
  41. $return .= $key . '={' . logging_implode($value, $skip) . '}; ';
  42. } else {
  43. $return .= "$key=$value; ";
  44. }
  45. }
  46. }
  47. }
  48. return $return;
  49. }