Skip to content

Instantly share code, notes, and snippets.

@rusco
Last active December 9, 2015 16:54
Show Gist options
  • Save rusco/37108a9765b0214f7043 to your computer and use it in GitHub Desktop.
Save rusco/37108a9765b0214f7043 to your computer and use it in GitHub Desktop.
Logfile consolidation (in memory sort)
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"time"
)
func trace(s string) (string, time.Time) {
log.Println("START:", s)
return s, time.Now()
}
func un(s string, startTime time.Time) {
endTime := time.Now()
log.Println(" END:", s, "ElapsedTime in seconds:", endTime.Sub(startTime))
}
type ByLength []string
func (s ByLength) Len() int {
return len(s)
}
func (s ByLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByLength) Less(i, j int) bool {
return s[i][28:] < s[j][28:]
}
var arr []string
func main() {
defer un(trace("logconsolidator."))
arr = make([]string, 0)
files, _ := filepath.Glob("*.log")
for _, filename := range files {
inFile, _ := os.Open(filename)
defer inFile.Close()
scanner := bufio.NewScanner(inFile)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
arr = append(arr, filename+line)
}
}
sort.Sort(ByLength(arr))
for _, v := range arr {
fmt.Println(v)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment