2017-08-18 1 views
1

私のアプリケーションで実行されるPythonスクリプトがあります。このスクリプトでは、Pythonの組み込みのlogging APIを使用しています。私が抱えている問題は、すべてのログメッセージのファイル名が<string>と書かれていることです。スニペットで同じコードを実行すると、正常に動作します。以下 は、私はロガーを設定するために使用するコードです:PythonロギングAPIの印刷ファイル名は「<string>」

import logging 
import os 
import sys 
from logging import FileHandler, StreamHandler 

logger = logging.getLogger('update_menu') 
logger.setLevel(logging.DEBUG) 

# create handlers and set level to debug 
fileHandler = FileHandler(filename='/home/fguimaraes/work/update_menu.log') 
fileHandler.setLevel(logging.DEBUG) 

consoleHandler = StreamHandler(stream=sys.stdout) 
consoleHandler.setLevel(logging.DEBUG) 

# create formatter 
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s;File:%(filename)s;Function:%(funcName)s;Line:%(lineno)d') 

# add formatter to log 
fileHandler.setFormatter(formatter) 
consoleHandler.setFormatter(formatter) 

# add log to logger 
logger.addHandler(fileHandler) 
logger.addHandler(consoleHandler) 
+0

ロガー呼び出しを表示できますか? – MrName

答えて

1

スクリプトがメモリに読み込まれ、例えば使用して文字列として実行されているので、これはおそらくですexec。これは、スクリプトのファイル名がないことを意味します。例:

$ cat /tmp/test.py 
import logging 

logging.basicConfig(level=logging.DEBUG, format='%(filename)s: %(message)s') 
logging.debug('This is a test') 
[email protected]:~/projects/orbitilweb$ python /tmp/test.py 
test.py: This is a test 
$ python 
Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> with open('/tmp/test.py') as f: data = f.read() 
... 
>>> exec data 
<string>: This is a test 
>>>