Skip to content

whitemeownet/log-processor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

log-proc

log-proc is a small Go CLI tool for reading, filtering, and summarizing log files.

The project was built as a practical Go training project focused on CLI input, file processing, parsing, state management, error handling, refactoring, and testing.

Features

  • Reads log lines from a file.

  • Parses log entries into date, time, level, message, and raw line.

  • Supports log levels:

    • ERROR
    • WARN
    • INFO
    • OTHER
  • Treats unknown levels such as DEBUG, TRACE, or NOTICE as OTHER.

  • Counts:

    • total valid log lines
    • errors
    • warnings
    • info messages
    • other messages
    • invalid lines
  • Filters selected entries by log level.

  • Supports summary-only mode.

  • Supports debug output for selected parsed entries.

  • Can write selected raw log lines to an output file.

  • Includes table-driven tests for parser logic, counters, filters, and stream processing.

Project structure

log-processor/
├── cmd/
│   └── logproc/
│       ├── main.go
│       └── main_test.go
├── testdata/
│   └── sample.log
├── go.mod
└── README.md

Log format

Expected log line format:

DATE TIME LEVEL MESSAGE

Example:

2026-05-06 12:03:12 ERROR failed login attempt
2026-05-06 12:02:44 WARN high memory usage
2026-05-06 12:04:01 INFO request completed
2026-05-06 12:00:00 DEBUG cache miss

The message part is optional:

2026-05-06 12:09:44 ERROR

Invalid lines are lines with fewer than three fields:

broken line
qwe1 1

Blank lines are ignored.

Unknown levels are treated as OTHER:

2026-05-06 12:00:00 DEBUG cache miss
2026-02-03 17:41:02 TRACE request trace
2026-05-06 12:08:33 NOTICE background job finished

Usage

Run from the project root:

go run ./cmd/logproc -file testdata/sample.log

Build binary:

go build -o bin/log-proc ./cmd/logproc

Run binary:

./bin/log-proc -file testdata/sample.log

Flags

Flag Description Default
-file Path to input log file required
-level Filter logs by level: ALL, ERROR, WARN, INFO, OTHER ALL
-summary Print summary only, without printing each selected entry false
-debug Print debug slice of selected parsed entries false
-output Write selected raw log lines to a file empty

Examples

Process all valid log lines

go run ./cmd/logproc -file testdata/sample.log

This reads the file, prints selected entries, and prints a summary.

Show only errors

go run ./cmd/logproc -file testdata/sample.log -level ERROR

This counts all valid log lines internally, but prints only entries with level ERROR.

Summary only

go run ./cmd/logproc -file testdata/sample.log -summary

This skips per-entry printing and prints only the final summary.

Filter unknown levels as OTHER

go run ./cmd/logproc -file testdata/sample.log -level OTHER

This selects lines with unknown levels such as DEBUG, TRACE, NOTICE, or any level that is not ERROR, WARN, or INFO.

Write selected lines to an output file

go run ./cmd/logproc -file testdata/sample.log -level ERROR -output selected.log

This writes selected raw log lines to selected.log.

Debug selected entries

go run ./cmd/logproc -file testdata/sample.log -level ERROR -debug

This prints selected entries normally and also prints the internal debug slice after processing.

Example input

The sample input file is located at:

testdata/sample.log

Example content:

2026-05-06 12:03:12 ERROR failed login attempt
2026-05-06 12:02:44 WARN high memory usage
2026-05-06 12:04:01 INFO request completed
2026-05-06 12:00:00 DEBUG cache miss
2026-02-03 17:41:02 TRACE request trace
broken line

2026-05-06 12:05:55 ERROR database timeout
2026-05-06 12:06:10 WARN slow response detected
2026-05-06 12:07:00 INFO user session closed
2026-05-06 12:08:33 NOTICE background job finished
qwe1 1
2026-05-06 12:09:44 ERROR

Testing

Run all tests:

go test ./...

Run tests with coverage:

go test -cover ./...

Generate coverage profile:

go test -coverprofile=coverage.out ./...

Open coverage report:

go tool cover -html=coverage.out

The test suite covers:

  • mode normalization
  • level detection
  • log line parsing
  • stats counting
  • level filtering
  • processing log streams through io.Reader
  • writing selected output through io.Writer
  • output enabled / disabled behavior
  • debug collection
  • invalid lines
  • blank lines
  • unknown levels mapped to OTHER
  • valid lines with no selected matches

Implementation overview

The current implementation is organized around a few core responsibilities:

main
├── buildFlags
├── buildConfig
└── run
    ├── open input/output resources
    ├── processLines
    │   ├── scan input stream
    │   ├── parse log line
    │   ├── update stats
    │   ├── filter selected entries
    │   └── handleSelectedEntry
    └── printFinalReports

Technical focus

This project was used to practice:

  • Go CLI flags
  • file input/output
  • bufio.Scanner
  • structs
  • pointers
  • constants
  • error wrapping with fmt.Errorf
  • state management with RunState
  • separation of config, runtime state, processing, and reporting
  • io.Reader / io.Writer abstractions
  • table-driven tests
  • edge-case testing
  • step-by-step refactoring of a growing CLI program

Notes

This is a learning-oriented CLI project. The goal was not to build a production-grade log analyzer, but to practice building, refactoring, and testing a small Go program with clear runtime flow and explicit boundaries.

About

A small Go CLI tool for reading, filtering, and summarizing log files.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages