github.com/eagleql/xray-core@v1.4.4/app/log/log_creator.go (about) 1 package log 2 3 import ( 4 "github.com/eagleql/xray-core/common" 5 "github.com/eagleql/xray-core/common/log" 6 ) 7 8 type HandlerCreatorOptions struct { 9 Path string 10 } 11 12 type HandlerCreator func(LogType, HandlerCreatorOptions) (log.Handler, error) 13 14 var ( 15 handlerCreatorMap = make(map[LogType]HandlerCreator) 16 ) 17 18 func RegisterHandlerCreator(logType LogType, f HandlerCreator) error { 19 if f == nil { 20 return newError("nil HandlerCreator") 21 } 22 23 handlerCreatorMap[logType] = f 24 return nil 25 } 26 27 func createHandler(logType LogType, options HandlerCreatorOptions) (log.Handler, error) { 28 creator, found := handlerCreatorMap[logType] 29 if !found { 30 return nil, newError("unable to create log handler for ", logType) 31 } 32 return creator(logType, options) 33 } 34 35 func init() { 36 common.Must(RegisterHandlerCreator(LogType_Console, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 37 return log.NewLogger(log.CreateStdoutLogWriter()), nil 38 })) 39 40 common.Must(RegisterHandlerCreator(LogType_File, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 41 creator, err := log.CreateFileLogWriter(options.Path) 42 if err != nil { 43 return nil, err 44 } 45 return log.NewLogger(creator), nil 46 })) 47 48 common.Must(RegisterHandlerCreator(LogType_None, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 49 return nil, nil 50 })) 51 }