v2ray-core/common/log/access.go

39 lines
892 B
Go
Raw Normal View History

2015-10-09 17:43:27 +02:00
package log
2016-01-18 12:31:27 +01:00
import (
2016-08-20 20:55:45 +02:00
"v2ray.com/core/common/log/internal"
2016-01-18 12:31:27 +01:00
)
2015-10-11 14:46:12 +02:00
// AccessStatus is the status of an access request from clients.
2015-10-09 17:43:27 +02:00
type AccessStatus string
const (
AccessAccepted = AccessStatus("accepted")
AccessRejected = AccessStatus("rejected")
)
2015-12-05 21:10:14 +01:00
var (
2016-05-11 23:24:41 -07:00
accessLoggerInstance internal.LogWriter = new(internal.NoOpLogWriter)
2015-12-05 21:10:14 +01:00
)
2015-10-09 17:43:27 +02:00
2015-10-11 14:46:12 +02:00
// InitAccessLogger initializes the access logger to write into the give file.
2015-12-05 21:10:14 +01:00
func InitAccessLogger(file string) error {
2016-05-11 23:24:41 -07:00
logger, err := internal.NewFileLogWriter(file)
2015-12-05 21:10:14 +01:00
if err != nil {
2016-01-18 12:24:33 +01:00
Error("Failed to create access logger on file (", file, "): ", file, err)
2015-12-05 21:10:14 +01:00
return err
2015-10-09 17:43:27 +02:00
}
2015-12-05 21:10:14 +01:00
accessLoggerInstance = logger
return nil
2015-10-09 17:43:27 +02:00
}
2015-10-11 14:46:12 +02:00
// Access writes an access log.
2016-05-24 22:41:51 +02:00
func Access(from, to interface{}, status AccessStatus, reason interface{}) {
2016-05-11 23:24:41 -07:00
accessLoggerInstance.Log(&internal.AccessLog{
2015-12-05 21:10:14 +01:00
From: from,
To: to,
2016-05-11 23:24:41 -07:00
Status: string(status),
2015-12-05 21:10:14 +01:00
Reason: reason,
})
2015-10-09 17:43:27 +02:00
}