1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-08 19:28:21 -05:00
v2fly/common/log/access.go

46 lines
999 B
Go
Raw Normal View History

2015-10-09 11:43:27 -04:00
package log
2015-10-11 08:46:12 -04:00
// AccessStatus is the status of an access request from clients.
2015-10-09 11:43:27 -04:00
type AccessStatus string
const (
AccessAccepted = AccessStatus("accepted")
AccessRejected = AccessStatus("rejected")
)
2015-12-05 15:10:14 -05:00
var (
accessLoggerInstance logWriter = &noOpLogWriter{}
)
2015-10-09 11:43:27 -04:00
type accessLog struct {
From string
To string
Status AccessStatus
Reason string
}
2015-12-05 15:10:14 -05:00
func (this *accessLog) String() string {
return this.From + " " + string(this.Status) + " " + this.To + " " + this.Reason
2015-10-09 11:43:27 -04:00
}
2015-10-11 08:46:12 -04:00
// InitAccessLogger initializes the access logger to write into the give file.
2015-12-05 15:10:14 -05:00
func InitAccessLogger(file string) error {
logger, err := newFileLogWriter(file)
if err != nil {
Error("Failed to create access logger on file (%s): %v", file, err)
return err
2015-10-09 11:43:27 -04:00
}
2015-12-05 15:10:14 -05:00
accessLoggerInstance = logger
return nil
2015-10-09 11:43:27 -04:00
}
2015-10-11 08:46:12 -04:00
// Access writes an access log.
2015-10-09 11:43:27 -04:00
func Access(from, to string, status AccessStatus, reason string) {
2015-12-05 15:10:14 -05:00
accessLoggerInstance.Log(&accessLog{
From: from,
To: to,
Status: status,
Reason: reason,
})
2015-10-09 11:43:27 -04:00
}