Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support locally stored log file #25

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ To analyze a log file, run the script with the following command line arguments:

Example usage:

~/.local/bin/logdetective https://example.com/logs.txt
logdetective https://example.com/logs.txt

Or if the log file is stored locally:

logdetective ./data/logs.txt


Contributing
Expand Down
21 changes: 20 additions & 1 deletion logdetective/logdetective.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import sys
from urllib.request import urlretrieve
from urllib.parse import urlparse

import drain3
import numpy as np
Expand Down Expand Up @@ -215,6 +216,24 @@ def process_log(log: str, model: Llama) -> str:
return model(PROMPT_TEMPLATE.format(log), max_tokens=0)["choices"][0]["text"]


def retrieve_log_content(log_path):
"""Get content of the file on the log_path path."""
parsed_url = urlparse(log_path)
log = ""

if not parsed_url.scheme:
if not os.path.exists(log_path):
raise ValueError(f"Local log {log_path} doesn't exist!")

with open(log_path, "rt") as f:
log = f.read()

else:
log = requests.get(log_path, timeout=60).text

return log


def main():
"""Main execution function."""
parser = argparse.ArgumentParser("logdetective")
Expand Down Expand Up @@ -260,7 +279,7 @@ def main():
n_ctx=0,
verbose=args.verbose > 2)

log = requests.get(args.url, timeout=60).text
log = retrieve_log_content(args.url)
log_summary = extractor(log)

ratio = len(log_summary.split('\n')) / len(log.split('\n'))
Expand Down