This commit is contained in:
2025-08-20 04:32:30 +02:00
parent e4bb201181
commit c9a45d3435
19 changed files with 385 additions and 99 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,10 +1,11 @@
import unittest
import os
import shutil
from lib.core.logger.factory import new
from lib.core.logger.model import LogItemArgs, LogType, Logger # Import Logger class
from lib.data.ourtime.ourtime import new as ourtime_new, now as ourtime_now
from lib.core.pathlib.pathlib import get_file, ls, rmdir_all
from herolib.core.logger.factory import new
from herolib.core.logger.model import LogItemArgs, LogType, Logger # Import Logger class
from herolib.data.ourtime.ourtime import new as ourtime_new, now as ourtime_now
from herolib.core.pathlib.pathlib import get_file, ls, rmdir_all
from herolib.core.logger.search import search, SearchArgs
class TestLogger(unittest.TestCase):
def setUp(self):
@@ -85,18 +86,18 @@ class TestLogger(unittest.TestCase):
self.assertEqual(len(files), 2) # Expecting two files: 2022-12-05-20.log and 2022-12-05-22.log
# Test search functionality
items_stdout = logger.search(
items_stdout = search(logger, SearchArgs(
timestamp_from=ourtime_new('2022-11-01 20:14:35'),
timestamp_to=ourtime_new('2025-11-01 20:14:35'),
logtype=LogType.STDOUT
)
))
self.assertEqual(len(items_stdout), 2)
items_error = logger.search(
items_error = search(logger, SearchArgs(
timestamp_from=ourtime_new('2022-11-01 20:14:35'),
timestamp_to=ourtime_new('2025-11-01 20:14:35'),
logtype=LogType.ERROR
)
))
self.assertEqual(len(items_error), 4)
# Test specific log content
@@ -115,34 +116,34 @@ class TestLogger(unittest.TestCase):
self.assertTrue(found_stdout_log, "Expected stdout log content not found")
# Test search by category
items_test_app = logger.search(
items_test_app = search(logger, SearchArgs(
timestamp_from=ourtime_new('2022-11-01 20:14:35'),
timestamp_to=ourtime_new('2025-11-01 20:14:35'),
cat='test-app'
)
))
self.assertEqual(len(items_test_app), 2)
items_error_test = logger.search(
items_error_test = search(logger, SearchArgs(
timestamp_from=ourtime_new('2022-11-01 20:14:35'),
timestamp_to=ourtime_new('2025-11-01 20:14:35'),
cat='error-test'
)
))
self.assertEqual(len(items_error_test), 4)
# Test search by log content
items_with_aaa = logger.search(
items_with_aaa = search(logger, SearchArgs(
timestamp_from=ourtime_new('2022-11-01 20:14:35'),
timestamp_to=ourtime_new('2025-11-01 20:14:35'),
log='aaa'
)
))
self.assertEqual(len(items_with_aaa), 2)
# Test search with timestamp range
items_specific_time = logger.search(
items_specific_time = search(logger, SearchArgs(
timestamp_from=ourtime_new('2022-12-05 22:00:00'),
timestamp_to=ourtime_new('2022-12-05 23:00:00'),
logtype=LogType.ERROR
)
))
self.assertEqual(len(items_specific_time), 2)

View File

@@ -16,122 +16,98 @@ class SearchArgs:
self.logtype = logtype
self.maxitems = maxitems
def process(result: List[LogItem], current_item: LogItem, current_time: OurTime,
args: SearchArgs, from_time: int, to_time: int):
# Add previous item if it matches filters
log_epoch = current_item.timestamp.unix()
if log_epoch < from_time or log_epoch > to_time:
return
cat_match = (args.cat == '' or current_item.cat.strip() == args.cat)
log_match = (args.log == '' or args.log.lower() in current_item.log.lower())
logtype_match = (args.logtype is None or current_item.logtype == args.logtype)
if cat_match and log_match and logtype_match:
result.append(current_item)
def search(l: Logger, args_: SearchArgs) -> List[LogItem]:
args = args_
# Format category (max 10 chars, ascii only)
args.cat = name_fix(args.cat)
if len(args.cat) > 10:
raise ValueError('category cannot be longer than 10 chars')
timestamp_from = args.timestamp_from if args.timestamp_from else OurTime()
timestamp_to = args.timestamp_to if args.timestamp_to else OurTime()
from_time = args.timestamp_from.unix() if args.timestamp_from else 0
to_time = args.timestamp_to.unix() if args.timestamp_to else ourtime_new('2100-01-01').unix()
# Get time range
from_time = timestamp_from.unix()
to_time = timestamp_to.unix()
if from_time > to_time:
raise ValueError(f'from_time cannot be after to_time: {from_time} < {to_time}')
raise ValueError(f'from_time cannot be after to_time: {from_time} > {to_time}')
result: List[LogItem] = []
# Find log files in time range
if not os.path.exists(l.path.path):
return []
files = sorted(os.listdir(l.path.path))
for file in files:
if not file.endswith('.log'):
continue
# Parse dayhour from filename
dayhour = file[:-4] # remove .log
dayhour = file[:-4]
try:
file_time = ourtime_new(dayhour)
except ValueError:
continue # Skip if filename is not a valid time format
current_time = OurTime()
current_item = LogItem(OurTime(), "", "", LogType.STDOUT) # Initialize with dummy values
collecting = False
# Skip if file is outside time range
if file_time.unix() < from_time or file_time.unix() > to_time:
continue
# Read and parse log file
content = ""
file_hour_start_unix = file_time.unix()
file_hour_end_unix = file_hour_start_unix + 3599
if file_hour_end_unix < from_time or file_hour_start_unix > to_time:
continue
try:
with open(os.path.join(l.path.path, file), 'r') as f:
content = f.read()
except FileNotFoundError:
continue
lines = content.split('\n')
current_time = None
current_item = None
for line in lines:
for line in content.splitlines():
if len(result) >= args.maxitems:
return result
break
line_trim = line.strip()
if not line_trim:
if not line.strip() and current_item:
if from_time <= current_item.timestamp.unix() <= to_time:
if (not args.cat or args.cat == current_item.cat) and \
(not args.log or args.log.lower() in current_item.log.lower()) and \
(args.logtype is None or args.logtype == current_item.logtype):
result.append(current_item)
current_item = None
continue
# Check if this is a timestamp line
if not (line.startswith(' ') or line.startswith('E')):
if not line.startswith(' ') and not line.startswith('E'):
if current_item:
if from_time <= current_item.timestamp.unix() <= to_time:
if (not args.cat or args.cat == current_item.cat) and \
(not args.log or args.log.lower() in current_item.log.lower()) and \
(args.logtype is None or args.logtype == current_item.logtype):
result.append(current_item)
try:
current_time = ourtime_new(line_trim)
current_time = ourtime_new(f"{file_time.day()} {line.strip()}")
current_item = None
except ValueError:
continue # Skip if not a valid timestamp line
current_time = None
current_item = None
elif current_time:
if line.startswith(' ') or line.startswith('E'):
if len(line) > 14 and line[13] == '-':
if current_item:
if from_time <= current_item.timestamp.unix() <= to_time:
if (not args.cat or args.cat == current_item.cat) and \
(not args.log or args.log.lower() in current_item.log.lower()) and \
(args.logtype is None or args.logtype == current_item.logtype):
result.append(current_item)
is_error = line.startswith('E')
logtype = LogType.ERROR if is_error else LogType.STDOUT
cat = line[2:12].strip()
log_content = line[15:]
if collecting:
process(result, current_item, current_time, args, from_time, to_time)
collecting = False
continue
if collecting and len(line) > 14 and line[13] == '-':
process(result, current_item, current_time, args, from_time, to_time)
collecting = False
# Parse log line
is_error = line.startswith('E')
if not collecting:
# Start new item
cat_start = 2
cat_end = 12
log_start = 15
if len(line) < log_start:
continue # Line too short to contain log content
current_item = LogItem(
timestamp=current_time,
cat=line[cat_start:cat_end].strip(),
log=line[log_start:].strip(),
logtype=LogType.ERROR if is_error else LogType.STDOUT
)
collecting = True
else:
# Continuation line
if len(line_trim) < 16: # Check for minimum length for continuation line
current_item.log += '\n' + line_trim
else:
current_item.log += '\n' + line[15:].strip() # Use strip for continuation lines
# Add last item if collecting
if collecting:
process(result, current_item, current_time, args, from_time, to_time)
current_item = LogItem(timestamp=current_time, cat=cat, log=log_content.strip(), logtype=logtype)
elif current_item:
current_item.log += "\n" + (line[15:] if len(line) >15 else line)
if current_item:
if from_time <= current_item.timestamp.unix() <= to_time:
if (not args.cat or args.cat == current_item.cat) and \
(not args.log or args.log.lower() in current_item.log.lower()) and \
(args.logtype is None or args.logtype == current_item.logtype):
result.append(current_item)
return result

View File

@@ -1,4 +1,6 @@
import re
from datetime import datetime
import os
def name_fix(name: str) -> str:
# VLang's name_fix converts '-' to '_' and cleans up special chars.

Binary file not shown.