commit af7086bbbfdf552743580369403b2258f3a6e9f0
parent 7d665fa3ab379622ebf88d446fbdd715faaa7393
Author: MTRNord <mtrnord1@gmail.com>
Date: Sat, 9 Mar 2024 16:39:51 +0100
Improve logging
Diffstat:
3 files changed, 24 insertions(+), 13 deletions(-)
diff --git a/edsm/edsm.go b/edsm/edsm.go
@@ -4,6 +4,8 @@ import (
"bytes"
"encoding/json"
"io"
+ "log"
+ "os"
"github.com/MTRNord/edsm_uploader/datatypes"
"github.com/hashicorp/go-retryablehttp"
@@ -14,14 +16,17 @@ type EDSM struct {
commanderName string
apiKey string
client *retryablehttp.Client
+ logger *log.Logger
}
-func NewEDSM(commanderName string, apiKey string) *EDSM {
+func NewEDSM(commanderName string, apiKey string, logger *log.Logger) *EDSM {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 10
+ retryClient.Logger = log.New(os.Stderr, "retryablehttp: ", log.LstdFlags)
return &EDSM{
commanderName: commanderName,
apiKey: apiKey,
+ logger: logger,
client: retryClient,
}
}
@@ -80,7 +85,7 @@ func (e *EDSM) SendJournalLine(fileHeader *datatypes.FileHeader, journalLine str
return errors.WithStack(err)
}
bodyString := string(bodyBytes)
- log.Printf("Response from EDSM: %s", bodyString)
+ e.logger.Printf("Response from EDSM: %s", bodyString)
} */
return nil
diff --git a/journal/journal.go b/journal/journal.go
@@ -6,6 +6,7 @@ import (
"encoding/json"
"log"
"os"
+ "strings"
"sync"
"time"
@@ -24,13 +25,14 @@ type Journal struct {
edsm *edsm.EDSM
lastDate *time.Time
fileHeader *datatypes.FileHeader
+ logger *log.Logger
}
-func NewJournal(edsm *edsm.EDSM) *Journal {
+func NewJournal(edsm *edsm.EDSM, logger *log.Logger) *Journal {
// Read lastDate from latest.txt
file, err := os.OpenFile("latest.txt", os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
- log.Printf("Error opening latest.txt: %s", err)
+ logger.Printf("Error opening latest.txt: %s", err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
@@ -44,7 +46,7 @@ func NewJournal(edsm *edsm.EDSM) *Journal {
} else {
lastDateL, err := time.Parse(time.RFC3339, text)
if err != nil {
- log.Printf("Error parsing latest.txt: %s", err)
+ logger.Printf("Error parsing latest.txt: %s", err)
}
lastDate = &lastDateL
}
@@ -56,11 +58,13 @@ func NewJournal(edsm *edsm.EDSM) *Journal {
edsm: edsm,
lastDate: lastDate,
fileHeader: nil,
+ logger: logger,
}
}
func (j *Journal) ParseJournal(journalPath string) error {
- log.Printf("Parsing journal file: %s", journalPath)
+ splitPath := strings.Split(journalPath, "/")
+ j.logger.Printf("Parsing journal file: %s", splitPath[len(splitPath)-1])
file, err := os.Open(journalPath)
if err != nil {
return errors.WithStack(err)
@@ -101,12 +105,12 @@ func (j *Journal) storeLastDate(timestamp time.Time) {
j.lastDate = ×tamp
file, err := os.OpenFile("latest.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
- log.Printf("Error opening latest.txt: %s", err)
+ j.logger.Printf("Error opening latest.txt: %s", err)
}
defer file.Close()
_, err = file.WriteString(timestamp.Format(time.RFC3339))
if err != nil {
- log.Printf("Error writing to latest.txt: %s", err)
+ j.logger.Printf("Error writing to latest.txt: %s", err)
}
}
}
diff --git a/main.go b/main.go
@@ -19,12 +19,14 @@ func main() {
// Take as a second positional argument the commander name.
// Take as a third positional argument the EDSM API key.
+ logger := log.New(os.Stderr, "edsm_uploader: ", log.LstdFlags)
+
jounnalPath := os.Args[1]
commanderName := os.Args[2]
apiKey := os.Args[3]
// Create a new EDSM object.
- edsm := edsm.NewEDSM(commanderName, apiKey)
+ edsm := edsm.NewEDSM(commanderName, apiKey, logger)
// Find all the journal files in the folder and parse them after sorting them by date.
files := make(map[string]string)
@@ -48,7 +50,7 @@ func main() {
return nil
})
if err != nil {
- log.Fatalf("Error walking the path: %+v", err)
+ logger.Fatalf("Error walking the path: %+v", err)
}
// Sort the files by date.
@@ -58,16 +60,16 @@ func main() {
}
sort.Strings(keys)
- journal_obj := journal.NewJournal(edsm)
+ journal_obj := journal.NewJournal(edsm, logger)
for _, k := range keys {
// Parse the journal file.
err := journal_obj.ParseJournal(files[k])
if err != nil {
- log.Fatalf("Error parsing journal file: %+v", err)
+ logger.Fatalf("Error parsing journal file: %+v", err)
}
// sleep for 1 second to not overload the EDSM API.
time.Sleep(1 * time.Second)
}
- log.Println("Done parsing journal files.")
+ logger.Println("Done parsing journal files.")
}