thur
2018-06-09 14:53
我的是nginx默认的日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
//nginx规则 '$remote_addr\t$http_x_forwarded_for\t$request\t$status\t$body_bytes_sent\t$bytes_sent\t$http_user_agent\t$request_length\t$request_time\t$time_local\t$upstream_response_time' /** 解析模块 */ func (l *LogProcess) Process() { /*rege := regexp.MustCompile(`([\d\.]+)\s+([^ \[]+)\s+([^ \[]+)\s+\[([^\]]+)\]\s+([a-z]+)\s+\"([^"]+)\"\s+(\d{3})\s+( \d+)\s+\"([^"]+)\"\s+\"(.*?)\"\s+\"([\d\.-]+)\"\s+([\d\.-]+)\s+([d\.-]+)`) //解析正则表达式*/ //循环解析 loc, _ := time.LoadLocation("Asia/Shanghai"); for v := range l.rc { //res := rege.FindStringSubmatch(string(v) ) fmt.Println(string(v)) res := strings.Split(string(v), "\t") if len(res) != 11 { //匹配失败 log.Println("rege.FindStringSubmatch fail: ", string(v), len(res)) //写日志 continue } message := &Message {} //时间 theTime, err := time.ParseInLocation("02/Jan/2006:15:04:05 +0800", res[9], loc) //解析时间 if err != nil { log.Println("time.ParseInLocation fail: ", err.Error(), res[9]) //写日志 continue } time := theTime.Unix() message.TimeLocal = time bytesSent, _ := strconv.Atoi(res[5]) //字符串转 int message.BytesSent = bytesSent //"POST /API/COURSE/INDEX HTTP/1.1" resSli := strings.Split(res[2], " ") //字符串分割 if len(resSli) != 3 { log.Println("strings.Split fail", res[2]) //写日志 continue } message.Method = resSli[0] resUrl, err := url.Parse(resSli[1]) if err != nil { log.Println("url.Parse fail", err.Error(),resSli[1]) //写日志 continue } message.Path = resUrl.Path message.Scheme = resSli[2] message.Status = res[3] upstreamTime, _ := strconv.ParseFloat(res[10], 64) message.UpstreamTime = upstreamTime requestTime, _ := strconv.ParseFloat(res[8], 64) message.RequestTime = requestTime l.wc <- message } }
Go并发编程案例解析
15207 学习 · 53 问题
相似问题