Recently, I was made a service which can provide a simple way to get best model. so, i spent lot of time to read source code of auto-sklearn, auto-sklearn is an automated machine learning toolkit and a drop-in replacement for a scikit-learn estimator.

when I write my logging module I found the logging module of auto-sklearn which used yaml file as config, the config file:

 ---
version: 1
disable_existing_loggers: False
formatters:
simple:
format: '[%(asctime)s: %(levelname)s] %(message)s' handlers:
console:
class: logging.StreamHandler
level: WARNING
formatter: simple
stream: ext://sys.stdout file_handler:
class: logging.FileHandler
level: DEBUG
formatter: simple
filename: autosklearn.log root:
level: ERROR
handlers: [console, file_handler] loggers:
server:
level: INFO
handlers: [file_handler]
propagate: no

the caller can define loggers、handlers、formatters here, after that, caller need write setup code and get logger code.

 # -*- encoding: utf-8 -*-
import logging
import logging.config
import os
import yaml def setup_logger(output_file=None, logging_config=None):
# logging_config must be a dictionary object specifying the configuration
if not os.path.exists(os.path.dirname(output_file)):
os.makedirs(os.path.dirname(output_file))
if logging_config is not None:
if output_file is not None:
logging_config['handlers']['file_handler']['filename'] = output_file
logging.config.dictConfig(logging_config)
else:
with open(os.path.join(os.path.dirname(__file__), 'logging.yaml'),
'r') as fh:
logging_config = yaml.safe_load(fh)
if output_file is not None:
logging_config['handlers']['file_handler']['filename'] = output_file
logging.config.dictConfig(logging_config) def _create_logger(name):
return logging.getLogger(name) def get_logger(name):
logger = PickableLoggerAdapter(name)
return logger class PickableLoggerAdapter(object): def __init__(self, name):
self.name = name
self.logger = _create_logger(name) def __getstate__(self):
"""
Method is called when pickle dumps an object. Returns
-------
Dictionary, representing the object state to be pickled. Ignores
the self.logger field and only returns the logger name.
"""
return {'name': self.name} def __setstate__(self, state):
"""
Method is called when pickle loads an object. Retrieves the name and
creates a logger. Parameters
----------
state - dictionary, containing the logger name. """
self.name = state['name']
self.logger = _create_logger(self.name) def debug(self, msg, *args, **kwargs):
self.logger.debug(msg, *args, **kwargs) def info(self, msg, *args, **kwargs):
self.logger.info(msg, *args, **kwargs) def warning(self, msg, *args, **kwargs):
self.logger.warning(msg, *args, **kwargs) def error(self, msg, *args, **kwargs):
self.logger.error(msg, *args, **kwargs) def exception(self, msg, *args, **kwargs):
self.logger.exception(msg, *args, **kwargs) def critical(self, msg, *args, **kwargs):
self.logger.critical(msg, *args, **kwargs) def log(self, level, msg, *args, **kwargs):
self.logger.log(level, msg, *args, **kwargs) def isEnabledFor(self, level):
return self.logger.isEnabledFor(level)

get_logger return a logger object, setup_logger setup logger handler which define output where.

when you use it,like this

 from .util import get_logger, setup_logger

 APP_DIR = os.path.dirname(os.path.abspath(__file__))
setup_logger(output_file=os.path.join(APP_DIR, 'logs', 'server.log'))
LOG = get_logger("server")

Define a global variable LOG for other modules to call.

all done.

最新文章

  1. document封装一些常用的方法
  2. Spring
  3. QImage::drawRect 和 fillRect在处理大面积区域时代价高昂
  4. Oracle诡异结果调查备忘 - A investigation memo of weird Oracle database search results
  5. [原] XAF How to Edit multiple objects in a ListViewAndDetailView
  6. 解决在android开发中ViewPager中Gallery无法滑动问题
  7. php 判断 xml 里是否存在某个节点
  8. 数据库范式(1NF 2NF 3NF BCNF)
  9. Mac 下如何使用sed -i命令
  10. C语言——第四次作业
  11. 单元测试过多,导致The configured user limit (128) on the number of inotify instances has been reached.
  12. 海量大数据大屏分析展示一步到位:DataWorks数据服务对接DataV最佳实践
  13. mysql 多行(GROUP_CONCAT)和多列(CONCAT)的合并函数
  14. 几种解决方法:idea 找不到符号或找不到包
  15. ViewModel学习
  16. java 多线程和并行程序设计
  17. unity3d-游戏实战突出重围,第一天
  18. protobuf和protostuff的区别
  19. Selenium (4) —— Selenium是什么? WebDriver是什么?做什么?(101 Tutorial)
  20. 计时器(Chronometer)

热门文章

  1. 序列化 jprotobuf
  2. spring+mybits 整合所需jar包的下载路径(亲测有效)
  3. python 基础(九) 文件操作
  4. [JLOI2016]圆的异或并
  5. python 全局变量 局部变量
  6. log4j打印错误日志输出 利用sql取出的值放在list集合中,集合中的字段类型为映射类类型
  7. java中的线程安全是什么?什么叫线程安全?什么叫不安全?
  8. android开发学习 ------- 上传本地项目到gitlab
  9. Oracle数据仓库创建教程
  10. 【Web应用-Kudu】Kudu 管理和诊断 azure web 应用