Update nologs.py
Browse files
nologs.py
CHANGED
@@ -1,26 +1,30 @@
|
|
|
|
1 |
import logging
|
2 |
-
import os
|
3 |
import subprocess
|
4 |
|
5 |
# 配置日志
|
6 |
log_file = '/content/logs.txt'
|
7 |
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
8 |
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
logging.error(f"标准输出:\n{e.stdout.decode()}")
|
17 |
-
logging.error(f"标准错误:\n{e.stderr.decode()}")
|
18 |
|
19 |
-
|
20 |
-
#
|
|
|
|
|
|
|
21 |
logging.exception(f"程序发生异常: {e}")
|
22 |
|
23 |
finally:
|
24 |
-
#
|
25 |
-
|
26 |
-
|
|
|
1 |
+
import sys
|
2 |
import logging
|
|
|
3 |
import subprocess
|
4 |
|
5 |
# 配置日志
|
6 |
log_file = '/content/logs.txt'
|
7 |
logging.basicConfig(filename=log_file, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
8 |
|
9 |
+
# 创建一个将输出同时发送到控制台和日志文件的处理程序
|
10 |
+
console_handler = logging.StreamHandler(sys.stdout)
|
11 |
+
console_handler.setLevel(logging.INFO)
|
12 |
+
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
|
13 |
+
console_handler.setFormatter(formatter)
|
14 |
+
logging.getLogger().addHandler(console_handler)
|
15 |
|
16 |
+
# 重定向标准输出和标准错误到日志文件和控制台
|
17 |
+
sys.stdout = logging.getLogger().handlers[0].stream
|
18 |
+
sys.stderr = logging.getLogger().handlers[0].stream
|
|
|
|
|
19 |
|
20 |
+
try:
|
21 |
+
# 在try块中运行sd_clsa_webui_colab_ipynb_v2_3.py
|
22 |
+
subprocess.run(['python', 'sd_clsa_webui_colab_ipynb_v2_3.py'], check=True)
|
23 |
+
except subprocess.CalledProcessError as e:
|
24 |
+
# 捕获异常并记录到日志文件
|
25 |
logging.exception(f"程序发生异常: {e}")
|
26 |
|
27 |
finally:
|
28 |
+
# 最后,关闭日志处理程序,以确保所有日志都被写入到日志文件
|
29 |
+
logging.getLogger().removeHandler(console_handler)
|
30 |
+
console_handler.close()
|