ObsLog.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. namespace Obs\Log;
  17. use Monolog\Formatter\LineFormatter;
  18. use Monolog\Handler\RotatingFileHandler;
  19. use Monolog\Logger;
  20. class ObsLog extends Logger
  21. {
  22. public static $log = null;
  23. protected $logPath = './';
  24. protected $logName = null;
  25. protected $logLevel = Logger::DEBUG;
  26. protected $logMaxFiles = 0;
  27. private $formatter = null;
  28. private $filepath = '';
  29. public static function initLog($logConfig = [])
  30. {
  31. $s3log = new ObsLog('');
  32. $s3log->setConfig($logConfig);
  33. $s3log->cheakDir();
  34. $s3log->setFilePath();
  35. $s3log->setFormat();
  36. $s3log->setHande();
  37. }
  38. private function setFormat()
  39. {
  40. $output = "[%datetime%][%level_name%]%message%\n";
  41. $this->formatter = new LineFormatter($output);
  42. }
  43. private function setHande()
  44. {
  45. static::$log = new Logger('obs_logger');
  46. $rotating = new RotatingFileHandler($this->filepath, $this->logMaxFiles, $this->logLevel);
  47. $rotating->setFormatter($this->formatter);
  48. static::$log->pushHandler($rotating);
  49. }
  50. private function setConfig($logConfig = [])
  51. {
  52. $arr = empty($logConfig) ? ObsConfig::LOG_FILE_CONFIG : $logConfig;
  53. $this->logPath = iconv('UTF-8', 'GBK', $arr['FilePath']);
  54. $this->logName = iconv('UTF-8', 'GBK', $arr['FileName']);
  55. $this->logMaxFiles = is_numeric($arr['MaxFiles']) ? 0 : intval($arr['MaxFiles']);
  56. $this->logLevel = $arr['Level'];
  57. }
  58. private function cheakDir()
  59. {
  60. if (!is_dir($this->logPath)) {
  61. mkdir($this->logPath, 0755, true);
  62. }
  63. }
  64. private function setFilePath()
  65. {
  66. $this->filepath = $this->logPath . '/' . $this->logName;
  67. }
  68. private static function writeLog($level, $msg)
  69. {
  70. switch ($level) {
  71. case DEBUG:
  72. static::$log->debug($msg);
  73. break;
  74. case INFO:
  75. static::$log->info($msg);
  76. break;
  77. case NOTICE:
  78. static::$log->notice($msg);
  79. break;
  80. case WARNING:
  81. static::$log->warning($msg);
  82. break;
  83. case ERROR:
  84. static::$log->error($msg);
  85. break;
  86. case CRITICAL:
  87. static::$log->critical($msg);
  88. break;
  89. case ALERT:
  90. static::$log->alert($msg);
  91. break;
  92. case EMERGENCY:
  93. static::$log->emergency($msg);
  94. break;
  95. default:
  96. break;
  97. }
  98. }
  99. public static function commonLog($level, $format, $args1 = null, $arg2 = null)
  100. {
  101. if (ObsLog::$log) {
  102. if ($args1 === null && $arg2 === null) {
  103. $msg = urldecode($format);
  104. } else {
  105. $msg = sprintf($format, $args1, $arg2);
  106. }
  107. $back = debug_backtrace();
  108. $line = $back[0]['line'];
  109. $filename = basename($back[0]['file']);
  110. $message = '[' . $filename . ':' . $line . ']: ' . $msg;
  111. ObsLog::writeLog($level, $message);
  112. }
  113. }
  114. }