logger.go 583 B

1234567891011121314151617181920212223242526272829303132
  1. package main
  2. import (
  3. "encoding/csv"
  4. "log"
  5. "os"
  6. "path/filepath"
  7. "time"
  8. )
  9. const (
  10. dateFormat = "20060102"
  11. )
  12. func getWriter() *csv.Writer {
  13. logFile := filepath.Join("log", (time.Now().Format(dateFormat) + ".log"))
  14. logOutput, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  15. if err != nil {
  16. log.Fatalf("Error creating log file: %v", err)
  17. }
  18. writer := csv.NewWriter(logOutput)
  19. return writer
  20. }
  21. // WriteLog main function, saves event to the log.
  22. func WriteLog(info SearchInfo) {
  23. writer := getWriter()
  24. writer.Write(info.ToArray())
  25. writer.Flush()
  26. }