feat(log file): read and write from log file Wrote logic for writing logs to log.json and reading them back. Also re-organised code.
Brijesh ops@brijesh.dev
Sun, 07 Jul 2024 13:11:42 +0530
9 files changed,
125 insertions(+),
84 deletions(-)
D
async.go
@@ -1,27 +0,0 @@
-package watchman_client - -import ( - "fmt" - "os" - "time" -) - -func BackgroundLogSync(seconds int) { - ticker := time.NewTicker(time.Duration(seconds) * time.Second) - defer ticker.Stop() - - for range ticker.C { - if IsLogFileEmpty() { - fmt.Println("Log file is empty") - } else { - fmt.Println("Log file is not empty, but sync function is not built yet") - } - } -} - -func IsLogFileEmpty() bool { - file, _ := os.Open("log.json") - defer file.Close() - fileInfo, _ := file.Stat() - return fileInfo.Size() == 0 -}
M
logger.go
→
logger.go
@@ -3,22 +3,13 @@
import ( "fmt" "os" -) + "time" -type LoggerProps struct { - UUID string `json:"uuid"` - Level string `json:"level"` - Message string `json:"message"` - Subject string `json:"subject"` - SyncStatus string `json:"sync_status"` - Timestamp int64 `json:"timestamp"` -} - -func LoggerInit() { - CreateLogFile() -} + "github.com/google/uuid" + "github.com/wbrijesh/watchman_client/utils" +) -func CreateLogFile() { +func InitialiseWatchman() { if _, err := os.Stat("log.json"); os.IsNotExist(err) { fmt.Println("Creating log.json file") file, _ := os.Create("log.json")@@ -28,25 +19,24 @@ fmt.Println("log.json file already exists")
} } -func WriteLog(log LoggerProps) { - file, _ := os.OpenFile("log.json", os.O_APPEND|os.O_WRONLY, 0644) - defer file.Close() - - jsonStyleString := fmt.Sprintf("{\"uuid\": \"%v\", \"level\": \"%v\", \"message\": \"%v\", \"subject\": \"%v\", \"sync_status\": \"%v\", \"timestamp\": %v}", log.UUID, log.Level, log.Message, log.Subject, log.SyncStatus, log.Timestamp) - fmt.Fprintf(file, "%v\n", jsonStyleString) +func GenerateUUID() string { + return uuid.New().String() } -func PrintLog(log LoggerProps) { - fmt.Printf( - "%v [%v] %v: %v\n", - TimeStampToHumanReadable(log.Timestamp), - log.Level, - log.Subject, - log.Message, - ) +func Log(log utils.LoggerProps) { + utils.PrintLog(log) + utils.WriteLog(log) } -func Log(log LoggerProps) { - PrintLog(log) - WriteLog(log) +func BackgroundLogSync(seconds int) { + ticker := time.NewTicker(time.Duration(seconds) * time.Second) + defer ticker.Stop() + + for range ticker.C { + if utils.IsLogFileEmpty() { + fmt.Println("Log file is empty") + } else { + utils.SendLogsToServer() + } + } }
D
main.go
@@ -1,7 +0,0 @@
-package watchman_client - -import "fmt" - -func ExampleFunction() { - fmt.Println("hello") -}
D
utils.go
@@ -1,18 +0,0 @@
-package watchman_client - -import ( - "time" - - "github.com/google/uuid" -) - -func GenerateUUID() string { - return uuid.New().String() -} - -func TimeStampToHumanReadable(timestamp int64) string { - t := time.Unix(timestamp, 0) - dateFormat := "2006-01-02 15:04:05" - - return t.Format(dateFormat) -}
A
utils/json_utils.go
@@ -0,0 +1,55 @@
+package utils + +import ( + "bufio" + "encoding/json" + "fmt" + "os" + "time" +) + +func WriteLog(log LoggerProps) { + file, _ := os.OpenFile("log.json", os.O_APPEND|os.O_WRONLY, 0644) + defer file.Close() + + jsonBytes, err := json.Marshal(log) + if err != nil { + fmt.Println("Error marshalling to JSON:", err) + return + } + jsonString := string(jsonBytes) + + fmt.Fprintf(file, "%v\n", jsonString) +} + +func ReadLogs() ([]LoggerProps, error) { + file, err := os.Open("log.json") + if err != nil { + return nil, err + } + defer file.Close() + + var logs []LoggerProps + scanner := bufio.NewScanner(file) + for scanner.Scan() { + var log LoggerProps + err := json.Unmarshal(scanner.Bytes(), &log) + if err != nil { + return nil, err + } + logs = append(logs, log) + } + + if err := scanner.Err(); err != nil { + return nil, err + } + + return logs, nil +} + +func TimeStampToHumanReadable(timestamp int64) string { + t := time.Unix(timestamp, 0) + dateFormat := "2006-01-02 15:04:05" + + return t.Format(dateFormat) +}
A
utils/log_utils.go
@@ -0,0 +1,23 @@
+package utils + +import ( + "fmt" + "os" +) + +func IsLogFileEmpty() bool { + file, _ := os.Open("log.json") + defer file.Close() + fileInfo, _ := file.Stat() + return fileInfo.Size() == 0 +} + +func PrintLog(log LoggerProps) { + fmt.Printf( + "%v [%v] %v: %v\n", + TimeStampToHumanReadable(log.Timestamp), + log.Level, + log.Subject, + log.Message, + ) +}
A
utils/send_logs.go
@@ -0,0 +1,15 @@
+package utils + +import "fmt" + +func SendLogsToServer() { + logs, err := ReadLogs() + if err != nil { + fmt.Println("Error reading logs:", err) + return + } + + for _, log := range logs { + fmt.Println(log) + } +}
A
utils/types.go
@@ -0,0 +1,10 @@
+package utils + +type LoggerProps struct { + UUID string `json:"uuid"` + Level string `json:"level"` + Message string `json:"message"` + Subject string `json:"subject"` + SyncStatus string `json:"sync_status"` + Timestamp int64 `json:"timestamp"` +}