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