logger.go (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package watchman_client import ( "fmt" "os" ) 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() } func CreateLogFile() { if _, err := os.Stat("log.json"); os.IsNotExist(err) { fmt.Println("Creating log.json file") file, _ := os.Create("log.json") defer file.Close() } else { 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 PrintLog(log LoggerProps) { fmt.Printf( "%v [%v] %v: %v\n", TimeStampToHumanReadable(log.Timestamp), log.Level, log.Subject, log.Message, ) } func Log(log LoggerProps) { PrintLog(log) WriteLog(log) } |