Python Log File Analyzer Example

A log file analyzer is a good beginner project because it combines several useful Python skills in one script.

In this example, you will read a log file, look for important lines such as ERROR, WARNING, and INFO, count them, and print a simple summary.

This page focuses on building one practical script. If you want a separate lesson on file handling, see how to read a file in Python or how to read a file line by line in Python.

Quick working version #

If you want a simple version first, start with this:

from collections import Counter

log_file = "app.log"
counts = Counter()

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()
        if "ERROR" in line:
            counts["ERROR"] += 1
        elif "WARNING" in line:
            counts["WARNING"] += 1
        elif "INFO" in line:
            counts["INFO"] += 1

print("Log summary")
for level, total in counts.items():
    print(f"{level}: {total}")

This version:

  • opens the file
  • reads it one line at a time
  • checks each line for common log levels
  • counts matches
  • prints the totals

Next, let’s build it step by step so the code is easier to understand.

What this example builds #

This script will:

  • Read a text log file line by line
  • Look for common log levels such as ERROR, WARNING, and INFO
  • Count how many times each level appears
  • Print a simple report at the end

What a log file is #

A log file is a text file created by a program.

It usually stores messages about what the program did, such as:

  • normal activity
  • warnings
  • errors

For example, a program might write messages like:

  • INFO: User logged in
  • WARNING: Disk space is low
  • ERROR: Could not connect to database

Skills used in this project #

This example uses several basic Python skills:

  • Opening a file with open()
  • Looping through lines with a for loop
  • Cleaning text with strip()
  • Checking text with the in operator
  • Counting values with a dictionary or Counter
  • Printing readable output

If dictionaries are new to you, see Python dictionaries explained. If loops are new, see Python for loops explained.

Sample log file input #

Create a file named app.log in the same folder as your Python script.

Put this sample text inside it:

INFO: Application started
INFO: User login successful
WARNING: Disk space is getting low
ERROR: Could not connect to server
INFO: Retrying connection
ERROR: Connection failed again

This file contains:

  • several INFO lines
  • one WARNING line
  • two ERROR lines

Build the analyzer step by step #

1. Open the file safely #

Use with open(...) to open the file.

log_file = "app.log"

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        print(line)

Why this works:

  • open() opens the file for reading
  • "r" means read mode
  • encoding="utf-8" is a safe choice for text files
  • with closes the file automatically when finished

2. Loop over each line #

A file can be looped through directly.

log_file = "app.log"

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        print(repr(line))

Using repr(line) helps you see hidden characters like \n.

3. Remove extra newline characters with strip() #

Each line usually ends with a newline character. You can remove it with strip().

log_file = "app.log"

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()
        print(line)

This makes the line cleaner before you check its content.

4. Check whether a line contains ERROR, WARNING, or INFO #

Now check each cleaned line with the in operator.

log_file = "app.log"

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()

        if "ERROR" in line:
            print("Found an error line")
        elif "WARNING" in line:
            print("Found a warning line")
        elif "INFO" in line:
            print("Found an info line")

Why use elif here?

Because one line should only be counted once. Using elif stops Python after the first matching condition.

5. Increase the correct count #

Now let’s count each type of line.

This version uses Counter, which is a convenient counting tool.

from collections import Counter

log_file = "app.log"
counts = Counter()

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()

        if "ERROR" in line:
            counts["ERROR"] += 1
        elif "WARNING" in line:
            counts["WARNING"] += 1
        elif "INFO" in line:
            counts["INFO"] += 1

print(counts)

After reading the sample log file, counts will contain:

Counter({'INFO': 3, 'ERROR': 2, 'WARNING': 1})

6. Print the final totals #

Here is the full script:

from collections import Counter

log_file = "app.log"
counts = Counter()
total_lines = 0

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        total_lines += 1
        line = line.strip()

        if "ERROR" in line:
            counts["ERROR"] += 1
        elif "WARNING" in line:
            counts["WARNING"] += 1
        elif "INFO" in line:
            counts["INFO"] += 1

print("Log summary")
print(f"Total lines: {total_lines}")

for level in ["ERROR", "WARNING", "INFO"]:
    print(f"{level}: {counts[level]}")

Expected output #

With the sample app.log file shown earlier, the output will be:

Log summary
Total lines: 6
ERROR: 2
WARNING: 1
INFO: 3

This output is simple and easy to compare with the input file.

Useful beginner improvements #

Once the basic version works, you can try small improvements.

Count all lines, not only matched lines #

The full example already shows total_lines += 1.

This helps you compare:

  • total lines in the file
  • matched log level lines

Store matching lines in separate lists #

You can save the actual lines, not just the totals.

error_lines = []
warning_lines = []

with open("app.log", "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()

        if "ERROR" in line:
            error_lines.append(line)
        elif "WARNING" in line:
            warning_lines.append(line)

This is useful if you want to print or save the problem lines later.

Find the first error line #

You can stop as soon as the first error is found.

first_error = None

with open("app.log", "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()
        if "ERROR" in line:
            first_error = line
            break

print(first_error)

Ignore letter case with upper() #

Some files may use error, Error, or ERROR.

To handle that, convert each line to one case first:

from collections import Counter

counts = Counter()

with open("app.log", "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()
        upper_line = line.upper()

        if "ERROR" in upper_line:
            counts["ERROR"] += 1
        elif "WARNING" in upper_line:
            counts["WARNING"] += 1
        elif "INFO" in upper_line:
            counts["INFO"] += 1

print(counts)

Ask the user for the file name with input() #

You can make the script more flexible:

from collections import Counter

log_file = input("Enter the log file name: ")
counts = Counter()

with open(log_file, "r", encoding="utf-8") as file:
    for line in file:
        line = line.strip()

        if "ERROR" in line:
            counts["ERROR"] += 1
        elif "WARNING" in line:
            counts["WARNING"] += 1
        elif "INFO" in line:
            counts["INFO"] += 1

print("Log summary")
for level in ["ERROR", "WARNING", "INFO"]:
    print(f"{level}: {counts[level]}")

This page is a full example project.

It is not a complete lesson on every file or string method used here.

For deeper explanations, use these related pages:

Common mistakes #

Here are some common problems beginners run into with this kind of script.

The log file name is wrong #

If the file does not exist, Python raises a FileNotFoundError.

Example:

with open("wrong_name.log", "r", encoding="utf-8") as file:
    pass

If that happens, check:

  • the spelling of the file name
  • that the file is in the correct folder
  • your current working directory

See also: FileNotFoundError: No such file or directory fix

The path points to a folder instead of a file #

Make sure log_file contains a file path, not just a folder name.

The log levels use different letter case #

If your file contains error instead of ERROR, your checks may fail.

Fix this by converting the line first:

upper_line = line.upper()

You forget to strip newline characters #

Without strip(), the line may contain extra spaces or \n.

That can make debugging harder.

You use if for every check instead of elif #

If you use separate if statements, one line could be counted more than once in some cases.

Using elif makes the checks exclusive.

The file contains unexpected text format #

Some log files may have extra spaces, timestamps, or different wording.

If needed, print each line while testing so you can see the real format.

FAQ #

Do I need a special library to analyze a log file? #

No. A basic analyzer can use only built-in Python tools such as open(), loops, strings, and dictionaries.

Should I read the whole file at once? #

For beginners, reading line by line is usually better. It uses less memory and is easier to understand.

What if my log file uses lowercase words like error? #

Convert each line to one case first, such as line.upper(), then check for ERROR.

Can this example search for dates or usernames too? #

Yes. You can check each line for other patterns and store extra counts or matching lines.

See also #

Try extending this project next:

  • count dates in the log
  • extract all error lines
  • save the summary to a new file

Press Esc to close