-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"log" | ||
"math/rand" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
var logfile = "test/logfile.log" | ||
var sleeptime = 1000 * time.Millisecond | ||
|
||
var lipsumwords = []string{"a", "ac", "accumsan", "ad", "adipiscing", "aenean", "aliquam", "aliquet", | ||
"amet", "ante", "aptent", "arcu", "at", "auctor", "augue", "bibendum", | ||
"blandit", "class", "commodo", "condimentum", "congue", "consectetur", | ||
"consequat", "conubia", "convallis", "cras", "cubilia", "cum", "curabitur", | ||
"curae", "cursus", "dapibus", "diam", "dictum", "dictumst", "dignissim", | ||
"dis", "dolor", "donec", "dui", "duis", "egestas", "eget", "eleifend", | ||
"elementum", "elit", "enim", "erat", "eros", "est", "et", "etiam", "eu", | ||
"euismod", "facilisi", "facilisis", "fames", "faucibus", "felis", | ||
"fermentum", "feugiat", "fringilla", "fusce", "gravida", "habitant", | ||
"habitasse", "hac", "hendrerit", "himenaeos", "iaculis", "id", "imperdiet", | ||
"in", "inceptos", "integer", "interdum", "ipsum", "justo", "lacinia", | ||
"lacus", "laoreet", "lectus", "leo", "libero", "ligula", "litora", | ||
"lobortis", "lorem", "luctus", "maecenas", "magna", "magnis", "malesuada", | ||
"massa", "mattis", "mauris", "metus", "mi", "molestie", "mollis", "montes", | ||
"morbi", "mus", "nam", "nascetur", "natoque", "nec", "neque", "netus", | ||
"nibh", "nisi", "nisl", "non", "nostra", "nulla", "nullam", "nunc", "odio", | ||
"orci", "ornare", "parturient", "pellentesque", "penatibus", "per", | ||
"pharetra", "phasellus", "placerat", "platea", "porta", "porttitor", | ||
"posuere", "potenti", "praesent", "pretium", "primis", "proin", "pulvinar", | ||
"purus", "quam", "quis", "quisque", "rhoncus", "ridiculus", "risus", | ||
"rutrum", "sagittis", "sapien", "scelerisque", "sed", "sem", "semper", | ||
"senectus", "sit", "sociis", "sociosqu", "sodales", "sollicitudin", | ||
"suscipit", "suspendisse", "taciti", "tellus", "tempor", "tempus", | ||
"tincidunt", "torquent", "tortor", "tristique", "turpis", "ullamcorper", | ||
"ultrices", "ultricies", "urna", "ut", "varius", "vehicula", "vel", "velit", | ||
"venenatis", "vestibulum", "vitae", "vivamus", "viverra", "volutpat", | ||
"vulputate"} | ||
|
||
var punctuation = []string{".", "?", "!"} | ||
var severity = []string{"ERROR", "WARN", "INFO", "DEBUG", "TRACE"} | ||
|
||
func main() { | ||
|
||
rand.Seed(time.Now().Unix()) | ||
file, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
|
||
if err != nil { | ||
log.Fatalf("failed creating file: %s", err) | ||
} | ||
|
||
c := make(chan os.Signal) | ||
signal.Notify(c, os.Interrupt, syscall.SIGTERM) | ||
go func() { | ||
<-c | ||
fmt.Println("\rCtrl+C pressed in Terminal, closing file...") | ||
file.Close() | ||
fmt.Println("\rGoodbye!") | ||
os.Exit(1) | ||
}() | ||
|
||
datawriter := bufio.NewWriter(file) | ||
|
||
i := 0 | ||
for { | ||
datawriter.WriteString(fmt.Sprint(i, ": (", time.Now().Format(time.RFC3339), ") [", randomSeverity(), "] ", paragraph(), "\n")) | ||
datawriter.Flush() | ||
time.Sleep(sleeptime) | ||
i++ | ||
} | ||
} | ||
|
||
func randomSeverity() string { | ||
return severity[rand.Intn(len(severity))] | ||
} | ||
|
||
func randomWord() string { | ||
return lipsumwords[rand.Intn(len(lipsumwords))] | ||
} | ||
|
||
func randomPunctuation() string { | ||
return punctuation[rand.Intn(len(punctuation))] | ||
} | ||
|
||
func words(count int) string { | ||
if count > 0 { | ||
return strings.TrimSpace(randomWord() + " " + words(count-1)) | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
func sentenceFragment() string { | ||
return words(rand.Intn(10) + 3) | ||
} | ||
|
||
func sentence() string { | ||
s := strings.Title(randomWord()) + " " | ||
if rand.Intn(2) == 0 { | ||
for i := 0; i < rand.Intn(3); i++ { | ||
s += sentenceFragment() + ", " | ||
} | ||
} | ||
return sentenceFragment() + randomPunctuation() | ||
} | ||
|
||
func sentences(count int) string { | ||
if count > 0 { | ||
return sentence() + " " + strings.TrimSpace(sentences(count-1)) | ||
} else { | ||
return "" | ||
} | ||
} | ||
|
||
func paragraph() string { | ||
return sentences(rand.Intn(3) + 2) | ||
} |