forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from WoSai/hotfix/v2.4.2
support external config file
- Loading branch information
Showing
7 changed files
with
115 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package ultron | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/jacexh/multiconfig" | ||
) | ||
|
||
type ( | ||
Option struct { | ||
Server ServerOption | ||
Logger LoggerOption | ||
} | ||
|
||
ServerOption struct { | ||
HTTPAddr string `default:":2017" yaml:"http_addr,omitempty" json:"http_addr,omitempty" toml:"http_addr"` | ||
GRPCAddr string `default:":2021" yaml:"grpc_addr,omitempty" json:"grpc_addr,omitempty" toml:"grpc_addr"` | ||
} | ||
|
||
LoggerOption struct { | ||
Level string `default:"info" yaml:"level,omitempty" json:"level,omitempty" toml:"level"` | ||
FileName string `yaml:"filename,omitempty" json:"filename,omitempty" toml:"filename"` | ||
MaxSize int `default:"100" yaml:"max_size,omitempty" json:"max_size,omitempty" toml:"max_size"` | ||
MaxBackups int `default:"30" yaml:"max_backups,omitempty" json:"max_backups,omitempty" toml:"max_backups"` | ||
} | ||
) | ||
|
||
var ( | ||
configFileBaseName = "config" | ||
configFileFormat = "yml" | ||
searchInPaths []string = []string{"."} | ||
) | ||
|
||
var ( | ||
loadedOption *Option | ||
) | ||
|
||
func findInDir(dir string, file string) string { | ||
fp := filepath.Join(dir, file) | ||
fi, err := os.Stat(fp) | ||
if err == nil && !fi.IsDir() { | ||
return fp | ||
} | ||
return "" | ||
} | ||
|
||
func findConfigFile() string { | ||
fp := fmt.Sprintf("%s.%s", configFileBaseName, configFileFormat) | ||
for _, dir := range searchInPaths { | ||
if path := findInDir(dir, fp); path != "" { | ||
return path | ||
} | ||
} | ||
return "" | ||
} | ||
|
||
func loadConfig() *Option { | ||
f := findConfigFile() | ||
opt := new(Option) | ||
loader := multiconfig.NewWithPathAndEnvPrefix(f, "ULTRON") | ||
loader.MustLoad(opt) | ||
|
||
buildLogger(opt.Logger) // todo | ||
return opt | ||
} | ||
|
||
func init() { | ||
loadedOption = loadConfig() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
logger: | ||
level: "debug" | ||
filename: "ultron.log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,40 @@ | ||
package ultron | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/jacexh/gopkg/zaprotate" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
var ( | ||
// Logger 全局日志 | ||
Logger *zap.Logger | ||
) | ||
|
||
func buildLogger() { | ||
cfg := zap.NewProductionConfig() | ||
// cfg.Encoding = "console" | ||
cfg.EncoderConfig.TimeKey = "@timestamp" | ||
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
cfg.Sampling = nil | ||
|
||
var err error | ||
Logger, err = cfg.Build() //zap.AddCallerSkip(1) | ||
if err != nil { | ||
panic(err) | ||
levelMapper = map[string]zapcore.Level{ | ||
"info": zapcore.InfoLevel, | ||
"debug": zapcore.DebugLevel, | ||
"warn": zapcore.WarnLevel, | ||
"error": zapcore.ErrorLevel, | ||
} | ||
} | ||
|
||
func init() { | ||
buildLogger() | ||
once sync.Once | ||
) | ||
|
||
func buildLogger(opt LoggerOption) { | ||
once.Do(func() { | ||
cfg := zap.NewProductionConfig() | ||
cfg.EncoderConfig.TimeKey = "@timestamp" | ||
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder | ||
cfg.Sampling = nil | ||
cfg.Level = zap.NewAtomicLevelAt(levelMapper[opt.Level]) | ||
|
||
Logger = zaprotate.BuildRotateLogger(cfg, zaprotate.RotatingFileConfig{ | ||
LoggerName: "", | ||
Filename: opt.FileName, | ||
MaxSize: opt.MaxSize, | ||
MaxBackups: opt.MaxBackups, | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters