log4j.properties
log4j.logger.LogRequest=DEBUG,Request
log4j.additivity.LogRequest=false
log4j.appender.Request=org.apache.log4j.DailyRollingFileAppender
log4j.appender.Request.Threshold=DEBUG
log4j.appender.Request.File=logs/request/request.log
log4j.appender.Request.DatePattern='.'yyyy-MM-dd
log4j.appender.Request.Append=true
log4j.appender.Request.Encoding=UTF-8
log4j.appender.Request.layout=org.apache.log4j.PatternLayout
log4j.appender.Request.layout.ConversionPattern=%m%n
代码
long start = System.currentTimeMillis(); Service service = Service.lookup(cmd,userId); Response response = service.execute(request); long use = System.currentTimeMillis() - start; int len = response.getContent().toJSONString().getBytes().length; //记录请求和响应信息 logRequest.info(System.currentTimeMillis()+"|"+use+"|"+len+"|"+cmd+"|"+userId+"|"+request.getContent()+"|"+response.getContent());
命令
cat request.log | awk -F | '{if($3 >500)print $4}' | sort | uniq -c | sort -nr | ad_awk
ad_awk.sh
#!/bin/awk -f BEGIN{ i=0; } { total+=$1; na[i]=$1; all[i]=$0 i++; } END{ print "--------------------------------------" print " total:",total; printf(" 占比 ----- num Key ") print "--------------------------------------" for(k=0;k<i;k++) { num=na[k]; printf(" %.2f% ----- %s ", num/total*100,all[k]); } }