Logarithmic scale for top N longest lines of some log
.py
def _longest(obj, n=20):''' Top n longest lines in the obj (file, filename, iterable) '''from collections import Counterimport mathifhasattr(obj, 'readlines'): obj.seek(0) c = Counter(len(z) for z in obj.readlines())elifisinstance(obj, str): f =open(obj, 'r', encoding='utf8') c = Counter(len(z) for z in f.readlines()) f.close()else: # treat as an iterabletry: c = Counter(map(len, obj))exceptTypeError:return lengths =sorted(c.keys())print(format('Line length', '>15'),format('Ln(len/prev)', '>15'),format('No. of lines', '>15'), ) prev =1for i in lengths[-20:]: ln = math.log(i/prev)print(format(i, '15'),format(ln, '15.1f') if ln >0.2elseformat(0, '15'),format(c[i], '15'), ) prev = i'''Initially written for the log preanalysis,using:>>> _Longest('logfile.log') Line length Ln(len/prev) No. of lines 1242206 ... 1 1306005 0 1 1490750 0 1 2338506 0.5 2 4817360 0.7 1 8017102 0.5 1'''