Some checks failed
- Added @click.version_option decorator to main() in commands.py - Imported __version__ from loglens package - Resolves CI build failure: 'loglens --version' command not found
30 lines
894 B
Python
30 lines
894 B
Python
from typing import Optional
|
|
|
|
from loglens.parsers.apache_parser import ApacheParser
|
|
from loglens.parsers.base import LogFormat
|
|
from loglens.parsers.json_parser import JSONParser
|
|
from loglens.parsers.syslog_parser import SyslogParser
|
|
|
|
|
|
class ParserFactory:
|
|
"""Factory for creating parsers based on format."""
|
|
|
|
_parsers = {
|
|
LogFormat.JSON: JSONParser(),
|
|
LogFormat.SYSLOG: SyslogParser(),
|
|
LogFormat.APACHE: ApacheParser(),
|
|
}
|
|
|
|
@classmethod
|
|
def get_parser(cls, format_enum: LogFormat):
|
|
"""Get a parser for the specified format."""
|
|
return cls._parsers.get(format_enum)
|
|
|
|
@classmethod
|
|
def detect_format(cls, sample_line: str) -> LogFormat:
|
|
"""Detect the format of a log line."""
|
|
for fmt, parser in cls._parsers.items():
|
|
if parser.parse(sample_line):
|
|
return fmt
|
|
return LogFormat.RAW
|