ObsLog.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 $log_path = './';
  24. protected $log_name = null;
  25. protected $log_level = Logger::DEBUG;
  26. protected $log_maxFiles = 0;
  27. private $formatter = null;
  28. private $handler = null;
  29. private $filepath = '';
  30. public static function initLog($logConfig= [])
  31. {
  32. $s3log = new ObsLog('');
  33. $s3log->setConfig($logConfig);
  34. $s3log->cheakDir();
  35. $s3log->setFilePath();
  36. $s3log->setFormat();
  37. $s3log->setHande();
  38. }
  39. private function setFormat()
  40. {
  41. $output = '[%datetime%][%level_name%]'.'%message%' . "\n";
  42. $this->formatter = new LineFormatter($output);
  43. }
  44. private function setHande()
  45. {
  46. self::$log = new Logger('obs_logger');
  47. $rotating = new RotatingFileHandler($this->filepath, $this->log_maxFiles, $this->log_level);
  48. $rotating->setFormatter($this->formatter);
  49. self::$log->pushHandler($rotating);
  50. }
  51. private function setConfig($logConfig= [])
  52. {
  53. $arr = empty($logConfig) ? ObsConfig::LOG_FILE_CONFIG : $logConfig;
  54. $this->log_path = iconv('UTF-8', 'GBK',$arr['FilePath']);
  55. $this->log_name = iconv('UTF-8', 'GBK',$arr['FileName']);
  56. $this->log_maxFiles = is_numeric($arr['MaxFiles']) ? 0 : intval($arr['MaxFiles']);
  57. $this->log_level = $arr['Level'];
  58. }
  59. private function cheakDir()
  60. {
  61. if (!is_dir($this->log_path)){
  62. mkdir($this->log_path, 0755, true);
  63. }
  64. }
  65. private function setFilePath()
  66. {
  67. $this->filepath = $this->log_path.'/'.$this->log_name;
  68. }
  69. private static function writeLog($level, $msg)
  70. {
  71. switch ($level) {
  72. case DEBUG:
  73. self::$log->debug($msg);
  74. break;
  75. case INFO:
  76. self::$log->info($msg);
  77. break;
  78. case NOTICE:
  79. self::$log->notice($msg);
  80. break;
  81. case WARNING:
  82. self::$log->warning($msg);
  83. break;
  84. case ERROR:
  85. self::$log->error($msg);
  86. break;
  87. case CRITICAL:
  88. self::$log->critical($msg);
  89. break;
  90. case ALERT:
  91. self::$log->alert($msg);
  92. break;
  93. case EMERGENCY:
  94. self::$log->emergency($msg);
  95. break;
  96. default:
  97. break;
  98. }
  99. }
  100. public static function commonLog($level, $format, $args1 = null, $arg2 = null)
  101. {
  102. if(ObsLog::$log){
  103. if ($args1 === null && $arg2 === null) {
  104. $msg = urldecode($format);
  105. } else {
  106. $msg = sprintf($format, $args1, $arg2);
  107. }
  108. $back = debug_backtrace();
  109. $line = $back[0]['line'];
  110. $funcname = $back[1]['function'];
  111. $filename = basename($back[0]['file']);
  112. $message = '['.$filename.':'.$line.']: '.$msg;
  113. ObsLog::writeLog($level, $message);
  114. }
  115. }
  116. }