forked from devfeel/dotweb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1、HttpContext增加ViewC、WriteStringC、WriteBlobC、WriteJsonC、WriteJsonBlobC方法,以支持传入指定HttpCode 2、调整Redirect(code int, targetUrl string) 为 Redirect(code int, targetUrl string) error,增加error返回值 3、DotWeb增加SetLogger方法,支持接入第三方Logger,需实现logger.AppLog接口 4、新增 example/logger 目录 * 2017-05-16 09:00
- Loading branch information
Showing
9 changed files
with
250 additions
and
32 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
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,148 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/devfeel/dotweb" | ||
"github.com/devfeel/dotweb/logger" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func main() { | ||
//初始化DotServer | ||
app := dotweb.New() | ||
|
||
//设置dotserver日志目录 | ||
//如果不设置,默认启用,且默认为当前目录 | ||
app.SetLogger(NewYLog()) | ||
app.SetEnabledLog(true) | ||
app.SetLogPath("d:/gotmp/") | ||
|
||
fmt.Println(logger.Logger()) | ||
|
||
//开启development模式 | ||
app.SetDevelopmentMode() | ||
|
||
//设置路由 | ||
InitRoute(app.HttpServer) | ||
|
||
//启动 监控服务 | ||
app.SetPProfConfig(true, 8081) | ||
|
||
// 开始服务 | ||
port := 8080 | ||
fmt.Println("dotweb.StartServer => " + strconv.Itoa(port)) | ||
err := app.StartServer(port) | ||
fmt.Println("dotweb.StartServer error => ", err) | ||
} | ||
|
||
func Index(ctx *dotweb.HttpContext) { | ||
ctx.Response.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
ctx.WriteStringC(201, "index => ", ctx.RouterParams) | ||
} | ||
|
||
func InitRoute(server *dotweb.HttpServer) { | ||
server.Router().GET("/", Index) | ||
} | ||
|
||
type chanLog struct { | ||
Content string | ||
LogTarget string | ||
} | ||
|
||
type yLog struct { | ||
logRootPath string | ||
logChan_Custom chan chanLog | ||
enabledLog bool | ||
} | ||
|
||
//create new yLog | ||
func NewYLog() *yLog { | ||
l := &yLog{logChan_Custom: make(chan chanLog, 10000)} | ||
go l.handleCustom() | ||
return l | ||
} | ||
|
||
const ( | ||
defaultDateFormatForFileName = "2006_01_02" | ||
defaultDateLayout = "2006-01-02" | ||
defaultFullTimeLayout = "2006-01-02 15:04:05.999999" | ||
defaultTimeLayout = "2006-01-02 15:04:05" | ||
) | ||
|
||
func (l *yLog) Debug(log string, logTarget string) { | ||
l.Log(log, logTarget, "debug") | ||
} | ||
|
||
func (l *yLog) Info(log string, logTarget string) { | ||
l.Log(log, logTarget, "info") | ||
} | ||
|
||
func (l *yLog) Warn(log string, logTarget string) { | ||
l.Log(log, logTarget, "warn") | ||
} | ||
|
||
func (l *yLog) Error(log string, logTarget string) { | ||
l.Log(log, logTarget, "error") | ||
} | ||
|
||
func (l *yLog) Log(log string, logTarget string, logLevel string) { | ||
if l.enabledLog { | ||
chanLog := chanLog{ | ||
LogTarget: "yLog_" + logTarget + "_" + logLevel, | ||
Content: log, | ||
} | ||
l.logChan_Custom <- chanLog | ||
} | ||
} | ||
|
||
//set log path | ||
func (l *yLog) SetLogPath(rootPath string) { | ||
//设置日志根目录 | ||
l.logRootPath = rootPath | ||
if !strings.HasSuffix(l.logRootPath, "/") { | ||
l.logRootPath = l.logRootPath + "/" | ||
} | ||
} | ||
|
||
//set enabled log | ||
func (l *yLog) SetEnabledLog(enabledLog bool) { | ||
l.enabledLog = enabledLog | ||
} | ||
|
||
//处理日志内部函数 | ||
func (l *yLog) handleCustom() { | ||
for { | ||
log := <-l.logChan_Custom | ||
l.writeLog(log, "custom") | ||
} | ||
} | ||
|
||
func (l *yLog) writeLog(chanLog chanLog, level string) { | ||
filePath := l.logRootPath + chanLog.LogTarget | ||
switch level { | ||
case "custom": | ||
filePath = filePath + "_" + time.Now().Format(defaultDateFormatForFileName) + ".log" | ||
break | ||
} | ||
log := time.Now().Format(defaultFullTimeLayout) + " " + chanLog.Content | ||
writeFile(filePath, log) | ||
} | ||
|
||
func writeFile(logFile string, log string) { | ||
var mode os.FileMode | ||
flag := syscall.O_RDWR | syscall.O_APPEND | syscall.O_CREAT | ||
mode = 0666 | ||
logstr := log + "\r\n" | ||
file, err := os.OpenFile(logFile, flag, mode) | ||
defer file.Close() | ||
if err != nil { | ||
fmt.Println(logFile, err) | ||
return | ||
} | ||
//fmt.Print(logstr) | ||
file.WriteString(logstr) | ||
} |
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
Oops, something went wrong.