1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 18:36:11 -05:00
v2fly/app/log/access.go

39 lines
916 B
Go
Raw Normal View History

2015-10-09 11:43:27 -04:00
package log
2016-01-18 06:31:27 -05:00
import (
2017-02-01 15:35:40 -05:00
"v2ray.com/core/app/log/internal"
2017-02-02 08:42:31 -05:00
"v2ray.com/core/common/errors"
2016-01-18 06:31:27 -05:00
)
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 (
2016-05-12 02:24:41 -04:00
accessLoggerInstance internal.LogWriter = new(internal.NoOpLogWriter)
2015-12-05 15:10:14 -05:00
)
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 {
2016-05-12 02:24:41 -04:00
logger, err := internal.NewFileLogWriter(file)
2015-12-05 15:10:14 -05:00
if err != nil {
2017-02-02 08:42:31 -05:00
return errors.Base(err).Message("Failed to create access logger on file: ", file)
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.
2016-05-24 16:41:51 -04:00
func Access(from, to interface{}, status AccessStatus, reason interface{}) {
2016-05-12 02:24:41 -04:00
accessLoggerInstance.Log(&internal.AccessLog{
2015-12-05 15:10:14 -05:00
From: from,
To: to,
2016-05-12 02:24:41 -04:00
Status: string(status),
2015-12-05 15:10:14 -05:00
Reason: reason,
})
2015-10-09 11:43:27 -04:00
}