@@ -97,26 +97,17 @@ def parse_line(line): |
||
| 97 | 97 |
|
| 98 | 98 |
|
| 99 | 99 |
def iter_lines(path): |
| 100 |
- """按 utf-8 读日志,失败则按 gbk 再试,最后兜底 latin-1(不抛异常)""" |
|
| 101 |
- for encoding in ('utf-8', 'gbk', 'latin-1'):
|
|
| 102 |
- try: |
|
| 103 |
- handle = io.open(path, 'r', encoding=encoding, errors='strict') |
|
| 104 |
- # 先读一小段确认能解开,避免读到一半才炸 |
|
| 105 |
- head = handle.read(4096) |
|
| 106 |
- handle.seek(0) |
|
| 107 |
- except (UnicodeDecodeError, LookupError): |
|
| 108 |
- try: |
|
| 109 |
- handle.close() |
|
| 110 |
- except Exception: |
|
| 111 |
- pass |
|
| 112 |
- continue |
|
| 100 |
+ """ |
|
| 101 |
+ 逐行读日志。 |
|
| 113 | 102 |
|
| 114 |
- for line in [head] + handle.readlines(): |
|
| 103 |
+ djlogit 的记录里,中文是 \u4e54 这种转义形式(纯 ASCII), |
|
| 104 |
+ 文件名和内容都按 utf-8 读即可;errors="replace" 兜住个别脏字节, |
|
| 105 |
+ 避免某个归档文件里混进坏数据就整批中断。 |
|
| 106 |
+ """ |
|
| 107 |
+ with io.open(path, 'r', encoding='utf-8', errors='replace') as handle: |
|
| 108 |
+ for line in handle: |
|
| 115 | 109 |
yield line |
| 116 | 110 |
|
| 117 |
- handle.close() |
|
| 118 |
- return |
|
| 119 |
- |
|
| 120 | 111 |
|
| 121 | 112 |
def collect(log_glob): |
| 122 | 113 |
""" |