Coverage for src/unit_cooler/actuator/work_log.py: 95%
34 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-23 14:35 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-23 14:35 +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)
66 # エラーメトリクス記録
67 try:
68 from unit_cooler.actuator.webapi.metrics import record_error
70 record_error("work_log_error", message)
71 except ImportError:
72 pass
73 elif level == unit_cooler.const.LOG_LEVEL.WARN:
74 # 警告メトリクス記録
75 try:
76 from unit_cooler.actuator.webapi.metrics import record_warning
78 record_warning("work_log_warning", message)
79 except ImportError:
80 pass
83if __name__ == "__main__":
84 # TEST Code
85 import multiprocessing
87 import docopt
88 import my_lib.config
89 import my_lib.logger
90 import my_lib.pretty
91 import my_lib.webapp.config
93 args = docopt.docopt(__doc__)
95 config_file = args["-c"]
96 debug_mode = args["-D"]
98 my_lib.logger.init("test", level=logging.DEBUG if debug_mode else logging.INFO)
100 config = my_lib.config.load(config_file)
101 event_queue = multiprocessing.Queue()
103 my_lib.webapp.config.init(config["actuator"])
104 my_lib.webapp.log.init(config)
105 init(config, event_queue)
107 add("Test", unit_cooler.const.LOG_LEVEL.INFO)
108 add("Test", unit_cooler.const.LOG_LEVEL.WARN)
109 add("Test", unit_cooler.const.LOG_LEVEL.ERROR)
111 logging.info(my_lib.pretty.format(hist_get()))
113 term()