首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Python日志中使用不同的类和导入动态更改文件句柄

如何在Python日志中使用不同的类和导入动态更改文件句柄
EN

Stack Overflow用户
提问于 2012-12-12 20:14:03
回答 3查看 47.2K关注 0票数 60

我不能执行动态日志记录fileHandle更改。

例如,我有3个班级

one.py

代码语言:javascript
复制
import logging
class One():
    def __init__(self,txt="?"):
        logging.debug("Hey, I'm the class One and I say: %s" % txt)

two.py

代码语言:javascript
复制
import logging
class Two():
    def __init__(self,txt="?"):
        logging.debug("Hey, I'm the class Two and I say: %s" % txt)

config.py

代码语言:javascript
复制
import logging
class Config():
    def __init__(self,logfile=None):
        logging.debug("Reading config")
        self.logfile(logfile)

myapp

代码语言:javascript
复制
from one import One
from two import Two
from config import Config
import logging

#Set default logging
logging.basicConfig( 
    level=logging.getLevelName(DEBUG), 
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    filename=None
)

logging.info("Starting with stdout")

o=One(txt="STDOUT")
c=Config(logfile="/tmp/logfile")

# Here must be the code that change the logging configuration and set the filehandler

t=One(txt="This must be on the file, not STDOUT")

如果我再次尝试loggin.basicConfig(),它不起作用。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13839554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档