Skip to content

Commit 2ede33f

Browse files
committed
添加日志功能
1 parent 7cab443 commit 2ede33f

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/DrcomExecutor/__main__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def parse_args() -> argparse.Namespace:
3333
parser.add_argument(
3434
"-c", "--config_path", help="查询配置文件路径", action="store_true",
3535
)
36+
parser.add_argument(
37+
"-l", "--log_path", help="查询日志文件路径", action="store_true",
38+
)
3639
parser.add_argument(
3740
"-r", "--reset", help="重置配置项", action="store_true",
3841
)
@@ -59,7 +62,12 @@ def parse_args() -> argparse.Namespace:
5962
if args.config_path:
6063
print(f"配置文件位于{Config.path}\n")
6164
sys.exit()
65+
if args.log_path:
66+
print(f"日志文件位于{Config.log_path}\n")
67+
sys.exit()
6268

6369
config.dump()
6470

6571
main()
72+
if __name__ == '__main__':
73+
main()

src/DrcomExecutor/config/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class Config:
77
path = Path(__file__).parent / "config.yaml"
88
default_path = Path(__file__).parent / "config_default.yaml"
9+
log_path = Path(__file__).parent / "DE.log"
910

1011
def __init__(self):
1112
if not self.path.is_file():
@@ -14,6 +15,7 @@ def __init__(self):
1415
else:
1516
Config.reset()
1617
self.data = self.read_yaml(self.path)
18+
self.log_path.open('a')
1719

1820
@staticmethod
1921
def read_yaml(file_path):

src/DrcomExecutor/utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,21 @@ def reset_config():
7272

7373
def log(msg, error=False, warning=False):
7474
global ERROR_COUNT
75+
76+
def log_print(text):
77+
config.log_path.write_text(text+"\n")
78+
print(text)
79+
7580
if error:
76-
print("[{}] {} - {}".format(datetime.now(), msg, ERROR_COUNT))
81+
log_print("[{}] {} - {}".format(datetime.now(), msg, ERROR_COUNT))
7782
if config["behavior"]["print_traceback"]:
78-
traceback.print_exc()
83+
log_print(traceback.format_exc())
7984
time.sleep(min(2 ** ERROR_COUNT, config["behavior"]["exp_backoff_limit"]))
8085
ERROR_COUNT += 1
8186
elif warning:
82-
print("[{}] {}".format(datetime.now(), msg))
87+
log_print("[{}] {}".format(datetime.now(), msg))
8388
else:
84-
print("[{}] {}".format(datetime.now(), msg))
89+
log_print("[{}] {}".format(datetime.now(), msg))
8590
reset_error_count()
8691

8792

0 commit comments

Comments
 (0)