Skip to content

Commit

Permalink
Version 0.3.13
Browse files Browse the repository at this point in the history
* HttpContext增加ViewC、WriteStringC、WriteBlobC、WriteJsonC、WriteJsonBlobC方法,以支持传入指定HttpCode
* 调整Redirect(code int, targetUrl string) 为 Redirect(code int, targetUrl string) error,增加error返回值
* DotWeb增加SetLogger方法,支持接入第三方Logger,需实现logger.AppLog接口
* DotWeb增加SetConfig方法,支持配置与启动分离
* 新增 example/logger 目录
* 2017-05-16 09:00
  • Loading branch information
devfeel committed May 16, 2017
1 parent 750dfd9 commit ed3dafa
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
3 changes: 3 additions & 0 deletions dotweb.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,9 @@ func (ds *DotWeb) DefaultHTTPErrorHandler(ctx *HttpContext, err error) {
ctx.Response.Header().Set(HeaderContentType, CharsetUTF8)
//if in development mode, output the error info
if ds.IsDevelopmentMode() {
//buf := make([]byte, 4096)
//n := runtime.Stack(buf, true)
//stack := string(buf[:n])
ctx.WriteString(fmt.Sprintln(err))
} else {
ctx.WriteString("Internal Server Error")
Expand Down
3 changes: 2 additions & 1 deletion example/logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {

//设置dotserver日志目录
//如果不设置,默认启用,且默认为当前目录
app.SetLogger(NewYLog())
//app.SetLogger(NewYLog())
app.SetEnabledLog(true)
app.SetLogPath("d:/gotmp/")

Expand All @@ -41,6 +41,7 @@ func main() {

func Index(ctx *dotweb.HttpContext) {
ctx.Response.Header().Set("Content-Type", "text/html; charset=utf-8")
logger.Logger().Info("xxxx", "x")
ctx.WriteStringC(201, "index => ", ctx.RouterParams)
}

Expand Down
7 changes: 3 additions & 4 deletions framework/exception/exception.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"runtime"
)

const logLevel_Error = "error"

//统一异常处理
func CatchError(title string, logtarget string, err interface{}) (errmsg string) {
errmsg = fmt.Sprintln(err)
os.Stdout.Write([]byte(title + " error! => " + errmsg + " => "))
buf := make([]byte, 4096)
n := runtime.Stack(buf, true)
return title + " error! => " + errmsg + " => " + string(buf[:n])
stack := string(buf[:n])
os.Stdout.Write([]byte(title + " error! => " + errmsg + " => " + stack))
return title + " error! => " + errmsg + " => " + stack
}
52 changes: 52 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package logger

import (
"errors"
"github.com/devfeel/dotweb/framework/file"
"path/filepath"
"runtime"
"strings"
)

const (
Expand Down Expand Up @@ -62,3 +66,51 @@ func InitLog() {
SetLogPath(DefaultLogPath) //set default log path
SetEnabledLog(EnabledLog) //set default enabled log
}

//日志内容
// fileName 文件名字
// line 调用行号
// fullPath 文件全路径
// funcName 那个方法进行调用
type logContext struct {
fileName string
line int
fullPath string
funcName string
}

//打印
// skip=0 runtime.Caller 的调用者.
// skip=1 runtime/proc.c 的 runtime.main
// skip=2 runtime/proc.c 的 runtime.goexit
//
//Go的普通程序的启动顺序:
//1.runtime.goexit 为真正的函数入口(并不是main.main)
//2.然后 runtime.goexit 调用 runtime.main 函数
//3.最终 runtime.main 调用用户编写的 main.main 函数
func callerInfo(skip int) (ctx *logContext, err error) {
pc, file, line, ok := runtime.Caller(skip)
if !ok {
return nil, errors.New("error during runtime.Callers")
}

funcInfo := runtime.FuncForPC(pc)
if funcInfo == nil {
return nil, errors.New("error during runtime.FuncForPC")
}

funcName := funcInfo.Name()
if strings.HasPrefix(funcName, ".") {
funcName = funcName[strings.Index(funcName, "."):]
}

ctx = &logContext{
funcName: filepath.Base(funcName),
line: line,
fullPath: file,
fileName: filepath.Base(file),
}

return ctx, nil

}
10 changes: 9 additions & 1 deletion logger/xlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type chanLog struct {
Content string
LogTarget string
LogLevel string
}

type xLog struct {
Expand Down Expand Up @@ -54,6 +55,7 @@ func (l *xLog) Log(log string, logTarget string, logLevel string) {
chanLog := chanLog{
LogTarget: logTarget + "_" + logLevel,
Content: log,
LogLevel: logLevel,
}
l.logChan_Custom <- chanLog
}
Expand Down Expand Up @@ -88,7 +90,13 @@ func (l *xLog) writeLog(chanLog chanLog, level string) {
filePath = filePath + "_" + time.Now().Format(defaultDateFormatForFileName) + ".log"
break
}
log := time.Now().Format(defaultFullTimeLayout) + " " + chanLog.Content

skip := 3
logCtx, err := callerInfo(skip)
if err != nil {
fmt.Println("log println err! " + time.Now().Format("2006-01-02 15:04:05") + " Error: " + err.Error())
}
log := fmt.Sprintf(fmt.Sprintf("[%s] %s [%s:%v] %s", chanLog.LogLevel, time.Now().Format(defaultFullTimeLayout), logCtx.fileName, logCtx.line, chanLog.Content))
writeFile(filePath, log)
}

Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func (server *HttpServer) wrapRouterHandle(handler HttpHandle, isHijack bool) Ro
defer func() {
var errmsg string
if err := recover(); err != nil {
errmsg = exception.CatchError("httpserver::RouterHandle", LogTarget_HttpServer, err)
errmsg = exception.CatchError("HttpServer::RouterHandle", LogTarget_HttpServer, err)

//handler the exception
if server.DotApp.ExceptionHandler != nil {
Expand Down

0 comments on commit ed3dafa

Please sign in to comment.