├── .gitignore ├── README.md ├── parse_logs.py └── test_log.log /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | 5 | # C extensions 6 | *.so 7 | 8 | # Distribution / packaging 9 | .Python 10 | env/ 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | *.egg-info/ 23 | .installed.cfg 24 | *.egg 25 | 26 | # PyInstaller 27 | # Usually these files are written by a python script from a template 28 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 29 | *.manifest 30 | *.spec 31 | 32 | # Installer logs 33 | pip-log.txt 34 | pip-delete-this-directory.txt 35 | 36 | # Unit test / coverage reports 37 | htmlcov/ 38 | .tox/ 39 | .coverage 40 | .coverage.* 41 | .cache 42 | nosetests.xml 43 | coverage.xml 44 | *,cover 45 | 46 | # Translations 47 | *.mo 48 | *.pot 49 | 50 | # Sphinx documentation 51 | docs/_build/ 52 | 53 | # PyBuilder 54 | target/ 55 | 56 | output/ 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # python-log-parse-example 2 | Simple log parsing example in Python 3 | 4 | To run this example, clone or download this repository, and in terminal run: 5 | `python parse_logs.py` 6 | -------------------------------------------------------------------------------- /parse_logs.py: -------------------------------------------------------------------------------- 1 | import os 2 | import re 3 | 4 | # Regex used to match relevant loglines (in this case, a specific IP address) 5 | line_regex = re.compile(r".*fwd=\"12.34.56.78\".*$") 6 | 7 | # Output file, where the matched loglines will be copied to 8 | output_filename = os.path.normpath("output/parsed_lines.log") 9 | # Overwrites the file, ensure we're starting out with a blank file 10 | with open(output_filename, "w") as out_file: 11 | out_file.write("") 12 | 13 | # Open output file in 'append' mode 14 | with open(output_filename, "a") as out_file: 15 | # Open input file in 'read' mode 16 | with open("test_log.log", "r") as in_file: 17 | # Loop over each log line 18 | for line in in_file: 19 | # If log line matches our regex, print to console, and output file 20 | if (line_regex.search(line)): 21 | print line 22 | out_file.write(line) 23 | -------------------------------------------------------------------------------- /test_log.log: -------------------------------------------------------------------------------- 1 | 2015-10-02 17:16:15.572813+00:00 heroku router - - at=info method=GET path="/" host=testapp.herokuapp.com request_id=5a32a15f-fb80-4559-b52a-d3551408c088 fwd="11.22.33.44" dyno=web.1 connect=1ms service=5440ms status=200 bytes=8823 High Response Time 2 | 2015-10-02 17:16:15.833793+00:00 heroku router - - at=info method=GET path="/main.js" host=testapp.herokuapp.com request_id=46eaec8e-afda-4872-a249-53a7f3c04f3c fwd="12.34.56.78" dyno=web.1 connect=1ms service=23ms status=200 bytes=12418 3 | 2015-10-02 17:16:24.270610+00:00 heroku router - - at=info method=GET path="/main.js" host=testapp.herokuapp.com request_id=2743a6f1-778f-4452-9fd7-c2a4c2473258 fwd="11.22.33.44" dyno=web.1 connect=1ms service=15ms status=304 bytes=236 4 | 2015-10-02 17:16:15.842297+00:00 heroku router - - at=info method=GET path="/styles.css" host=testapp.herokuapp.com request_id=85c5c3ed-c893-4e50-a99d-8e01bac0c52b fwd="12.34.56.78" dyno=web.1 connect=0ms service=17ms status=200 bytes=2993 5 | 2015-10-02 17:16:24.271826+00:00 heroku router - - at=info method=GET path="/styles.css" host=testapp.herokuapp.com request_id=a409275e-3e27-4e14-b6fd-59edac5a7792 fwd="11.22.33.44" dyno=web.1 connect=0ms service=9ms status=304 bytes=235 6 | 2015-10-02 17:16:16.486789+00:00 heroku router - - at=info method=GET path="/icons/android-touch-icon.png" host=testapp.herokuapp.com request_id=1c16c27b-751e-483e-a0e5-014e47c7fb0f fwd="12.34.56.78" dyno=web.1 connect=0ms service=29ms status=404 bytes=239 7 | 2015-10-02 17:16:24.047899+00:00 heroku router - - at=info method=GET path="/" host=testapp.herokuapp.com request_id=c72f2ff7-c7c8-43ac-9d79-5ab7c9aeaae6 fwd="12.34.56.78" dyno=web.1 connect=0ms service=12ms status=304 bytes=236 8 | 2015-10-02 17:17:17.035437+00:00 heroku router - - at=error code=H13 desc="Connection closed without response" method=POST path="/api/1.0/player/weekly" host=testapp.herokuapp.com request_id=56ff999f-2e56-4bf7-8716-fb6765adc86e fwd="52.6.158.18" dyno=web.1 connect=1ms service=1325ms status=503 bytes=0 Critical 9 | 10 | --------------------------------------------------------------------------------