如何仅在Python中设置模块的日志记录级别?

时间:2018-02-14 12:42:38

标签: python python-3.x logging

我正在使用MessageDigest输出有关我的脚本正在执行的操作的信息,并且我使用logging.info来启用此功能。这个(logging.basicConfig(level=logging.INFO))也影响我调用的其他模块(例如SQLAlchemy),导致比我想要的更详细的输出。我可以将我的实际脚本的日志记录级别设置为INFO,但不能将其使用的第三方模块设置为INFO(我更喜欢它们是警告吗?)

2 个答案:

答案 0 :(得分:2)

执行此操作的常规方法是在模块的开头为当前模块定义记录器(通常基于文件名),并在整个过程中参考。

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

def function_that_logs():
    logger.info('will log')   # note, using logger not logging
    logger.debug('will not log')

答案 1 :(得分:0)

/* just a snippet */
int fd = open("file.txt",O_RDONLY);
pid_t p = fork();
if(p<0)
{
  perror("forking error");
  exit(0);
}
else if(p>0)
{
  /* code part for parent*/
}
else //child
{
  dup2(fd,0);
  char* arg[1] = {NULL};
  execv("Process_name",arg);
}

这不适用于3.7.0。