Update nologs.py
Browse files
nologs.py
CHANGED
|
@@ -1,30 +1,28 @@
|
|
| 1 |
-
import os
|
| 2 |
import subprocess
|
| 3 |
import logging
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
os.remove("logs.txt")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
level=logging.INFO,
|
| 12 |
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 13 |
-
filename="logs.txt",
|
| 14 |
-
filemode="w",
|
| 15 |
-
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
logging.error(f"程序发生异常: {e}")
|
|
|
|
|
|
|
| 1 |
import subprocess
|
| 2 |
import logging
|
| 3 |
|
| 4 |
+
# 配置日志
|
| 5 |
+
logging.basicConfig(filename='logs.txt', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
|
|
|
| 6 |
|
| 7 |
+
# 执行程序并将标准输出和标准错误重定向到日志
|
| 8 |
+
proc = subprocess.Popen(['python', 'v2_3.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# 读取并记录标准输出和标准错误
|
| 11 |
+
while True:
|
| 12 |
+
output = proc.stdout.readline()
|
| 13 |
+
error = proc.stderr.readline()
|
| 14 |
+
|
| 15 |
+
if not output and not error:
|
| 16 |
+
break
|
| 17 |
+
|
| 18 |
+
if output:
|
| 19 |
+
logging.info(output.strip())
|
| 20 |
+
|
| 21 |
+
if error:
|
| 22 |
+
logging.error(error.strip())
|
| 23 |
|
| 24 |
+
# 等待程序执行完成
|
| 25 |
+
proc.wait()
|
| 26 |
|
| 27 |
+
# 关闭日志处理程序
|
| 28 |
+
logging.shutdown()
|
|
|