...
This commit is contained in:
0
herolib/core/logger/__init__.py
Normal file
0
herolib/core/logger/__init__.py
Normal file
9
herolib/core/logger/factory.py
Normal file
9
herolib/core/logger/factory.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from herolib.core.pathlib.pathlib import get_dir
|
||||
from herolib.core.logger.model import Logger
|
||||
|
||||
def new(path: str) -> Logger:
|
||||
p = get_dir(path=path, create=True)
|
||||
return Logger(
|
||||
path=p,
|
||||
lastlog_time=0
|
||||
)
|
3
herolib/core/logger/log.py
Normal file
3
herolib/core/logger/log.py
Normal file
@@ -0,0 +1,3 @@
|
||||
# This file is now empty as the log function has been moved to model.py
|
||||
# It can be removed or kept as a placeholder if needed for future extensions.
|
||||
# For now, we will keep it empty.
|
150
herolib/core/logger/log_test.py
Normal file
150
herolib/core/logger/log_test.py
Normal file
@@ -0,0 +1,150 @@
|
||||
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
|
||||
|
||||
class TestLogger(unittest.TestCase):
|
||||
def setUp(self):
|
||||
# Corresponds to testsuite_begin()
|
||||
if os.path.exists('/tmp/testlogs'):
|
||||
rmdir_all('/tmp/testlogs')
|
||||
|
||||
def tearDown(self):
|
||||
# Corresponds to testsuite_end()
|
||||
# if os.path.exists('/tmp/testlogs'):
|
||||
# rmdir_all('/tmp/testlogs')
|
||||
pass
|
||||
|
||||
def test_logger_functionality(self):
|
||||
logger = new('/tmp/testlogs')
|
||||
|
||||
# Test stdout logging
|
||||
logger.log(LogItemArgs(
|
||||
cat='test-app',
|
||||
log='This is a test message\nWith a second line\nAnd a third line',
|
||||
logtype=LogType.STDOUT,
|
||||
timestamp=ourtime_new('2022-12-05 20:14:35')
|
||||
))
|
||||
|
||||
# Test error logging
|
||||
logger.log(LogItemArgs(
|
||||
cat='error-test',
|
||||
log='This is an error\nWith details',
|
||||
logtype=LogType.ERROR,
|
||||
timestamp=ourtime_new('2022-12-05 20:14:35')
|
||||
))
|
||||
|
||||
logger.log(LogItemArgs(
|
||||
cat='test-app',
|
||||
log='This is a test message\nWith a second line\nAnd a third line',
|
||||
logtype=LogType.STDOUT,
|
||||
timestamp=ourtime_new('2022-12-05 20:14:36')
|
||||
))
|
||||
|
||||
logger.log(LogItemArgs(
|
||||
cat='error-test',
|
||||
log='''
|
||||
This is an error
|
||||
|
||||
With details
|
||||
''',
|
||||
logtype=LogType.ERROR,
|
||||
timestamp=ourtime_new('2022-12-05 20:14:36')
|
||||
))
|
||||
|
||||
logger.log(LogItemArgs(
|
||||
cat='error-test',
|
||||
log='''
|
||||
aaa
|
||||
|
||||
bbb
|
||||
''',
|
||||
logtype=LogType.ERROR,
|
||||
timestamp=ourtime_new('2022-12-05 22:14:36')
|
||||
))
|
||||
|
||||
logger.log(LogItemArgs(
|
||||
cat='error-test',
|
||||
log='''
|
||||
aaa2
|
||||
|
||||
bbb2
|
||||
''',
|
||||
logtype=LogType.ERROR,
|
||||
timestamp=ourtime_new('2022-12-05 22:14:36')
|
||||
))
|
||||
|
||||
# Verify log directory exists
|
||||
self.assertTrue(os.path.exists('/tmp/testlogs'), 'Log directory should exist')
|
||||
|
||||
# Get log file
|
||||
files = ls('/tmp/testlogs')
|
||||
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(
|
||||
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(
|
||||
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
|
||||
found_error_log = False
|
||||
for item in items_error:
|
||||
if "This is an error\nWith details" in item.log:
|
||||
found_error_log = True
|
||||
break
|
||||
self.assertTrue(found_error_log, "Expected error log content not found")
|
||||
|
||||
found_stdout_log = False
|
||||
for item in items_stdout:
|
||||
if "This is a test message\nWith a second line\nAnd a third line" in item.log:
|
||||
found_stdout_log = True
|
||||
break
|
||||
self.assertTrue(found_stdout_log, "Expected stdout log content not found")
|
||||
|
||||
# Test search by category
|
||||
items_test_app = logger.search(
|
||||
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(
|
||||
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(
|
||||
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(
|
||||
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)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
72
herolib/core/logger/model.py
Normal file
72
herolib/core/logger/model.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from enum import Enum
|
||||
from typing import Optional
|
||||
from herolib.data.ourtime.ourtime import OurTime
|
||||
from herolib.core.pathlib.pathlib import Path
|
||||
|
||||
class LogType(Enum):
|
||||
STDOUT = "stdout"
|
||||
ERROR = "error"
|
||||
|
||||
class LogItemArgs:
|
||||
def __init__(self, cat: str, log: str, logtype: LogType, timestamp: Optional[OurTime] = None):
|
||||
self.timestamp = timestamp
|
||||
self.cat = cat
|
||||
self.log = log
|
||||
self.logtype = logtype
|
||||
|
||||
import os
|
||||
from herolib.core.texttools.texttools import name_fix, expand, dedent
|
||||
from herolib.data.ourtime.ourtime import OurTime, now as ourtime_now
|
||||
|
||||
class Logger:
|
||||
def __init__(self, path: Path, lastlog_time: int = 0):
|
||||
self.path = path
|
||||
self.lastlog_time = lastlog_time
|
||||
|
||||
def log(self, args_: LogItemArgs):
|
||||
args = args_
|
||||
|
||||
t = args.timestamp if args.timestamp else ourtime_now()
|
||||
|
||||
# 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')
|
||||
args.cat = expand(args.cat, 10, ' ')
|
||||
|
||||
args.log = dedent(args.log).strip()
|
||||
|
||||
logfile_path = os.path.join(self.path.path, f"{t.dayhour()}.log")
|
||||
|
||||
# Create log file if it doesn't exist
|
||||
if not os.path.exists(logfile_path):
|
||||
with open(logfile_path, 'w') as f:
|
||||
pass # Create empty file
|
||||
self.lastlog_time = 0 # make sure we put time again
|
||||
|
||||
with open(logfile_path, 'a') as f:
|
||||
content = ''
|
||||
|
||||
# Add timestamp if we're in a new second
|
||||
if t.unix() > self.lastlog_time:
|
||||
content += f"\n{t.time().format_ss()}\n"
|
||||
self.lastlog_time = t.unix()
|
||||
|
||||
# Format log lines
|
||||
error_prefix = 'E' if args.logtype == LogType.ERROR else ' '
|
||||
lines = args.log.split('\n')
|
||||
|
||||
for i, line in enumerate(lines):
|
||||
if i == 0:
|
||||
content += f"{error_prefix} {args.cat} - {line}\n"
|
||||
else:
|
||||
content += f"{error_prefix} {line}\n"
|
||||
f.write(content.rstrip()) # Use rstrip to remove trailing whitespace
|
||||
f.write('\n') # Add a newline after each log entry for consistency
|
||||
|
||||
class LogItem:
|
||||
def __init__(self, timestamp: OurTime, cat: str, log: str, logtype: LogType):
|
||||
self.timestamp = timestamp
|
||||
self.cat = cat
|
||||
self.log = log
|
||||
self.logtype = logtype
|
137
herolib/core/logger/search.py
Normal file
137
herolib/core/logger/search.py
Normal file
@@ -0,0 +1,137 @@
|
||||
import os
|
||||
from typing import Optional, List
|
||||
from herolib.core.texttools.texttools import name_fix
|
||||
from herolib.data.ourtime.ourtime import OurTime, new as ourtime_new
|
||||
from herolib.core.logger.model import Logger, LogItem, LogType
|
||||
|
||||
class SearchArgs:
|
||||
def __init__(self, timestamp_from: Optional[OurTime] = None,
|
||||
timestamp_to: Optional[OurTime] = None,
|
||||
cat: str = "", log: str = "", logtype: Optional[LogType] = None,
|
||||
maxitems: int = 10000):
|
||||
self.timestamp_from = timestamp_from
|
||||
self.timestamp_to = timestamp_to
|
||||
self.cat = cat
|
||||
self.log = log
|
||||
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()
|
||||
|
||||
# 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}')
|
||||
|
||||
result: List[LogItem] = []
|
||||
|
||||
# Find log files in time range
|
||||
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
|
||||
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 = ""
|
||||
try:
|
||||
with open(os.path.join(l.path.path, file), 'r') as f:
|
||||
content = f.read()
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
|
||||
lines = content.split('\n')
|
||||
|
||||
for line in lines:
|
||||
if len(result) >= args.maxitems:
|
||||
return result
|
||||
|
||||
line_trim = line.strip()
|
||||
if not line_trim:
|
||||
continue
|
||||
|
||||
# Check if this is a timestamp line
|
||||
if not (line.startswith(' ') or line.startswith('E')):
|
||||
try:
|
||||
current_time = ourtime_new(line_trim)
|
||||
except ValueError:
|
||||
continue # Skip if not a valid timestamp line
|
||||
|
||||
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)
|
||||
|
||||
return result
|
Reference in New Issue
Block a user