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

1#!/usr/bin/env python3 

2""" 

3作動ログを記録します。主にテストで使用します。 

4 

5Usage: 

6 work_log.py [-c CONFIG] [-D] 

7 

8Options: 

9 -c CONFIG : CONFIG を設定ファイルとして読み込んで実行します。[default: config.yaml] 

10 -D : デバッグモードで動作します。 

11""" 

12 

13import logging 

14 

15import my_lib.webapp.event 

16import my_lib.webapp.log 

17 

18import unit_cooler.const 

19import unit_cooler.util 

20 

21config = None 

22enven_queue = None 

23 

24log_hist = [] 

25 

26 

27def init(config_, event_queue_): 

28 global config # noqa: PLW0603 

29 global event_queue # noqa: PLW0603 

30 

31 config = config_ 

32 event_queue = event_queue_ 

33 

34 

35def term(): 

36 global event_queue 

37 my_lib.webapp.log.term() 

38 

39 

40# NOTE: テスト用 

41def hist_clear(): 

42 global log_hist # noqa: PLW0603 

43 

44 log_hist = [] 

45 

46 

47# NOTE: テスト用 

48def hist_get(): 

49 global log_hist 

50 

51 return log_hist 

52 

53 

54def add(message, level=unit_cooler.const.LOG_LEVEL.INFO): 

55 global log_hist 

56 global config 

57 global event_queue 

58 

59 event_queue.put(my_lib.webapp.event.EVENT_TYPE.LOG) 

60 my_lib.webapp.log.add(message, level) 

61 

62 log_hist.append(message) 

63 

64 if level == unit_cooler.const.LOG_LEVEL.ERROR: 

65 unit_cooler.util.notify_error(config, message) 

66 

67 

68if __name__ == "__main__": 

69 # TEST Code 

70 import multiprocessing 

71 

72 import docopt 

73 import my_lib.config 

74 import my_lib.logger 

75 import my_lib.pretty 

76 import my_lib.webapp.config 

77 

78 args = docopt.docopt(__doc__) 

79 

80 config_file = args["-c"] 

81 debug_mode = args["-D"] 

82 

83 my_lib.logger.init("test", level=logging.DEBUG if debug_mode else logging.INFO) 

84 

85 config = my_lib.config.load(config_file) 

86 event_queue = multiprocessing.Queue() 

87 

88 my_lib.webapp.config.init(config["actuator"]) 

89 my_lib.webapp.log.init(config) 

90 init(config, event_queue) 

91 

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) 

95 

96 logging.info(my_lib.pretty.format(hist_get())) 

97 

98 term()