[英]How to capture STDOUT of a Python process running under IIS, FastCGI, and WSGI?
我有一個Python Flask應用程序。 當我從PowerShell運行它時,我可以在整個代碼中看到輸出流來自對print()
和logging.info()
等函數的調用。
當我將IIS指向我的應用程序並讓它通過帶有web.config文件的FastCGI運行時,該輸出流在哪里? 如何將其捕獲到日志文件中?
使用FastCGI / WSGI時有3種日志文件。
我們給它們命名:
app.logger.LEVEL("...")
登錄的所有內容 "[2019-01-12 21:08:00,748] INFO in _internal: 127.0.0.1 - - [12/Jan/2019 21:08:00] "GET /static/js/jquery-3.3.1.min.js HTTP/1.1" 304 -
2019-01-12 20:42:41 10.175.214.88 GET /static/js/jquery-ui.min.js 80 ****USER**** ****IP**** Mozilla/5.0+(Windows+NT+10.0;+WOW64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/70.0.3538.110+Safari/537.36 http://yourweb.com/smthing 304 0 0 1875
看這個例子:
from flask import Flask
from logging.config import dictConfig
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
}},
'handlers': {
'wsgi': {
'class': 'logging.StreamHandler',
'formatter': 'default'
},
'custom_handler': {
'class': 'logging.FileHandler',
'formatter': 'default',
'filename': r'C:\inetpub\wwwroot\myapp\logs\myapp.log'
}
},
'root': {
'level': 'INFO',
'handlers': ['wsgi', 'custom_handler']
}
})
app = Flask(__name__)
# other imports using logger should go here
# from a import b
# ...
和web.config文件:
<configuration>
<system.webServer>
<handlers>
<remove name="Python FastCGI" />
<add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" scriptProcessor="E:\Python362_64\python.exe|E:\Python362_64\Lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
</handlers>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer>
<appSettings>
<!-- Required settings -->
<add key="WSGI_HANDLER" value="myapp.app" />
<add key="PYTHONPATH" value="C:\inetpub\wwwroot\myapp" />
<add key="SCRIPT_NAME" value="/myapp" />
<add key="WSGI_LOG" value="C:\inetpub\wwwroot\myapp\logs\wsgi_myapp.log" />
<add key="WSGI_RESTART_FILE_REGEX" value=".*((\.py)|(\.config))$" />
</appSettings>
</configuration>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.