Fujio-Turner / sg-log-reader-demo

Parsing and Aggregating Sync Gateway Logs
https://fujio-turner.github.io/sg-log-reader-demo/
Apache License 2.0
5 stars 1 forks source link

House Keeping - Too Many Exceptions to SG Versions #39

Open Fujio-Turner opened 1 month ago

Fujio-Turner commented 1 month ago

Your approach to handling different versions of Sync Gateway logs based on a configuration file is smart. Here's how you could implement this:

1. Modify config.json

Add a new field for the Sync Gateway version:

{
  "sgversionParse": "3.1.1",
  // other config entries
}

2. Modify SGLogReader Class

Update the __init__ method to read this configuration:

import json
from packaging import version

class SGLogReader:
    def __init__(self, configFile):
        with open(configFile, 'r') as file:
            config = json.load(file)

        self.sg_version = version.parse(config.get('sgversionParse', '0.0.0'))
        # Other initialization code...

    def parse_log_entry(self, log_entry):
        if self.sg_version >= version.parse('3.1.0'):
            # Parsing logic for versions >= 3.1.0
            # Example:
            return self._parse_v3_1(log_entry)
        elif self.sg_version >= version.parse('3.0.0'):
            # Parsing logic for versions >= 3.0.0 but < 3.1.0
            return self._parse_v3_0(log_entry)
        else:
            # Default or older version parsing
            return self._parse_default(log_entry)

    def _parse_v3_1(self, log_entry):
        # Specific parsing logic for version 3.1.x
        pass

    def _parse_v3_0(self, log_entry):
        # Specific parsing logic for version 3.0.x
        pass

    def _parse_default(self, log_entry):
        # Default parsing logic
        pass

3. Using packaging for Version Comparison

4. Additional Considerations:

5. Example Test:

def test_parse_log_entry_version_3_1(self):
    # Assuming you have a way to mock or set the version for testing
    reader = SGLogReader('path_to_config.json')  # Mock config with version 3.1.1
    sample_entry = "Sample log entry for 3.1.1"
    parsed = reader.parse_log_entry(sample_entry)
    # Assert expected parsing results

Benefits:

This approach leverages configuration for flexibility, uses Python's strengths in handling complex logic with simple syntax, and ensures that your application can adapt to different versions of Sync Gateway logs with minimal code changes.