Coverage for src/unit_cooler/actuator/work_log.py: 100%
23 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-28 12:29 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-28 12:29 +0000
1#!/usr/bin/env python3
2"""
3作動ログを記録します。主にテストで使用します。
5Usage:
6 work_log.py [-c CONFIG] [-D]
8Options:
9 -c CONFIG : CONFIG を設定ファイルとして読み込んで実行します。[default: config.yaml]
10 -D : デバッグモードで動作します。
11"""
13import logging
15import my_lib.webapp.event
16import my_lib.webapp.log
18import unit_cooler.const
19import unit_cooler.util
21config = None
22enven_queue = None
24log_hist = []
27def init(config_, event_queue_):
28 global config # noqa: PLW0603
29 global event_queue # noqa: PLW0603
31 config = config_
32 event_queue = event_queue_
35def term():
36 global event_queue
37 my_lib.webapp.log.term()
40# NOTE: テスト用
41def hist_clear():
42 global log_hist # noqa: PLW0603
44 log_hist = []
47# NOTE: テスト用
48def hist_get():
49 global log_hist
51 return log_hist
54def add(message, level=unit_cooler.const.LOG_LEVEL.INFO):
55 global log_hist
56 global config
57 global event_queue
59 event_queue.put(my_lib.webapp.event.EVENT_TYPE.LOG)
60 my_lib.webapp.log.add(message, level)
62 log_hist.append(message)
64 if level == unit_cooler.const.LOG_LEVEL.ERROR:
65 unit_cooler.util.notify_error(config, message)
68if __name__ == "__main__":
69 # TEST Code
70 import multiprocessing
72 import docopt
73 import my_lib.config
74 import my_lib.logger
75 import my_lib.pretty
76 import my_lib.webapp.config
78 args = docopt.docopt(__doc__)
80 config_file = args["-c"]
81 debug_mode = args["-D"]
83 my_lib.logger.init("test", level=logging.DEBUG if debug_mode else logging.INFO)
85 config = my_lib.config.load(config_file)
86 event_queue = multiprocessing.Queue()
88 my_lib.webapp.config.init(config["actuator"])
89 my_lib.webapp.log.init(config)
90 init(config, event_queue)
92 add("Test", unit_cooler.const.LOG_LEVEL.INFO)
93 add("Test", unit_cooler.const.LOG_LEVEL.WARN)
94 add("Test", unit_cooler.const.LOG_LEVEL.ERROR)
96 logging.info(my_lib.pretty.format(hist_get()))
98 term()