mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 22:31:28 +00:00
Merge pull request #1652 from JessonChan/develop
colorful console and go fmt
This commit is contained in:
commit
6eaa5537f5
37
config.go
37
config.go
@ -111,7 +111,6 @@ var (
|
|||||||
// GlobalSessions is the instance for the session manager
|
// GlobalSessions is the instance for the session manager
|
||||||
GlobalSessions *session.Manager
|
GlobalSessions *session.Manager
|
||||||
|
|
||||||
workPath string
|
|
||||||
// appConfigPath is the path to the config files
|
// appConfigPath is the path to the config files
|
||||||
appConfigPath string
|
appConfigPath string
|
||||||
// appConfigProvider is the provider for the config, default is ini
|
// appConfigProvider is the provider for the config, default is ini
|
||||||
@ -120,12 +119,8 @@ var (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
|
AppPath, _ = filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
workPath, _ = os.Getwd()
|
|
||||||
workPath, _ = filepath.Abs(workPath)
|
|
||||||
|
|
||||||
if workPath != AppPath {
|
|
||||||
os.Chdir(AppPath)
|
os.Chdir(AppPath)
|
||||||
}
|
|
||||||
|
|
||||||
BConfig = &Config{
|
BConfig = &Config{
|
||||||
AppName: "beego",
|
AppName: "beego",
|
||||||
@ -175,7 +170,7 @@ func init() {
|
|||||||
SessionName: "beegosessionID",
|
SessionName: "beegosessionID",
|
||||||
SessionGCMaxLifetime: 3600,
|
SessionGCMaxLifetime: 3600,
|
||||||
SessionProviderConfig: "",
|
SessionProviderConfig: "",
|
||||||
SessionCookieLifeTime: 0, //set cookie default is the brower life
|
SessionCookieLifeTime: 0, //set cookie default is the browser life
|
||||||
SessionAutoSetCookie: true,
|
SessionAutoSetCookie: true,
|
||||||
SessionDomain: "",
|
SessionDomain: "",
|
||||||
},
|
},
|
||||||
@ -189,7 +184,7 @@ func init() {
|
|||||||
|
|
||||||
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
appConfigPath = filepath.Join(AppPath, "conf", "app.conf")
|
||||||
if !utils.FileExists(appConfigPath) {
|
if !utils.FileExists(appConfigPath) {
|
||||||
AppConfig = &beegoAppConfig{config.NewFakeConfig()}
|
AppConfig = &beegoAppConfig{innerConfig: config.NewFakeConfig()}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -205,8 +200,8 @@ func parseConfig(appConfigPath string) (err error) {
|
|||||||
// set the run mode first
|
// set the run mode first
|
||||||
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
|
if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" {
|
||||||
BConfig.RunMode = envRunMode
|
BConfig.RunMode = envRunMode
|
||||||
} else if runmode := AppConfig.String("RunMode"); runmode != "" {
|
} else if runMode := AppConfig.String("RunMode"); runMode != "" {
|
||||||
BConfig.RunMode = runmode
|
BConfig.RunMode = runMode
|
||||||
}
|
}
|
||||||
|
|
||||||
BConfig.AppName = AppConfig.DefaultString("AppName", BConfig.AppName)
|
BConfig.AppName = AppConfig.DefaultString("AppName", BConfig.AppName)
|
||||||
@ -393,46 +388,46 @@ func (b *beegoAppConfig) Float(key string) (float64, error) {
|
|||||||
return b.innerConfig.Float(key)
|
return b.innerConfig.Float(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) DefaultString(key string, defaultval string) string {
|
func (b *beegoAppConfig) DefaultString(key string, defaultVal string) string {
|
||||||
if v := b.String(key); v != "" {
|
if v := b.String(key); v != "" {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return defaultval
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) DefaultStrings(key string, defaultval []string) []string {
|
func (b *beegoAppConfig) DefaultStrings(key string, defaultVal []string) []string {
|
||||||
if v := b.Strings(key); len(v) != 0 {
|
if v := b.Strings(key); len(v) != 0 {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return defaultval
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) DefaultInt(key string, defaultval int) int {
|
func (b *beegoAppConfig) DefaultInt(key string, defaultVal int) int {
|
||||||
if v, err := b.Int(key); err == nil {
|
if v, err := b.Int(key); err == nil {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return defaultval
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) DefaultInt64(key string, defaultval int64) int64 {
|
func (b *beegoAppConfig) DefaultInt64(key string, defaultVal int64) int64 {
|
||||||
if v, err := b.Int64(key); err == nil {
|
if v, err := b.Int64(key); err == nil {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return defaultval
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) DefaultBool(key string, defaultval bool) bool {
|
func (b *beegoAppConfig) DefaultBool(key string, defaultVal bool) bool {
|
||||||
if v, err := b.Bool(key); err == nil {
|
if v, err := b.Bool(key); err == nil {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return defaultval
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) DefaultFloat(key string, defaultval float64) float64 {
|
func (b *beegoAppConfig) DefaultFloat(key string, defaultVal float64) float64 {
|
||||||
if v, err := b.Float(key); err == nil {
|
if v, err := b.Float(key); err == nil {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
return defaultval
|
return defaultVal
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
|
func (b *beegoAppConfig) DIY(key string) (interface{}, error) {
|
||||||
|
@ -49,7 +49,7 @@ var colors = []brush{
|
|||||||
type consoleWriter struct {
|
type consoleWriter struct {
|
||||||
lg *log.Logger
|
lg *log.Logger
|
||||||
Level int `json:"level"`
|
Level int `json:"level"`
|
||||||
Color bool `json:"color"`
|
Colorful bool `json:"color"` //this filed is useful only when system's terminal supports color
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewConsole create ConsoleWriter returning as LoggerInterface.
|
// NewConsole create ConsoleWriter returning as LoggerInterface.
|
||||||
@ -57,7 +57,7 @@ func NewConsole() Logger {
|
|||||||
cw := &consoleWriter{
|
cw := &consoleWriter{
|
||||||
lg: log.New(os.Stdout, "", 0),
|
lg: log.New(os.Stdout, "", 0),
|
||||||
Level: LevelDebug,
|
Level: LevelDebug,
|
||||||
Color: true,
|
Colorful: true,
|
||||||
}
|
}
|
||||||
return cw
|
return cw
|
||||||
}
|
}
|
||||||
@ -68,7 +68,11 @@ func (c *consoleWriter) Init(jsonConfig string) error {
|
|||||||
if len(jsonConfig) == 0 {
|
if len(jsonConfig) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return json.Unmarshal([]byte(jsonConfig), c)
|
err := json.Unmarshal([]byte(jsonConfig), c)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
c.Colorful = false
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteMsg write message in console.
|
// WriteMsg write message in console.
|
||||||
@ -77,12 +81,11 @@ func (c *consoleWriter) WriteMsg(when time.Time, msg string, level int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
msg = formatLogTime(when) + msg
|
msg = formatLogTime(when) + msg
|
||||||
if runtime.GOOS == "windows" || !c.Color {
|
if c.Colorful {
|
||||||
c.lg.Println(msg)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
c.lg.Println(colors[level](msg))
|
c.lg.Println(colors[level](msg))
|
||||||
|
} else {
|
||||||
|
c.lg.Println(msg)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user