python logging with yaml
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.
python logging with yaml的更多相关文章
- Python logging 模块和使用经验
记录下常用的一些东西,每次用总是查文档有点小麻烦. py2.7 日志应该是生产应用的重要生命线,谁都不应该掉以轻心 有益原则 级别分离 日志系统通常有下面几种级别,看情况是使用 FATAL - 导致程 ...
- (转)python logging模块
python logging模块 原文:http://www.cnblogs.com/dahu-daqing/p/7040764.html 1 logging模块简介 logging模块是Python ...
- python logging模块可能会令人困惑的地方
python logging模块主要是python提供的通用日志系统,使用的方法其实挺简单的,这块就不多介绍.下面主要会讲到在使用python logging模块的时候,涉及到多个python文件的调 ...
- python logging 配置
python logging 配置 在python中,logging由logger,handler,filter,formater四个部分组成,logger是提供我们记录日志的方法:handler是让 ...
- Python LOGGING使用方法
Python LOGGING使用方法 1. 简介 使用场景 场景 适合使用的方法 在终端输出程序或脚本的使用方法 print 报告一个事件的发生(例如状态的修改) logging.info()或log ...
- python logging 日志轮转文件不删除问题
前言 最近在维护项目的python项目代码,项目使用了 python 的日志模块 logging, 设定了保存的日志数目, 不过没有生效,还要通过contab定时清理数据. 分析 项目使用了 logg ...
- python logging模块使用
近来再弄一个小项目,已经到收尾阶段了.希望加入写log机制来增加程序出错后的判断分析.尝试使用了python logging模块. #-*- coding:utf-8 -*- import loggi ...
- python Logging的使用
日志是用来记录程序在运行过程中发生的状况,在程序开发过程中添加日志模块能够帮助我们了解程序运行过程中发生了哪些事件,这些事件也有轻重之分. 根据事件的轻重可分为以下几个级别: DEBUG: 详细信息, ...
- Python logging日志系统
写我小小的日志系统 配置logging有以下几种方式: 1)使用Python代码显式的创建loggers, handlers和formatters并分别调用它们的配置函数: 2)创建一个日志配置文件, ...
随机推荐
- python3错误之TypeError: 'dict_items' object is not callable
这种错误出现在循环结构中套循环结构,而循环时内部循环为字典,外部循环为该字典调用items方法后取到的值,内部循环调用外部循环中遍历的结果: 解决方案: 将外部循环的items()方法调用改为.key ...
- 2017浙江工业大学-校赛决赛 小M和天平
Description 小M想知道某件物品的重量,但是摆在他面前的只有一个天平(没有游标)和一堆石子,石子可以放左边也可以放右边.他现在知道每个石子的重量.问能不能根据上述条件,能不能测出所问的重量. ...
- freertos之任务
taskYIELD(): 通知调度器自己放弃运行态,可立即进行任务切换,而不必等到当前任务的时间片耗尽.这对于相同任务优先级的2个任务来说可加速效率.
- 一个页面有相同ID元素的情况分析
经常会遇到一个页面中有相同定义相同id的情况,从道理上来说,id应该是这个页面中某个元素的唯一标识,所以不应该出现有相同id的情况,否则会产生意想不到的结果.而且各个浏览器的表现也是不一样的.我只做了 ...
- 将GitLab上面的代码克隆到本地
1.安装GitLab客户端 2.去GitLab服务端找项目路径 3.去GitLab客户端去克隆代码 右键-->git Clone 4.最后结果
- git更新到远程服务器代码
git commit -a 在vi里输入一些内容 wq退出,git pull, git push
- let和const注意点
let 一.块级作用域 下面的代码如果使用var,最后输出的是10. var a = []; for (var i = 0; i < 10; i++) { a[i] = function () ...
- webpack(1)
在网页中会引用哪些常见的静态资源? JS .js .jsx .coffee .ts(TypeScript 类 C# 语言) CSS .css .less .sass .scss Images .jpg ...
- LR中下载文件的脚本
#include "web_api.h" Action(){ int iflen; //文件大小 long lfbody; //响应数据内容大小 web_url("xxx ...
- ios UI自动化测试
转载:http://www.cnblogs.com/dokaygang128/p/3517674.html 一.一些注意事项: 1.做自动化测试时注意如果是真机话首先要设置不锁屏. 2.自动化测试过程 ...