github.com/v2fly/v2ray-core/v5@v5.16.2-0.20240507031116-8191faa6e095/app/log/log_creator.go (about) 1 package log 2 3 import ( 4 "github.com/v2fly/v2ray-core/v5/common" 5 "github.com/v2fly/v2ray-core/v5/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 handlerCreatorMap = make(map[LogType]HandlerCreator) 15 16 func RegisterHandlerCreator(logType LogType, f HandlerCreator) error { 17 if f == nil { 18 return newError("nil HandlerCreator") 19 } 20 21 handlerCreatorMap[logType] = f 22 return nil 23 } 24 25 func createHandler(logType LogType, options HandlerCreatorOptions) (log.Handler, error) { 26 creator, found := handlerCreatorMap[logType] 27 if !found { 28 return nil, newError("unable to create log handler for ", logType) 29 } 30 return creator(logType, options) 31 } 32 33 func init() { 34 common.Must(RegisterHandlerCreator(LogType_Console, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 35 return log.NewLogger(log.CreateStdoutLogWriter()), nil 36 })) 37 38 common.Must(RegisterHandlerCreator(LogType_File, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 39 creator, err := log.CreateFileLogWriter(options.Path) 40 if err != nil { 41 return nil, err 42 } 43 return log.NewLogger(creator), nil 44 })) 45 46 common.Must(RegisterHandlerCreator(LogType_None, func(lt LogType, options HandlerCreatorOptions) (log.Handler, error) { 47 return nil, nil 48 })) 49 }