无法将控制台警告/输出记录到日志文件中

时间:2015-09-27 06:54:57

标签: python logging

我希望在日志文件中看到控制台上显示的所有输出。我浏览了日志记录模块的基础知识,但无法确定我出错的地方。

我的代码是:

# temp2.py
import logging
import pytz
from parameters import *
import pandas as pd
import recos



def create_tbid_cc(tbid_path):
    """Function to read campaign-TBID mapping and remove duplicate entries by
    TBID-CampaignName"""
    logging.debug('Started')
    tbid_cc = pd.read_csv(tbid_path, sep='|')
    tbid_cc.columns = map(str.lower, tbid_cc.columns)
    tbid_cc_unique = tbid_cc[~(tbid_cc.tbid.duplicated())]
    tbid_cc_unique.set_index('tbid', inplace=True)
    tbid_cc_unique['campaignname_upd'] = tbid_cc_unique['campaignname']
    del tbid_cc_unique['campaignname']
    return tbid_cc, tbid_cc_unique


def main():
    logging.basicConfig(
    filename='app.log', filemode='w',
    format='%(asctime)s : %(levelname)s : %(message)s',
    datefmt='%m/%d/%Y %I:%M:%S %p',
    level=logging.DEBUG)
    tbid_cc, tbid_cc_unique = create_tbid_cc(tbid_path=tbid_campaign_map_path)     

if __name__ == '__main__':
    main()

控制台输出:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  tbid_cc_unique['campaignname_upd'] = tbid_cc_unique['campaignname']

myapp.log的输出是:

09/27/2015 06:29:56 AM : DEBUG : Started

我想在myapp.log文件中看到控制台上显示的警告,但是无法执行此操作。我已将日志记录级别设置为' DEBUG'但仍然输出日志文件是上面提到的唯一一行。对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

引发的警告不会自动记录 - 您在控制台中看到它们,因为pandas正在将它们写入stderr。

如果要将这些消息写入日志,则需要捕获这些警告,然后手动记录消息。

请参阅:

https://docs.python.org/2/library/warnings.html

相关问题