project init
This commit is contained in:
		
							parent
							
								
									070afc0939
								
							
						
					
					
						commit
						9d7405d0c2
					
				
							
								
								
									
										9
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
			
		||||
# Created by .ignore support plugin (hsz.mobi)
 | 
			
		||||
### Example user template template
 | 
			
		||||
### Example user template
 | 
			
		||||
 | 
			
		||||
# IntelliJ project files
 | 
			
		||||
.idea
 | 
			
		||||
*.iml
 | 
			
		||||
out
 | 
			
		||||
gen
 | 
			
		||||
							
								
								
									
										102
									
								
								log.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										102
									
								
								log.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,102 @@
 | 
			
		||||
package grlogs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"github.com/astaxie/beego"
 | 
			
		||||
	"github.com/astaxie/beego/logs"
 | 
			
		||||
	"lot.gaore.com/library/common"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	_              = iota
 | 
			
		||||
	LEVEL_NONE     = iota
 | 
			
		||||
	LEVEL_CRITICAL = iota
 | 
			
		||||
	LEVEL_ERROR    = iota
 | 
			
		||||
	LEVEL_WARNING  = iota
 | 
			
		||||
	LEVEL_WARN     = iota
 | 
			
		||||
	LEVEL_INFO     = iota
 | 
			
		||||
	LEVEL_DEBUG    = iota
 | 
			
		||||
	LEVEL_ALL      = iota
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type ConsoleLogConfig struct {
 | 
			
		||||
	Level int `json:"level"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type FileLogConfig struct {
 | 
			
		||||
	Filename string `json:"filename"`
 | 
			
		||||
	Level    int    `json:"level"`
 | 
			
		||||
	Maxlines int    `json:"maxlines"`
 | 
			
		||||
	Daily    bool   `json:"daily"`
 | 
			
		||||
	Maxdays  int    `json:"maxdays"`
 | 
			
		||||
	Color    bool   `json:"color"`
 | 
			
		||||
	Rotate   bool   `json:"rotate"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ConnLogConfig struct {
 | 
			
		||||
	ReconnectOnMsg bool   `json:"reconnect_on_msg"`
 | 
			
		||||
	Reconnect      bool   `json:"reconnect"`
 | 
			
		||||
	Net            string `json:"net"`
 | 
			
		||||
	Addr           string `json:"addr"`
 | 
			
		||||
	Level          int    `json:"level"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var loggers = make(map[string]*Logger)
 | 
			
		||||
 | 
			
		||||
type Logger struct {
 | 
			
		||||
	*logs.BeeLogger
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *Logger) Write(p []byte) (n int, err error) {
 | 
			
		||||
	l.Debug(bytes.NewBuffer(p).String())
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *Logger) SetFormat() {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (l *Logger) Stop() {
 | 
			
		||||
	l.BeeLogger.Close()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func GetLogger(name string) *Logger {
 | 
			
		||||
 | 
			
		||||
	if l, ok := loggers[name]; ok {
 | 
			
		||||
		return l
 | 
			
		||||
	} else {
 | 
			
		||||
 | 
			
		||||
		var level int = LEVEL_WARN
 | 
			
		||||
 | 
			
		||||
		if beego.BConfig.RunMode == "dev" {
 | 
			
		||||
			level = LEVEL_ALL
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		conf1 := FileLogConfig{
 | 
			
		||||
			Filename: common.GetCwd(fmt.Sprintf("runtime/logs/%s.log", name)),
 | 
			
		||||
			Level:    LEVEL_ALL,
 | 
			
		||||
			Maxlines: 0,
 | 
			
		||||
			Daily:    true,
 | 
			
		||||
			Maxdays:  7,
 | 
			
		||||
			Color:    true,
 | 
			
		||||
			Rotate:   true,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		conf2 := ConsoleLogConfig{
 | 
			
		||||
			Level: level,
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		confString, _ := json.Marshal(&conf1)
 | 
			
		||||
		confString2, _ := json.Marshal(&conf2)
 | 
			
		||||
		loggers[name] = &Logger{}
 | 
			
		||||
		loggers[name].BeeLogger = logs.NewLogger()
 | 
			
		||||
		loggers[name].SetLogger(logs.AdapterFile, bytes.NewBuffer(confString).String())
 | 
			
		||||
		loggers[name].SetLogger(logs.AdapterConsole, bytes.NewBuffer(confString2).String())
 | 
			
		||||
		loggers[name].BeeLogger.SetPrefix("_" + name)
 | 
			
		||||
		loggers[name].BeeLogger.EnableFuncCallDepth(true)
 | 
			
		||||
		loggers[name].BeeLogger.SetLogFuncCallDepth(2)
 | 
			
		||||
		return loggers[name]
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								log_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								log_test.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
			
		||||
package grlogs
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/astaxie/beego/logs"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestGetLogger(t *testing.T) {
 | 
			
		||||
	logs.NewLogger()
 | 
			
		||||
	logs.Info(LEVEL_ALL)
 | 
			
		||||
	GetLogger("nds").Debug("akldalskflasfa")
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user