记录模块不在IPython中打印

时间:2014-06-17 09:17:11

标签: python logging ipython qtconsole

以下代码在'ipython qtconsole'中打印'我想要打印',但它不能在普通的IPython中打印。

import logging
import logging.handlers

log = logging.getLogger()
f = logging.Formatter("%(asctime)s - %(module)s.   %(funcName)s - %(levelname)s - %(message)s")
fh = logging.handlers.TimedRotatingFileHandler('log.txt', 'W6')
fh.setFormatter(f)
log.addHandler(fh)
log.setLevel(logging.INFO)
log.info('I want this to print')

在'IPython qtconsole'中,我遇到了不同的问题,我试图解释here(这种情况不太好,不需要阅读!)。

你能告诉我为什么吗?

编辑:我使用的是Python 2.7

EDIT2:也许我真的只需要添加logging.StreamHandler。

3 个答案:

答案 0 :(得分:22)

似乎qtconsoleroot记录器添加了一个处理程序:

In [1]: import logging
   ...: root = logging.getLogger()
   ...: root.handlers
   ...: 
Out[1]: [<logging.StreamHandler at 0x7fd8e00e1f98>]

使用普通的python解释器或只使用ipython

In [1]: import logging

In [2]: root = logging.getLogger()

In [3]: root.handlers
Out[3]: []

如果您希望两者的行为相同,则应向正常StreamHandler的根记录器添加ipython,或从qtconsole解释器中删除StreamHandler

如果你想要前者只需添加:

root = logging.getLogger()
root.addHandler(logging.StreamHandler())

如果你想要后者,添加你自己的处理程序之前,请执行:

for handler in root.handlers[:]:
    root.removeHandler(handler)

请注意,IPython已经提供了一些用于记录文件的机制。见the documentation。如果你只想在ipython中使用它的魔法可能会更简单。

答案 1 :(得分:2)

如果您使用Bakuriu的后一种解决方案,如果root有多个处理程序,由于迭代时删除问题,它将无法正常工作。

改为使用:

while len(root.handlers):
    root.removeHandler(root.handlers[0])

答案 2 :(得分:2)

什么对我有用

在任何其他库/代码之前导入记录器,在单独的单元格中。这实际上是主要要求。如果我在一个单元格中加载日志记录和其他库,则无论该单元格中的层次结构是什么,日志记录都不起作用

<?php 
 $time_start = microtime(true);

        $healthy = array("fruits", "vegetables", "fiber");
        $yummy   = array("pizza", "beer", "ice cream");

        for($i=0;$i<=1000000;$i++){
            // Method 1
            $phrase  = "You should eat fruits, vegetables, and fiber every day.";
            $phrase = str_replace($healthy, $yummy, $phrase);
        }
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "Did Test 1 in ($time seconds)". PHP_EOL. PHP_EOL;



        $time_start = microtime(true);
        for($i=0;$i<=1000000;$i++){
            // Method2
            $phrase  = "You should eat fruits, vegetables, and fiber every day.";
            $phrase = str_replace("fruits", "pizza", $phrase);
            $phrase = str_replace("vegetables", "beer", $phrase);
            $phrase = str_replace("fiber", "ice cream", $phrase);

        }
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "Did Test 2 in ($time seconds)" . PHP_EOL. PHP_EOL;




        $time_start = microtime(true);
        for($i=0;$i<=1000000;$i++){
            $a = $healthy;
            $b = $yummy;
                foreach ($healthy as $k => $v) {
                  if (strpos($phrase, $healthy[$k]) === FALSE)  
                  unset($a[$k], $b[$k]);
                }                                          
                if ($a) $new_str = str_replace($a, $b, $phrase);

        }
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "Did Test 3 in ($time seconds)". PHP_EOL. PHP_EOL;



        $time_start = microtime(true);
        for($i=0;$i<=1000000;$i++){
            $ree = false;
            foreach ($healthy as $k) {
              if (strpos($phrase, $k) !== FALSE)  { //something to replace
                  $ree = true;
                  break;
              }
            }                                          
            if ($ree === true) {
                $new_str = str_replace($healthy, $yummy, $phrase);
            }
        }
        $time_end = microtime(true);
        $time = $time_end - $time_start;
        echo "Did Test 4 in ($time seconds)". PHP_EOL. PHP_EOL;

只有在加载库后我才设置日志配置以避免打印pyspark load debug

import logging
reload(logging)
logger = logging.getLogger(__name__)