Skip to content

Commit

Permalink
add the function of log cutting
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas7788 committed Oct 20, 2017
1 parent e33c698 commit a188ceb
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 57 deletions.
Binary file removed ad_server
Binary file not shown.
1 change: 0 additions & 1 deletion data/log/click.2017102010.log

This file was deleted.

Empty file removed data/log/impression.2017102010.log
Empty file.
3 changes: 3 additions & 0 deletions log/impression/impression.log.201710201600
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
time="2017-10-20T16:59:41+08:00" level=info msg="test displayHandler" adNum=1 appId=1 creativeId=456 deviceId=0x22q53 iP=127.0.0.1 oS=1 osVersion=1.0.0 searchId=123 slotId=2 unitId=123
time="2017-10-20T16:59:59+08:00" level=info msg="test displayHandler" adNum=1 appId=1 creativeId=456 deviceId=0x22q53 iP=127.0.0.1 oS=1 osVersion=1.0.0 searchId=123 slotId=2 unitId=123
time="2017-10-20T16:59:59+08:00" level=info msg="test displayHandler" adNum=1 appId=1 creativeId=456 deviceId=0x22q53 iP=127.0.0.1 oS=1 osVersion=1.0.0 searchId=123 slotId=2 unitId=123
37 changes: 8 additions & 29 deletions src/adhandler/clickhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,21 @@ import (
"net/http"
"strconv"
//"math/rand"
"ad-server/src/adserver"
"fmt"
"adserver"
"encoding/base64"
"github.com/sirupsen/logrus"
"ad-server/src/utils"
"os"
"time"
)
var adlog = logrus.New()
var logpath = "./data/log/"
var clickFile *os.File
func init(){
logFile := utils.GetLogFileName("click",logpath)
//判断日志文件是否存在
if !utils.CheckFileIsExist(logFile){
_ , err := os.Create(logFile)
if err != nil{
fmt.Println(err)
}
}
clickFile , _= os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
}
func ClickHandler(w http.ResponseWriter, r *http.Request) {
ConfigLocalFilesystemLogger("./log/click","click.log",time.Hour*24,time.Hour)

//获得编码后的查询字符串
queryStringEncoded := r.URL.RawQuery
//解码
queryStringDecodedBytes , err := base64.StdEncoding.DecodeString(queryStringEncoded)
if err != nil{//异常处理
fmt.Println("error:" , err)
res := adserver.Response{
ResCode: 4,
AdList: nil,
}
resBytes, _ := json.Marshal(res)
w.Write(resBytes)
if err != nil {//异常处理
adLog.Println(err)
return
}
r.URL.RawQuery = string(queryStringDecodedBytes)

Expand Down Expand Up @@ -91,9 +72,7 @@ func ClickHandler(w http.ResponseWriter, r *http.Request) {
if len(r.Form["click_url"]) > 0 {
req.ClickUrl = r.Form["click_url"][0]
}
//
adlog.Out = clickFile
adlog.WithFields(logrus.Fields{
adLog.WithFields(logrus.Fields{
"appId": req.AppId,
"slotId": req.SlotId,
"adNum":req.AdNum,
Expand Down
33 changes: 7 additions & 26 deletions src/adhandler/displayhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,22 @@ import (
"net/http"
"strconv"
//"math/rand"
"ad-server/src/adserver"
"adserver"
"encoding/base64"
"fmt"
"time"
"github.com/sirupsen/logrus"
"os"
"ad-server/src/utils"
)
var impressionFile *os.File
func init(){
logFile:=utils.GetLogFileName("impression",logpath)
//判断日志文件是否存在
if !utils.CheckFileIsExist(logFile){
_,err := os.Create(logFile)
if err != nil{
fmt.Println("**********")
fmt.Println(err)
}
}
impressionFile , _ = os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
}

//展示handler
func DisplayHandler(w http.ResponseWriter, r *http.Request) {
ConfigLocalFilesystemLogger("./log/impression","impression.log",time.Hour*24,time.Hour)
//获得编码后的查询字符串
queryStringEncoded := r.URL.RawQuery
//解码
queryStringDecodedBytes,err := base64.StdEncoding.DecodeString(queryStringEncoded)
if err != nil{//异常处理
fmt.Println("error:",err)
res := adserver.Response{
ResCode: 4,
AdList: nil,
}
resBytes , _ := json.Marshal(res)
w.Write(resBytes)
adLog.Println(err)
return
}
r.URL.RawQuery = string(queryStringDecodedBytes)

Expand Down Expand Up @@ -86,8 +68,7 @@ func DisplayHandler(w http.ResponseWriter, r *http.Request) {
if len(r.Form["search_id"]) > 0 {
req.SearchId = r.Form["search_id"][0]
}
adlog.Out = impressionFile
adlog.WithFields(logrus.Fields{
adLog.WithFields(logrus.Fields{
"appId": req.AppId,
"slotId": req.SlotId,
"adNum":req.AdNum,
Expand Down
32 changes: 32 additions & 0 deletions src/adhandler/initlog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package adhandler
import (
"github.com/lestrrat/go-file-rotatelogs"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
"time"
"github.com/pkg/errors"
"path"
)
var adLog=logrus.New()
// config logrus log to local filesystem, with file rotation
func ConfigLocalFilesystemLogger(logPath string, logFileName string, maxAge time.Duration, rotationTime time.Duration) {
baseLogPaht := path.Join(logPath, logFileName)
writer, err := rotatelogs.New(
baseLogPaht+".%Y%m%d%H%M",
// rotatelogs.WithLinkName(baseLogPaht), // 生成软链,指向最新日志文件
rotatelogs.WithMaxAge(maxAge), // 文件最大保存时间
rotatelogs.WithRotationTime(rotationTime), // 日志切割时间间隔
)
if err != nil {
adLog.Errorf("config local file system logger error. %+v", errors.WithStack(err))
}
lfHook := lfshook.NewHook(lfshook.WriterMap{
logrus.DebugLevel: writer, // 为不同级别设置不同的输出目的
logrus.InfoLevel: writer,
logrus.WarnLevel: writer,
logrus.ErrorLevel: writer,
logrus.FatalLevel: writer,
logrus.PanicLevel: writer,
})
adLog.AddHook(lfHook)
}
2 changes: 1 addition & 1 deletion src/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func CheckFileIsExist(filename string) (bool) {
}
return true;
}
func GetLogFileName(filename,logpath string) string{
func GetLogFileName(filename,logpath string) string {
dateStr := strconv.Itoa(time.Now().Year()) + strconv.Itoa(int(time.Now().Month())) + strconv.Itoa(time.Now().Day())+strconv.Itoa(time.Now().Hour())
var logFileName string
if filename == "impression"{
Expand Down

0 comments on commit a188ceb

Please sign in to comment.