Skip to main content

Similarity-driven log clustering for error log analysis

Project description

Log Similarity SDK

A powerful Python SDK for similarity-driven log clustering and error log analysis. Automatically groups similar log messages into clusters and identifies anomalous logs that don't match existing patterns.

Features

  • Intelligent Log Clustering: Automatically groups similar logs based on semantic similarity
  • Multi-layer Preprocessing: Advanced pattern normalization for universal log formats
  • Unmatched Log Detection: Identifies error logs that don't fit any existing cluster
  • High Performance: Efficient length-based indexing and similarity calculation
  • Flexible Configuration: Customizable similarity thresholds and preprocessing rules

Installation

pip install log-similarity

Or install from source:

git clone https://github.com/suiwenfeng/log-similarity-sdk.git
cd log-similarity-sdk
pip install -e .

Quick Start

1. Build Clusters from Base Error Logs

from log_similarity import SimilarityClusterer

# Sample base error logs
base_logs = [
    "User 12345 logged in from 192.168.1.100",
    "User 67890 logged in from 10.0.0.1",
    "Database connection timeout after 30 seconds",
    "Database connection timeout after 45 seconds",
    "GET /api/users returned 404",
    "GET /api/posts returned 500",
]

# Create and train clusterer
clusterer = SimilarityClusterer(similarity_threshold=0.85)
clusterer.fit(base_logs, verbose=True)

# View cluster summary
clusterer.print_summary()

2. Find Unmatched Error Logs

# New error logs to check
new_logs = [
    "User 11111 logged in from 172.16.0.1",  # Similar to cluster 1
    "GET /api/products returned 404",         # Similar to cluster 3
    "Critical: Out of memory error",          # NEW - doesn't match any cluster
    "Fatal: Disk space exhausted",            # NEW - doesn't match any cluster
]

# Find logs that don't match any existing cluster
unmatched = clusterer.find_unmatched_logs(new_logs, verbose=True)

# Process unmatched logs
for log in unmatched:
    print(f"Unmatched: {log['original']}")
    print(f"Template:  {log['template']}")
    print(f"Similarity: {log['similarity']:.2f}\n")

API Reference

SimilarityClusterer

Main class for log clustering and analysis.

Constructor

SimilarityClusterer(
    similarity_threshold: float = 0.90,
    merge_threshold: float = 0.95,
    length_tolerance: float = 0.3,
    preprocessor_config: Optional[PreprocessorConfig] = None
)

Parameters:

  • similarity_threshold: Minimum similarity score to match a cluster (default: 0.90)
  • merge_threshold: Threshold for merging logs (default: 0.95)
  • length_tolerance: Token length tolerance for comparison (default: 0.3)
  • preprocessor_config: Custom preprocessing configuration

Methods

fit(log_messages, verbose=True)

Build clusters from training logs.

Parameters:

  • log_messages (List[str]): List of log messages to cluster
  • verbose (bool): Print progress information

Returns:

  • self: The clusterer instance (for method chaining)

Example:

clusterer.fit(base_error_logs, verbose=True)
predict(log_message)

Predict which cluster a single log belongs to.

Parameters:

  • log_message (str): Log message to predict

Returns:

  • dict: Prediction result with keys:
    • cluster_id: Matched cluster ID or None
    • template: Log template
    • similarity: Similarity score
    • match_type: 'matched' or 'unmatched'
    • original: Original log message
    • preprocessed: Preprocessed log

Example:

result = clusterer.predict("User 999 logged in from 10.0.0.5")
if result['match_type'] == 'matched':
    print(f"Matched cluster: {result['cluster_id']}")
else:
    print("No matching cluster found")
predict_batch(log_messages, verbose=False)

Predict clusters for multiple logs.

Parameters:

  • log_messages (List[str]): List of log messages
  • verbose (bool): Show progress

Returns:

  • List[dict]: List of prediction results
find_unmatched_logs(log_messages, verbose=False)

Find logs that don't match any existing cluster.

Parameters:

  • log_messages (List[str]): Logs to check
  • verbose (bool): Show progress and statistics

Returns:

  • List[dict]: List of unmatched logs with keys:
    • original: Original log message
    • preprocessed: Preprocessed log
    • template: Extracted template
    • similarity: Highest similarity score

Example:

unmatched = clusterer.find_unmatched_logs(new_error_logs, verbose=True)
print(f"Found {len(unmatched)} new error patterns")
get_all_clusters()

Get all clusters with their details.

Returns:

  • List[dict]: Sorted list of clusters (by frequency)
save(filepath)

Save clusterer state to JSON file.

Parameters:

  • filepath (str): Path to save file

Advanced Usage

Custom Preprocessing

from log_similarity import PreprocessorConfig, LogPreprocessor

# Create custom config
config = PreprocessorConfig()
# Modify patterns as needed
# config.dynamic_patterns.append((r'custom_pattern', '<CUSTOM>'))

clusterer = SimilarityClusterer(preprocessor_config=config)

Adjusting Similarity Threshold

# Stricter matching (higher threshold)
strict_clusterer = SimilarityClusterer(similarity_threshold=0.95)

# More lenient matching (lower threshold)
lenient_clusterer = SimilarityClusterer(similarity_threshold=0.80)

Batch Processing

# Process large log files
with open('error_logs.txt', 'r') as f:
    logs = f.readlines()

# Build clusters from first 10k logs
clusterer.fit(logs[:10000], verbose=True)

# Find unmatched in remaining logs
unmatched = clusterer.find_unmatched_logs(logs[10000:], verbose=True)

# Save results
with open('unmatched_errors.txt', 'w') as f:
    for log in unmatched:
        f.write(log['original'] + '\n')

Use Cases

  1. Error Log Monitoring: Detect new error patterns in production logs
  2. Log Deduplication: Group similar logs to reduce noise
  3. Anomaly Detection: Identify unusual log patterns
  4. Log Template Extraction: Automatically extract log templates
  5. Log Analysis: Understand log distribution and patterns

Algorithm

The SDK uses a similarity-driven clustering algorithm with:

  1. Multi-layer Preprocessing: Normalizes dynamic parts (IPs, timestamps, UUIDs, etc.)
  2. Template Extraction: Identifies static and dynamic parts using Logram parser
  3. Similarity Calculation: Combines Jaccard similarity, prefix matching, and length ratio
  4. Efficient Indexing: Length-based indexing for fast cluster lookup

Requirements

  • Python >= 3.7
  • No external dependencies (pure Python)

License

MIT License

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Support

For issues and questions, please open an issue on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

log_similarity-1.0.0.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

log_similarity-1.0.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file log_similarity-1.0.0.tar.gz.

File metadata

  • Download URL: log_similarity-1.0.0.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for log_similarity-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b2aae49fcc744451e90b8407bf4485b93c6f887c0ec706578f3e5c77abc257d0
MD5 4dfc6469dc38a4638b240a5763d77a73
BLAKE2b-256 422525ea19460c77e0b89dad2b1fe1a2522b0193d1e93288a54a345321f2c301

See more details on using hashes here.

File details

Details for the file log_similarity-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: log_similarity-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for log_similarity-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67a30be96762bc82a570e98816514b313d7d80fe353f420098adaed32209dbdb
MD5 7502b7c4b50a1d79eaefd858f2e97add
BLAKE2b-256 87722a5fb43402fb88d46bdfcc2feb53ed86d9f3c69fe74fb61ef8325879e27f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page