Skip to content

Commit

Permalink
Add file output type
Browse files Browse the repository at this point in the history
This fixes Issue #4. IT allows for writing to a file, if that's easier for
other software to pick up.
  • Loading branch information
FTWynn committed Jan 26, 2016
1 parent bf93583 commit 957f0ca
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ The first needed file is gologgen.conf, which stores global variables in a JSON

Conf Parameter | Notes
--------- | -----
OutputType | "http" or "syslog"
OutputType | "http", "syslog", or "file"
httpLoc | URL of the http endpoint to send logs. Supports https.
SyslogLoc | Location to send syslog traffic, in the form of IP:port
SyslogType | "tcp" or "udp"
FileOutputPath | Path of the file to write out to. Will *overwrite* whatever already exists.
DataFiles | Array of objects describing DataFiles. Only contains "Path".
ReplayFiles | Array of objects describing ReplayFiles. Contains values described below.

Expand Down
3 changes: 2 additions & 1 deletion config/gologgen.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"OutputType" : "http",
"OutputType" : "file",
"httpLoc" : "https://collectors.sumologic.com/receiver/v1/http/ZaVnC4dhaV0o8ZcEo-edSG28OScCSzOzHtojKTRId_fimMMYzbIBk1f7ciR2FE6JHXKONkhlHohT30cD1ZeCrDvvQAhMbgjjjRxEQBcn-M3sh9PRMVtt6A==",
"SyslogLoc" : "192.168.99.100:5000",
"SyslogType": "tcp",
"FileOutputPath" : "testoutput.dat",
"DataFiles" : [
{
"Path": "config/gologgen.data"
Expand Down
16 changes: 16 additions & 0 deletions loggensender/loggensender.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"net"
"net/http"
"os"
"time"

"github.com/ftwynn/gologgen/loggenmunger"
Expand All @@ -29,6 +30,7 @@ type LogLineProperties struct {
Headers []LogLineHTTPHeader `json:"Headers"`
StartTime string `json:"StartTime"`
HTTPClient *http.Client
FileHandler *os.File
}

// LogLineHTTPHeader holds the key and vlue for each header
Expand Down Expand Up @@ -87,6 +89,8 @@ func RunLogLine(params LogLineProperties, sendTime time.Time) {
go sendLogLineHTTP(params.HTTPClient, stringBody, params)
case "syslog":
go sendLogLineSyslog(stringBody, params)
case "file":
go sendLogLineFile(stringBody, params)
}
log.Info("Finished Individual Log Runner", "time", sendTime, "logline", params.PostBody)
}
Expand Down Expand Up @@ -133,3 +137,15 @@ func sendLogLineSyslog(stringBody []byte, params LogLineProperties) {

fmt.Fprintf(conn, string(stringBody))
}

//sendLogLineFile writes log lines to a file
func sendLogLineFile(stringBody []byte, params LogLineProperties) {
log.Info("Writing log to file", "line", string(stringBody))

_, err := params.FileHandler.Write(append(stringBody, []byte("\n")...))
if err != nil {
log.Error("Error writing to file", "error", err)
panic(err)
}

}
28 changes: 21 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ var log log15.Logger

// GlobalConfStore holds all the config data from the conf file
type GlobalConfStore struct {
HTTPLoc string `json:"httpLoc"`
OutputType string `json:"OutputType"`
SyslogType string `json:"SyslogType"`
SyslogLoc string `json:"SyslogLoc"`
DataFiles []DataFileMetaData `json:"DataFiles"`
ReplayFiles []ReplayFileMetaData `json:"ReplayFiles"`
HTTPClient http.Client
HTTPLoc string `json:"httpLoc"`
OutputType string `json:"OutputType"`
SyslogType string `json:"SyslogType"`
SyslogLoc string `json:"SyslogLoc"`
FileOutputPath string `json:"FileOutputPath"`
DataFiles []DataFileMetaData `json:"DataFiles"`
ReplayFiles []ReplayFileMetaData `json:"ReplayFiles"`
HTTPClient http.Client
FileHandler *os.File
}

// DataFileMetaData stores the configs around data files
Expand Down Expand Up @@ -201,6 +203,7 @@ func storeDataFileLogLines(confData GlobalConfStore) (logLines []loggensender.Lo
// Set individual log lines to global configs / defaults if need be
for i := 0; i < len(logLines); i++ {
logLines[i].HTTPClient = &confData.HTTPClient
logLines[i].FileHandler = confData.FileHandler

if logLines[i].OutputType == "" {
logLines[i].OutputType = confData.OutputType
Expand Down Expand Up @@ -246,6 +249,17 @@ func main() {
return
}

// Initialize the FileHandler if needed
if confData.OutputType == "file" {
f, err := os.Create(confData.FileOutputPath)
if err != nil {
log.Error("Error in creating the output file", "FileOutputPath", confData.FileOutputPath)
return
}
confData.FileHandler = f
defer f.Close()
}

// Create an object to store DataFile LogLines
logLines := storeDataFileLogLines(confData)

Expand Down

0 comments on commit 957f0ca

Please sign in to comment.