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)创建一个日志配置文件, ...
随机推荐
- self.navigationController.navigationBar.translucent = YES航栏的属性默认 YES是透明效果并且主view不会偏移 NO是导航栏不透明 主view会向下偏移64px
交友:微信号 dwjluck2013 从iOS7开始,苹果对navigationBar进行了模糊处理,并把self.navigationController.navigationBar.translu ...
- Zynq7000开发系列-2(VMware与Ubuntu安装使用)
一.前言 在嵌入式开发中,是无法避免使用Linux系统的,因为在开发之前必须先搭建起交叉编译环境,而后关于Bootloader.Linux Kernel的裁剪移植,File system的制作,底层驱 ...
- 292. Nim游戏
292. Nim游戏 class Solution(object): def canWinNim(self, n): """ :type n: int :rtype: b ...
- linux系统下安装PHP扩展pcntl
1.查看当前的PHP版本并下载一个同样版本的php(我的是php5.6.22,我下的是php5.6.22) wget http://hk1.php.net/get/php-5.5.10.tar.gz ...
- 牛客网Java刷题知识点之方法覆盖(方法重写)和方法重载的区别
不多说,直接上干货! https://www.nowcoder.com/ta/review-java/review?query=&asc=true&order=&page=6 ...
- session是什么时候创建的
总结:session不是一打开网站就会立刻建立.它的建立需要基于下面两个条件中的任意一个: 1:在servlet中手动调用 HttpSession session = request.getSessi ...
- SQL Server 查看列,添加列,修改列,删除列
查看表:exec sp_help 表名 查看列: exec sp_columns 表名 查看列:select * from information_schema.columns where table ...
- 说说JVM原理?内存泄漏与溢出的区别?何时产生内存泄漏?
1.JVM原理 JVM是Java Virtual Machine(Java虚拟机)的缩写,它是整个java实现跨平台的最核心的部分,所有的Java程序会首先被编译为.class的类文件,这种类文件可以 ...
- P3375 【模板】KMP字符串匹配(全程注释,简单易懂)
题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...
- Android Studio 遇到的java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessExce问题
在将一个Eclipse的项目转移到AndroidStudio的过程中,碰到了的问题如下: Error:Execution failed for task ':learnChinese:mergeDeb ...