mirror of https://github.com/v2ray/v2ray-core
parent
b9450d8475
commit
30b5bffad4
@ -0,0 +1,51 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/log"
|
||||
)
|
||||
|
||||
type HandlerCreatorOptions struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
type HandlerCreator func(LogType, HandlerCreatorOptions) (log.Handler, error)
|
||||
|
||||
var (
|
||||
handlerCreatorMap = make(map[LogType]HandlerCreator)
|
||||
)
|
||||
|
||||
func RegisterHandlerCreator(logType LogType, f HandlerCreator) error {
|
||||
if f == nil {
|
||||
return newError("nil HandlerCreator")
|
||||
}
|
||||
|
||||
handlerCreatorMap[logType] = f
|
||||
return nil
|
||||
}
|
||||
|
||||
func createHandler(logType LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
||||
creator, found := handlerCreatorMap[logType]
|
||||
if !found {
|
||||
return nil, newError("unable to create log handler for ", logType)
|
||||
}
|
||||
return creator(logType, options)
|
||||
}
|
||||
|
||||
func init() {
|
||||
common.Must(RegisterHandlerCreator(LogType_Console, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
||||
return log.NewLogger(log.CreateStdoutLogWriter()), nil
|
||||
}))
|
||||
|
||||
common.Must(RegisterHandlerCreator(LogType_File, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
||||
creator, err := log.CreateFileLogWriter(options.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return log.NewLogger(creator), nil
|
||||
}))
|
||||
|
||||
common.Must(RegisterHandlerCreator(LogType_None, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) {
|
||||
return nil, nil
|
||||
}))
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package log_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
"v2ray.com/core/app/log"
|
||||
"v2ray.com/core/common"
|
||||
clog "v2ray.com/core/common/log"
|
||||
"v2ray.com/core/testing/mocks"
|
||||
)
|
||||
|
||||
func TestCustomLogHandler(t *testing.T) {
|
||||
mockCtl := gomock.NewController(t)
|
||||
defer mockCtl.Finish()
|
||||
|
||||
var loggedValue []string
|
||||
|
||||
mockHandler := mocks.NewLogHandler(mockCtl)
|
||||
mockHandler.EXPECT().Handle(gomock.Any()).AnyTimes().DoAndReturn(func(msg clog.Message) {
|
||||
loggedValue = append(loggedValue, msg.String())
|
||||
})
|
||||
|
||||
log.RegisterHandlerCreator(log.LogType_Console, func(lt log.LogType, options log.HandlerCreatorOptions) (clog.Handler, error) {
|
||||
return mockHandler, nil
|
||||
})
|
||||
|
||||
logger, err := log.New(context.Background(), &log.Config{
|
||||
ErrorLogLevel: clog.Severity_Debug,
|
||||
ErrorLogType: log.LogType_Console,
|
||||
AccessLogType: log.LogType_None,
|
||||
})
|
||||
common.Must(err)
|
||||
|
||||
common.Must(logger.Start())
|
||||
|
||||
clog.Record(&clog.GeneralMessage{
|
||||
Severity: clog.Severity_Debug,
|
||||
Content: "test",
|
||||
})
|
||||
|
||||
if len(loggedValue) < 2 {
|
||||
t.Fatal("expected 2 log messages, but actually ", loggedValue)
|
||||
}
|
||||
|
||||
if loggedValue[1] != "[Debug] test" {
|
||||
t.Fatal("expected '[Debug] test', but actually ", loggedValue[1])
|
||||
}
|
||||
|
||||
common.Must(logger.Close())
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: v2ray.com/core/common/log (interfaces: Handler)
|
||||
|
||||
// Package mocks is a generated GoMock package.
|
||||
package mocks
|
||||
|
||||
import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
reflect "reflect"
|
||||
log "v2ray.com/core/common/log"
|
||||
)
|
||||
|
||||
// LogHandler is a mock of Handler interface
|
||||
type LogHandler struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *LogHandlerMockRecorder
|
||||
}
|
||||
|
||||
// LogHandlerMockRecorder is the mock recorder for LogHandler
|
||||
type LogHandlerMockRecorder struct {
|
||||
mock *LogHandler
|
||||
}
|
||||
|
||||
// NewLogHandler creates a new mock instance
|
||||
func NewLogHandler(ctrl *gomock.Controller) *LogHandler {
|
||||
mock := &LogHandler{ctrl: ctrl}
|
||||
mock.recorder = &LogHandlerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use
|
||||
func (m *LogHandler) EXPECT() *LogHandlerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Handle mocks base method
|
||||
func (m *LogHandler) Handle(arg0 log.Message) {
|
||||
m.ctrl.Call(m, "Handle", arg0)
|
||||
}
|
||||
|
||||
// Handle indicates an expected call of Handle
|
||||
func (mr *LogHandlerMockRecorder) Handle(arg0 interface{}) *gomock.Call {
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Handle", reflect.TypeOf((*LogHandler)(nil).Handle), arg0)
|
||||
}
|
Loading…
Reference in new issue