You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

logger.py 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. import io
  14. import logging
  15. import os
  16. import sys
  17. import config as configs
  18. from datetime import datetime
  19. from logging import handlers
  20. sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8')
  21. parser = configs.get_parser()
  22. args = parser.parse_args()
  23. class Logger(object):
  24. """
  25. 日志配置类,定义日志格式及保存
  26. """
  27. level_mapping = {
  28. 'debug': logging.DEBUG,
  29. 'info': logging.INFO,
  30. 'warning': logging.WARNING,
  31. 'error': logging.ERROR,
  32. 'critical': logging.CRITICAL
  33. }
  34. def __init__(self):
  35. if 'logs' not in os.listdir():
  36. os.mkdir('logs')
  37. date = datetime.now().strftime('%Y-%m-%d')
  38. file_name = 'logs/' + 'serving.log.' + date
  39. self.logger = logging.getLogger('serving')
  40. if not self.logger.handlers:
  41. formatter = logging.Formatter('%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s')
  42. self.logger.setLevel(self.level_mapping.get(args.level)) # 设置日志级别
  43. self.logger.propagate = False
  44. console_log = logging.StreamHandler() # 往屏幕上输出
  45. console_log.setFormatter(formatter) # 设置屏幕上显示的格式
  46. file_log = handlers.TimedRotatingFileHandler(filename=file_name, when="D", backupCount=10, encoding='utf-8')
  47. file_log.setFormatter(formatter) # 设置文件里写入的格式
  48. self.logger.addHandler(console_log) # 把对象加到logger里
  49. self.logger.addHandler(file_log)
  50. console_log.close()
  51. file_log.close()

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能