Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

auto_logger.py 7.6 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  1. import json
  2. import os
  3. import pkg_resources
  4. from super_gradients.common.aws_connection.aws_secrets_manager_connector import AWSSecretsManagerConnector
  5. from super_gradients.common.environment import AWS_ENV_NAME
  6. from super_gradients.common.environment.environment_config import DONT_USE_ELASTICSEARCH_LOGGER, DEFAULT_LOGGING_LEVEL
  7. class AutoLoggerConfig:
  8. """
  9. A Class for the Automated Logging Config that is created from the JSON config file (auto_logging_conf)
  10. """
  11. @staticmethod
  12. def generate_config_for_module_name(module_name, training_log_path=None, log_level=DEFAULT_LOGGING_LEVEL, max_bytes=10485760, logs_dir_path=None,
  13. handlers_list=None) -> dict:
  14. """
  15. generate_config_for_module_name - Returns a Config Dict For Logging
  16. :param module_name: The Python Module name to create auto_logging for
  17. :param log_level: Minimal log level to set for the new auto_logging
  18. :param max_bytes: Max size for the log file before rotation starts
  19. :param handlers_list: A list specifying the handlers (Console, etc..) - Better Leave Empty or None
  20. :param training_log_path: Path to training log file which all modules of super_gradients will write to. Ignored
  21. when set to None.
  22. :param logs_dir_path: Path to sg_logs directory (default=None), where module logs will be saved. When set
  23. to None- module logs will be saved in ~/sg_logs (created if path does not exist). Main use case is for
  24. testing.
  25. :return: python dict() with the new auto_logging for the module
  26. """
  27. # LOADING THE ORIGINAL ROOT CONFIG FILE
  28. conf_file_name = 'auto_logging_conf.json'
  29. conf_file_path = os.path.join(pkg_resources.resource_filename('super_gradients', '/common/auto_logging/'),
  30. conf_file_name)
  31. with open(conf_file_path, 'r') as logging_configuration_file:
  32. config_dict = json.load(logging_configuration_file)
  33. # CREATING THE PATH TO THE "HOME" FOLDER WITH THE LOG FILE NAME
  34. if not logs_dir_path:
  35. log_file_name = module_name + '.log'
  36. user_dir = os.path.expanduser(r"~")
  37. logs_dir_path = os.path.join(user_dir, 'sg_logs')
  38. if not os.path.exists(logs_dir_path):
  39. try:
  40. os.mkdir(logs_dir_path)
  41. except Exception as ex:
  42. print('[WARNING] - sg_logs folder was not found and couldn\'t be created from the code - '
  43. 'All of the Log output will be sent to Console!' + str(ex))
  44. # HANDLERS LIST IS EMPTY AS CONSOLE IS ONLY ROOT HANDLER BECAUSE MODULE LOGGERS PROPAGATE THEIR LOGS UP.
  45. handlers_list = []
  46. logger = {
  47. "level": log_level,
  48. "handlers": handlers_list,
  49. "propagate": True
  50. }
  51. config_dict['loggers'][module_name] = logger
  52. return config_dict
  53. log_file_path = os.path.join(logs_dir_path, log_file_name)
  54. # THE ENTRIES TO ADD TO THE ORIGINAL CONFIGURATION
  55. handler_name = module_name + '_file_handler'
  56. file_handler = {
  57. "class": "logging.handlers.RotatingFileHandler",
  58. "level": log_level,
  59. "formatter": "fileFormatter",
  60. "filename": log_file_path,
  61. "maxBytes": max_bytes,
  62. "backupCount": 20,
  63. "encoding": "utf8"
  64. }
  65. # CREATING ONLY A FILE HANDLER, CONSOLE IS ONLY ROOT HANDLER AS MODULE LOGGERS PROPAGATE THEIR LOGS UP.
  66. if handlers_list is None or handlers_list.empty():
  67. handlers_list = [handler_name]
  68. logger = {
  69. "level": log_level,
  70. "handlers": handlers_list,
  71. "propagate": True
  72. }
  73. # ADDING THE NEW LOGGER ENTRIES TO THE CONFIG DICT
  74. config_dict['handlers'][handler_name] = file_handler
  75. config_dict['loggers'][module_name] = logger
  76. config_dict['root']['handlers'].append(handler_name)
  77. if DONT_USE_ELASTICSEARCH_LOGGER:
  78. return config_dict
  79. # Creating a ElasticSearch handler
  80. elastic_handler, elastic_handler_name = AutoLoggerConfig.configure_elasticsearch_handler(config_dict,
  81. module_name)
  82. if elastic_handler and elastic_handler_name:
  83. handlers_list.append(elastic_handler_name)
  84. config_dict['handlers'][elastic_handler_name] = elastic_handler
  85. if training_log_path:
  86. training_file_handler = {
  87. "class": "logging.handlers.RotatingFileHandler",
  88. "level": log_level,
  89. "formatter": "fileFormatter",
  90. "filename": training_log_path,
  91. "maxBytes": max_bytes,
  92. "backupCount": 20,
  93. "encoding": "utf8"
  94. }
  95. # ALL OF DECI_TRAINER MODULES LOGGERS PROPAGATE UP TO THE ROOT SO THE ADD TRAIN FILE HANDLER FOR THE ROOT.
  96. config_dict['handlers']["training"] = training_file_handler
  97. config_dict['root']['handlers'].append("training")
  98. return config_dict
  99. @staticmethod
  100. def configure_elasticsearch_handler(config_dict: dict, module_name: str):
  101. """
  102. Configures the ElasticSearch loggeing handler through an matching library.
  103. """
  104. # Getting the elasticsearch secrets
  105. if not AWS_ENV_NAME:
  106. return None, None
  107. try:
  108. elastic_secrets = AWSSecretsManagerConnector. \
  109. get_secret_values_dict_for_secret_key_properties(env=AWS_ENV_NAME,
  110. secret_name='elasticLogging',
  111. secret_key='ELASTIC')
  112. # logging_user_name = elastic_secrets['ELASTIC.USERNAME']
  113. # logging_user_password = elastic_secrets['ELASTIC.PASSWORD']
  114. elastic_host = elastic_secrets['ELASTIC.HOST']
  115. elastic_port = int(elastic_secrets['ELASTIC.PORT'])
  116. elastic_index_name = elastic_secrets['ELASTIC.DEFAULT_INDEX_NAME']
  117. flush_frequency = int(elastic_secrets['ELASTIC.FLUSH_FREQUENCY_SECONDS'])
  118. # We import here because not everybody may want elasticsearch handler, thus doesn't need CMRESHandler library.
  119. from cmreslogging.handlers import CMRESHandler
  120. config_dict['handlers']['elasticsearch'] = {
  121. "level": "DEBUG",
  122. "class": "cmreslogging.handlers.CMRESHandler",
  123. "hosts": [
  124. {
  125. "host": elastic_host,
  126. "port": elastic_port
  127. }
  128. ],
  129. "es_index_name": elastic_index_name,
  130. "es_additional_fields": {
  131. "App": "Deci",
  132. "Environment": AWS_ENV_NAME
  133. },
  134. "auth_type": CMRESHandler.AuthType.NO_AUTH,
  135. # "auth_details": [
  136. # logging_user_name,
  137. # logging_user_password
  138. # ],
  139. "use_ssl": True,
  140. "flush_frequency_in_sec": flush_frequency
  141. }
  142. elastic_handler = config_dict['handlers']['elasticsearch']
  143. elastic_handler_name = module_name + '_elastic_handler'
  144. return elastic_handler, elastic_handler_name
  145. except Exception as e:
  146. print(f'Failed to get the elasticsearch logging secrets: {e}')
  147. return None, None
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...