Skip to content

Commit

Permalink
open logfile when init,change adlog.go to utils.go
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas7788 committed Oct 20, 2017
1 parent 15108d5 commit e33c698
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 95 deletions.
Binary file modified ad_server
Binary file not shown.
Empty file modified build.sh
100644 → 100755
Empty file.
1 change: 1 addition & 0 deletions data/log/click.2017102010.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
time="2017-10-20T10:26:58+08:00" level=info msg="test click" adNum=1 appId=1 clickUrl=baidu.com creativeId=456 deviceId=0x22q53 iP=127.0.0.1 oS=1 osVersion=1.0.0 searchId=123 slotId=2 unitId=123
Empty file.
72 changes: 28 additions & 44 deletions src/adhandler/clickhandler.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
package adhandler
import (
"os"
"time"
"fmt"
"strconv"
"net/http"
"encoding/json"
"encoding/base64"

"strconv"
//"math/rand"
"ad-server/src/adserver"
"fmt"
"encoding/base64"
"github.com/sirupsen/logrus"

"adserver"
"utils"
"ad-server/src/utils"
"os"
)
var adlog = logrus.New()
var logpath="./data/logfile/"
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) {
//获得编码后的查询字符串
queryStringEncoded:=r.URL.RawQuery
queryStringEncoded := r.URL.RawQuery
//解码
queryStringDecodedBytes,err:=base64.StdEncoding.DecodeString(queryStringEncoded)
if err!=nil{//异常处理
fmt.Println("error:",err)
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)
}
r.URL.RawQuery=string(queryStringDecodedBytes)
r.URL.RawQuery = string(queryStringDecodedBytes)

r.ParseForm()
req := new(adserver.Request)
Expand Down Expand Up @@ -82,33 +91,8 @@ func ClickHandler(w http.ResponseWriter, r *http.Request) {
if len(r.Form["click_url"]) > 0 {
req.ClickUrl = r.Form["click_url"][0]
}
//log设置输出
adlog.Out = os.Stdout
dateStr:=strconv.Itoa(time.Now().Year())+strconv.Itoa(int(time.Now().Month()))+strconv.Itoa(time.Now().Day())
logFileName:=dateStr+"log.log"
logFile:=logpath+logFileName
fmt.Println(logFile)
//判断日志文件是否存在
if !utils.CheckFileIsExist(logFile){
_,err:= os.Create(logFile)
if err!=nil{
fmt.Println(err)
res := adserver.Response{
ResCode: 4,
AdList: nil,
}
resBytes, _ := json.Marshal(res)
w.Write(resBytes)
}
}
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
adlog.Out = file
} else {
adlog.Info("Failed to log to file, using default stderr")
fmt.Println(err)
}

//
adlog.Out = clickFile
adlog.WithFields(logrus.Fields{
"appId": req.AppId,
"slotId": req.SlotId,
Expand All @@ -121,5 +105,5 @@ func ClickHandler(w http.ResponseWriter, r *http.Request) {
"creativeId":req.CreativeId,
"searchId":req.SearchId,
"clickUrl":req.ClickUrl,
}).Info("test")
}).Info("test click")
}
65 changes: 25 additions & 40 deletions src/adhandler/displayhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,43 @@ package adhandler
import (
"net/http"
"strconv"
"time"
"os"
"fmt"
"encoding/json"
//"math/rand"
"ad-server/src/adserver"
"encoding/base64"

"fmt"
"github.com/sirupsen/logrus"

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

r.ParseForm()
req := new(adserver.Request)
Expand Down Expand Up @@ -57,7 +67,7 @@ func DisplayHandler(w http.ResponseWriter, r *http.Request) {
}
// os
if len(r.Form["os"]) > 0 {
os, _ := strconv.ParseUint(r.Form["os"][0], 10, 32)
os , _ := strconv.ParseUint(r.Form["os"][0], 10, 32)
req.Os = uint(os)
}
// os_version
Expand All @@ -76,32 +86,7 @@ func DisplayHandler(w http.ResponseWriter, r *http.Request) {
if len(r.Form["search_id"]) > 0 {
req.SearchId = r.Form["search_id"][0]
}
//log设置输出
adlog.Out = os.Stdout
dateStr:=strconv.Itoa(time.Now().Year())+strconv.Itoa(int(time.Now().Month()))+strconv.Itoa(time.Now().Day())
logFileName:=dateStr+"log.log"
logFile:=logpath+logFileName
fmt.Println(logFile)
//判断日志文件是否存在
if !utils.CheckFileIsExist(logFile){
_,err:= os.Create(logFile)
if err!=nil{
fmt.Println(err)
res := adserver.Response{
ResCode: 4,
AdList: nil,
}
resBytes, _ := json.Marshal(res)
w.Write(resBytes)
}
}
file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err == nil {
adlog.Out = file
} else {
adlog.Info("Failed to log to file, using default stderr")
fmt.Println(err)
}
adlog.Out = impressionFile
adlog.WithFields(logrus.Fields{
"appId": req.AppId,
"slotId": req.SlotId,
Expand Down
11 changes: 0 additions & 11 deletions src/utils/adlog.go

This file was deleted.

25 changes: 25 additions & 0 deletions src/utils/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils
import(
"os"
"strconv"
"time"
)
func CheckFileIsExist(filename string) (bool) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false;
}
return true;
}
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"{
logFileName = "impression." + dateStr + ".log"
}else if filename == "click"{
logFileName = "click." + dateStr + ".log"
}else{
logFileName = "default." + dateStr + ".log"
}
logFile := logpath + logFileName
return logFile
}

0 comments on commit e33c698

Please sign in to comment.