trpc.group/trpc-go/trpc-go@v1.0.3/log/writer_factory.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package log 15 16 import ( 17 "errors" 18 "path/filepath" 19 20 "trpc.group/trpc-go/trpc-go/plugin" 21 ) 22 23 var ( 24 // DefaultConsoleWriterFactory is the default console output implementation. 25 DefaultConsoleWriterFactory = &ConsoleWriterFactory{} 26 // DefaultFileWriterFactory is the default file output implementation. 27 DefaultFileWriterFactory = &FileWriterFactory{} 28 29 writers = make(map[string]plugin.Factory) 30 ) 31 32 // RegisterWriter registers log output writer. Writer may have multiple implementations. 33 func RegisterWriter(name string, writer plugin.Factory) { 34 writers[name] = writer 35 } 36 37 // GetWriter gets log output writer, returns nil if not exist. 38 func GetWriter(name string) plugin.Factory { 39 return writers[name] 40 } 41 42 // ConsoleWriterFactory is the console writer instance. 43 type ConsoleWriterFactory struct { 44 } 45 46 // Type returns the log plugin type. 47 func (f *ConsoleWriterFactory) Type() string { 48 return pluginType 49 } 50 51 // Setup starts, loads and registers console output writer. 52 func (f *ConsoleWriterFactory) Setup(name string, dec plugin.Decoder) error { 53 if dec == nil { 54 return errors.New("console writer decoder empty") 55 } 56 decoder, ok := dec.(*Decoder) 57 if !ok { 58 return errors.New("console writer log decoder type invalid") 59 } 60 cfg := &OutputConfig{} 61 if err := decoder.Decode(&cfg); err != nil { 62 return err 63 } 64 decoder.Core, decoder.ZapLevel = newConsoleCore(cfg) 65 return nil 66 } 67 68 // FileWriterFactory is the file writer instance Factory. 69 type FileWriterFactory struct { 70 } 71 72 // Type returns log file type. 73 func (f *FileWriterFactory) Type() string { 74 return pluginType 75 } 76 77 // Setup starts, loads and register file output writer. 78 func (f *FileWriterFactory) Setup(name string, dec plugin.Decoder) error { 79 if dec == nil { 80 return errors.New("file writer decoder empty") 81 } 82 decoder, ok := dec.(*Decoder) 83 if !ok { 84 return errors.New("file writer log decoder type invalid") 85 } 86 if err := f.setupConfig(decoder); err != nil { 87 return err 88 } 89 return nil 90 } 91 92 func (f *FileWriterFactory) setupConfig(decoder *Decoder) error { 93 cfg := &OutputConfig{} 94 if err := decoder.Decode(&cfg); err != nil { 95 return err 96 } 97 if cfg.WriteConfig.LogPath != "" { 98 cfg.WriteConfig.Filename = filepath.Join(cfg.WriteConfig.LogPath, cfg.WriteConfig.Filename) 99 } 100 if cfg.WriteConfig.RollType == "" { 101 cfg.WriteConfig.RollType = RollBySize 102 } 103 104 core, level, err := newFileCore(cfg) 105 if err != nil { 106 return err 107 } 108 decoder.Core, decoder.ZapLevel = core, level 109 return nil 110 }