Create a gist now

Instantly share code, notes, and snippets.

Diff cleaned PHP logs (removes unnecessary parts in the logs).
/**
* Diff cleaned PHP logs (removes unnecessary parts in the logs).
*
* @author Akeda Bagus <admin@gedex.web.id>
* @license MIT
*/
package main
import (
"bufio"
"errors"
"fmt"
"os"
"os/exec"
"regexp"
"sort"
"strings"
)
var (
validLog = regexp.MustCompile(`^\[\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2} UTC\] (.*)$`)
stackTraceHeader = regexp.MustCompile(`^PHP Stack trace:(.*)$`)
stackTraceItem = regexp.MustCompile(`^PHP\s+\d+\. .*$`)
)
func main() {
if len(os.Args) != 3 {
usage()
}
before := os.Args[1]
after := os.Args[2]
diffCleanLog(before, after)
}
func diffCleanLog(before, after string) {
var beforeLogs []string
var afterLogs []string
var err error
beforeLogs, err = cleanLog(before)
if err != nil {
fmt.Printf("Error cleaning before logs: %s\n", err)
os.Exit(1)
}
sort.Strings(beforeLogs)
afterLogs, err = cleanLog(after)
if err != nil {
fmt.Printf("Error cleaning before logs: %s\n", err)
os.Exit(1)
}
sort.Strings(afterLogs)
fa := ".clean-php-log-a"
if err = cleanLogsToFile(fa, beforeLogs); err != nil {
fmt.Printf("Error creating temp file: %s\n", err)
os.Exit(1)
}
fb := ".clean-php-log-b"
if err = cleanLogsToFile(fb, afterLogs); err != nil {
fmt.Printf("Error creating temp file: %s\n", err)
os.Exit(1)
}
out, _ := exec.Command("diff", fa, fb).Output()
fmt.Println(string(out))
removeCleanLogsFile(fa)
removeCleanLogsFile(fb)
}
func cleanLogsToFile(fn string, logs []string) error {
f, err := os.Create(fn)
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteString(strings.Join(logs, "\n")); err != nil {
return err
}
return nil
}
func removeCleanLogsFile(fn string) {
os.Remove(fn)
}
func cleanLog(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
var line string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line, _ = cleanRowLog(scanner.Text())
if line != "" {
lines = append(lines, line)
}
}
return lines, scanner.Err()
}
func cleanRowLog(row string) (string, error) {
var clean string
if match := validLog.MatchString(row); !match {
return "", errors.New("Invalid log")
}
// Removes date and stack trace.
clean = validLog.ReplaceAllString(row, "$1")
clean = removeStackTrace(clean)
return clean, nil
}
func removeStackTrace(s string) string {
if match := stackTraceHeader.MatchString(s); match {
return ""
}
if match := stackTraceItem.MatchString(s); match {
return ""
}
return s
}
func usage() {
fmt.Println("diff-php-log <before> <after>\n")
os.Exit(1)
}
@gedex
Owner
gedex commented Oct 30, 2014

To build and run:

  • go build diff-php-log.go
  • diff-php-log before.log after.log
@jonathanbardo

Nice thanks Akeda!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment