v2ray-core/app/log/access.go

36 lines
873 B
Go
Raw Normal View History

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