access.go 999 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package log
  2. // AccessStatus is the status of an access request from clients.
  3. type AccessStatus string
  4. const (
  5. AccessAccepted = AccessStatus("accepted")
  6. AccessRejected = AccessStatus("rejected")
  7. )
  8. var (
  9. accessLoggerInstance logWriter = &noOpLogWriter{}
  10. )
  11. type accessLog struct {
  12. From string
  13. To string
  14. Status AccessStatus
  15. Reason string
  16. }
  17. func (this *accessLog) String() string {
  18. return this.From + " " + string(this.Status) + " " + this.To + " " + this.Reason
  19. }
  20. // InitAccessLogger initializes the access logger to write into the give file.
  21. func InitAccessLogger(file string) error {
  22. logger, err := newFileLogWriter(file)
  23. if err != nil {
  24. Error("Failed to create access logger on file (%s): %v", file, err)
  25. return err
  26. }
  27. accessLoggerInstance = logger
  28. return nil
  29. }
  30. // Access writes an access log.
  31. func Access(from, to string, status AccessStatus, reason string) {
  32. accessLoggerInstance.Log(&accessLog{
  33. From: from,
  34. To: to,
  35. Status: status,
  36. Reason: reason,
  37. })
  38. }