From d9e6250d08aa12e1f84e4236b1994e2ecbae7679 Mon Sep 17 00:00:00 2001 From: youngsterxyf Date: Wed, 27 Jan 2016 00:10:21 +0800 Subject: [PATCH] fix config logic --- beego.go | 12 +-------- config.go | 80 +++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 64 insertions(+), 28 deletions(-) diff --git a/beego.go b/beego.go index 7aa6b221..68a76e52 100644 --- a/beego.go +++ b/beego.go @@ -15,7 +15,6 @@ package beego import ( - "fmt" "os" "path/filepath" "strconv" @@ -76,15 +75,6 @@ func initBeforeHTTPRun() { } */ var err error - //init log - for adaptor, config := range BConfig.Log.Outputs { - err = BeeLogger.SetLogger(adaptor, config) - if err != nil { - fmt.Printf("%s with the config `%s` got err:%s\n", adaptor, config, err) - } - } - - SetLogFuncCall(BConfig.Log.FileLineNum) //init hooks AddAPPStartHook(registerMime) @@ -95,7 +85,7 @@ func initBeforeHTTPRun() { AddAPPStartHook(registerAdmin) for _, hk := range hooks { - if err := hk(); err != nil { + if err = hk(); err != nil { panic(err) } } diff --git a/config.go b/config.go index e91ca28b..246c53f0 100644 --- a/config.go +++ b/config.go @@ -19,6 +19,7 @@ import ( "os" "path/filepath" "strings" + "fmt" "github.com/astaxie/beego/config" "github.com/astaxie/beego/session" @@ -103,14 +104,15 @@ var ( BConfig *Config // AppConfig is the instance of Config, store the config information from file AppConfig *beegoAppConfig - // AppConfigPath is the path to the config files - AppConfigPath string - // AppConfigProvider is the provider for the config, default is ini - AppConfigProvider = "ini" // TemplateCache stores template caching TemplateCache map[string]*template.Template // GlobalSessions is the instance for the session manager GlobalSessions *session.Manager + + // AppConfigPath is the path to the config files + AppConfigPath string + // AppConfigProvider is the provider for the config, default is ini + AppConfigProvider = "ini" ) func init() { @@ -173,22 +175,26 @@ func init() { Outputs: map[string]string{"console": ""}, }, } - ParseConfig() + + AppConfigPath = getDefaultAppConfigPath() + + if !utils.FileExists(AppConfigPath) { + AppConfig = &beegoAppConfig{config.NewFakeConfig()} + return + } + + parseConfig(AppConfigPath) +} + +func getDefaultAppConfigPath() string { + // default config path + AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0])) + return filepath.Join(AppPath, "conf", "app.conf") } -// ParseConfig parsed default config file. // now only support ini, next will support json. -func ParseConfig() (err error) { - if AppConfigPath == "" { - // initialize default configurations - AppPath, _ := filepath.Abs(filepath.Dir(os.Args[0])) - AppConfigPath = filepath.Join(AppPath, "conf", "app.conf") - if !utils.FileExists(AppConfigPath) { - AppConfig = &beegoAppConfig{config.NewFakeConfig()} - return - } - } - AppConfig, err = newAppConfig(AppConfigProvider, AppConfigPath) +func parseConfig(appConfigPath string) (err error) { + AppConfig, err = newAppConfig(AppConfigProvider, appConfigPath) if err != nil { return err } @@ -242,6 +248,8 @@ func ParseConfig() (err error) { BConfig.WebConfig.Session.SessionCookieLifeTime = AppConfig.DefaultInt("SessionCookieLifeTime", BConfig.WebConfig.Session.SessionCookieLifeTime) BConfig.WebConfig.Session.SessionAutoSetCookie = AppConfig.DefaultBool("SessionAutoSetCookie", BConfig.WebConfig.Session.SessionAutoSetCookie) BConfig.WebConfig.Session.SessionDomain = AppConfig.DefaultString("SessionDomain", BConfig.WebConfig.Session.SessionDomain) + BConfig.Log.AccessLogs = AppConfig.DefaultBool("LogAccessLogs", BConfig.Log.AccessLogs) + BConfig.Log.FileLineNum = AppConfig.DefaultBool("LogFileLineNum", BConfig.Log.FileLineNum) if sd := AppConfig.String("StaticDir"); sd != "" { for k := range BConfig.WebConfig.StaticDir { @@ -274,9 +282,47 @@ func ParseConfig() (err error) { BConfig.WebConfig.StaticExtensionsToGzip = fileExts } } + + if lo := AppConfig.String("LogOutputs"); lo != "" { + los := strings.Split(lo, ";") + for _, v := range los { + if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 { + BConfig.Log.Outputs[logType2Config[0]] = logType2Config[1] + } else { + continue + } + } + } + + //init log + BeeLogger.Close() + for adaptor, config := range BConfig.Log.Outputs { + err = BeeLogger.SetLogger(adaptor, config) + if err != nil { + fmt.Printf("%s with the config `%s` got err:%s\n", adaptor, config, err) + } + } + SetLogFuncCall(BConfig.Log.FileLineNum) + return nil } +// LoadAppConfig allow developer to apply a config file +func LoadAppConfig(configPath string, adapterName string) error { + absConfigPath, err := filepath.Abs(configPath) + if err != nil { + return err + } + if absConfigPath == AppConfigPath { + return nil + } + + AppConfigPath = absConfigPath + AppConfigProvider = adapterName + + return parseConfig(AppConfigPath) +} + type beegoAppConfig struct { innerConfig config.Configer }