MCPcopy
hub / github.com/FedML-AI/FedML / init_logs

Method init_logs

python/fedml/core/mlops/mlops_runtime_log.py:100–155  ·  view source on GitHub ↗
(self, show_stdout_log=True)

Source from the content-addressed store, hash-verified

98 return MLOpsRuntimeLog._log_sdk_instance
99
100 def init_logs(self, show_stdout_log=True):
101 log_file_path, program_prefix = MLOpsRuntimeLog.build_log_file_path(self.args)
102 logging.raiseExceptions = True
103 self.logger = logging.getLogger(log_file_path)
104
105 class MLOpsFormatter(logging.Formatter):
106 converter = datetime.datetime.utcfromtimestamp
107
108 def __init__(self, fmt=None, datefmt=None, style='%', validate=True):
109 super().__init__(fmt, datefmt, style, validate)
110 self.ntp_offset = 0.0
111
112 # Here the `record` is a LogRecord object
113 def formatTime(self, record, datefmt=None):
114 log_time = record.created
115 if record.created is None:
116 log_time = time.time()
117
118 if self.ntp_offset is None:
119 self.ntp_offset = 0.0
120
121 log_ntp_time = int((log_time * 1000 + self.ntp_offset) / 1000.0)
122 ct = self.converter(log_ntp_time)
123 if datefmt:
124 s = ct.strftime(datefmt)
125 else:
126 s = ct.strftime("%a, %d %b %Y %H:%M:%S")
127 return s
128
129 format_str = MLOpsFormatter(fmt="[" + program_prefix + "] [%(asctime)s] [%(levelname)s] "
130 "[%(filename)s:%(lineno)d:%(funcName)s] %("
131 "message)s",
132 datefmt="%a, %d %b %Y %H:%M:%S")
133 format_str.ntp_offset = MLOpsUtils.get_ntp_offset()
134
135 stdout_handle = logging.StreamHandler()
136 stdout_handle.setFormatter(format_str)
137 if show_stdout_log:
138 stdout_handle.setLevel(logging.INFO)
139 self.logger.setLevel(logging.INFO)
140 else:
141 stdout_handle.setLevel(logging.CRITICAL)
142 self.logger.setLevel(logging.CRITICAL)
143 self.logger.handlers.clear()
144 self.logger.addHandler(stdout_handle)
145 if hasattr(self, "should_write_log_file") and self.should_write_log_file:
146 when = 'D'
147 backup_count = 100
148 file_handle = handlers.TimedRotatingFileHandler(filename=log_file_path, when=when,
149 backupCount=backup_count, encoding='utf-8')
150 file_handle.setFormatter(format_str)
151 file_handle.setLevel(logging.INFO)
152 self.logger.addHandler(file_handle)
153 logging.root = self.logger
154 # Rewrite sys.stdout to redirect stdout (i.e print()) to Logger
155 sys.stdout.write = self.logger.info
156
157 @staticmethod

Callers 13

initFunction · 0.80
init_logsFunction · 0.80
run_implMethod · 0.80
callback_start_trainMethod · 0.80
run_implMethod · 0.80
run_implMethod · 0.80
run_implMethod · 0.80

Calls 4

MLOpsFormatterClass · 0.85
get_ntp_offsetMethod · 0.80
build_log_file_pathMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected