| 181 | |
| 182 | |
| 183 | def close_logfile(logdir: str): |
| 184 | if not isinstance(logdir, str) or len(logdir) == 0 or not os.path.isdir(logdir): |
| 185 | return |
| 186 | # 日志关闭前保存日志路径 |
| 187 | filepath = None |
| 188 | try: |
| 189 | filepath = sys.stdout.filepath |
| 190 | except: |
| 191 | pass |
| 192 | sys.stdout.close() |
| 193 | sys.stderr.close() |
| 194 | log_dir = Path(logdir).resolve() |
| 195 | if isinstance(filepath, Path): |
| 196 | print(f"Log file '{filepath}' saved.") |
| 197 | assert (filepath.parent.samefile(log_dir)) |
| 198 | # 清理空文件 |
| 199 | for f in log_dir.glob(r'*_err.txt'): |
| 200 | if f.stat().st_size == 0: |
| 201 | try: |
| 202 | f.unlink(missing_ok=True) |
| 203 | except: |
| 204 | pass |
| 205 | # 合并日志 只检测日志目录内的文本日志,忽略子目录。三天前的日志,按日合并为单个日志,三个月前的日志, |
| 206 | # 按月合并为单个月志,去年及以前的月志,今年4月以后将之按年合并为年志 |
| 207 | # 测试步骤: |
| 208 | """ |
| 209 | LOGDIR=/tmp/mlog |
| 210 | mkdir -p $LOGDIR |
| 211 | for f in {2016..2020}{01..12}{01..28};do;echo $f>$LOGDIR/mdc_${f}T235959.txt;done |
| 212 | for f in {01..09}{01..28};do;echo 2021$f>$LOGDIR/mdc_2021${f}T235959.txt;done |
| 213 | for f in {00..23};do;echo 20211001T$f>$LOGDIR/mdc_20211001T${f}5959.txt;done |
| 214 | echo "$(ls -1 $LOGDIR|wc -l) files in $LOGDIR" |
| 215 | # 1932 files in /tmp/mlog |
| 216 | mdc -zgic1 -d0 -m3 -o $LOGDIR |
| 217 | # python3 ./Movie_Data_Capture.py -zgic1 -o $LOGDIR |
| 218 | ls $LOGDIR |
| 219 | # rm -rf $LOGDIR |
| 220 | """ |
| 221 | today = datetime.today() |
| 222 | # 第一步,合并到日。3天前的日志,文件名是同一天的合并为一份日志 |
| 223 | for i in range(1): |
| 224 | txts = [f for f in log_dir.glob(r'*.txt') if re.match(r'^mdc_\d{8}T\d{6}$', f.stem, re.A)] |
| 225 | if not txts or not len(txts): |
| 226 | break |
| 227 | e = [f for f in txts if '_err' in f.stem] |
| 228 | txts.sort() |
| 229 | tmstr_3_days_ago = (today.replace(hour=0) - timedelta(days=3)).strftime("%Y%m%dT99") |
| 230 | deadline_day = f'mdc_{tmstr_3_days_ago}' |
| 231 | day_merge = [f for f in txts if f.stem < deadline_day] |
| 232 | if not day_merge or not len(day_merge): |
| 233 | break |
| 234 | cutday = len('T235959.txt') # cut length mdc_20201201|T235959.txt |
| 235 | for f in day_merge: |
| 236 | try: |
| 237 | day_file_name = str(f)[:-cutday] + '.txt' # mdc_20201201.txt |
| 238 | with open(day_file_name, 'a', encoding='utf-8') as m: |
| 239 | m.write(f.read_text(encoding='utf-8')) |
| 240 | f.unlink(missing_ok=True) |