update log add access log

This commit is contained in:
fanqq 2015-06-17 14:10:54 +08:00
parent fedc904ca0
commit 0e8a167604
6 changed files with 112 additions and 10 deletions

View file

@ -8,6 +8,7 @@ import org.springframework.stereotype.Component;
import org.unidal.dal.jdbc.DalException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@ -31,7 +32,7 @@ public class LockResource {
@GET
@Path("/status")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getStatus(@Context HttpHeaders hh) throws Exception {
public Response getStatus(@Context HttpServletRequest request,@Context HttpHeaders hh) throws Exception {
LockList ll = new LockList();
for (LockStatus ls : lockService.getLockStatus()) {
ll.addLockStatus(ls);
@ -41,7 +42,7 @@ public class LockResource {
@GET
@Path("/unlock/{key}")
public Response forceUnlock(@Context HttpHeaders hh, @PathParam("key") String key) throws DalException {
public Response forceUnlock(@Context HttpServletRequest request,@Context HttpHeaders hh, @PathParam("key") String key) throws DalException {
lockService.forceUnlock(key);
return Response.status(200).build();
}

View file

@ -6,6 +6,7 @@ import com.ctrip.zeus.service.nginx.NginxService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
@ -29,7 +30,7 @@ public class NginxResource {
@GET
@Path("/load")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response load(@Context HttpHeaders hh) {
public Response load(@Context HttpServletRequest request,@Context HttpHeaders hh) {
try {
NginxResponse result = nginxService.load();
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
@ -45,7 +46,7 @@ public class NginxResource {
@GET
@Path("/write")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response write(@Context HttpHeaders hh )
public Response write(@Context HttpServletRequest request,@Context HttpHeaders hh )
{
try {
NginxResponse result = nginxService.writeToDisk();
@ -61,7 +62,7 @@ public class NginxResource {
@POST
@Path("/dyups/{upStreamName:[a-zA-Z0-9_-]+}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response localDyups(@Context HttpHeaders hh,@PathParam("upStreamName") String upsName, String upsCommands ){
public Response localDyups(@Context HttpServletRequest request,@Context HttpHeaders hh,@PathParam("upStreamName") String upsName, String upsCommands ){
try {
NginxResponse result = nginxService.dyopsLocal(upsName,upsCommands);
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
@ -78,7 +79,7 @@ public class NginxResource {
@GET
@Path("/status")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response status(@Context HttpHeaders hh) throws Exception {
public Response status(@Context HttpServletRequest request,@Context HttpHeaders hh) throws Exception {
NginxServerStatus status = nginxService.getStatus();
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
return Response.status(200).entity(String.format(NginxServerStatus.XML, status)).type(MediaType.APPLICATION_XML).build();
@ -90,7 +91,7 @@ public class NginxResource {
@GET
@Path("/trafficStatus")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getLocalTrafficStatus(@Context HttpHeaders hh) throws Exception {
public Response getLocalTrafficStatus(@Context HttpServletRequest request,@Context HttpHeaders hh) throws Exception {
TrafficStatusList l = new TrafficStatusList();
for (TrafficStatus status : nginxService.getLocalTrafficStatus()) {
l.addTrafficStatus(status);
@ -101,7 +102,7 @@ public class NginxResource {
@GET
@Path("/loadAll/slb/{slbId:[0-9]+}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response loadAll(@Context HttpHeaders hh, @PathParam("slbId") Long slbId) throws Exception {
public Response loadAll(@Context HttpServletRequest request,@Context HttpHeaders hh, @PathParam("slbId") Long slbId) throws Exception {
List<NginxResponse> nginxResponseList = nginxService.loadAll(slbId);
NginxResponseList result = new NginxResponseList();
for (NginxResponse nginxResponse : nginxResponseList) {
@ -117,7 +118,7 @@ public class NginxResource {
@GET
@Path("/allStatus/slb/{slbId:[0-9]+}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response allStatus(@Context HttpHeaders hh, @PathParam("slbId") Long slbId) throws Exception {
public Response allStatus(@Context HttpServletRequest request,@Context HttpHeaders hh, @PathParam("slbId") Long slbId) throws Exception {
List<NginxServerStatus> nginxServerStatusList = nginxService.getStatusAll(slbId);
NginxServerStatusList result = new NginxServerStatusList();
for (NginxServerStatus nginxServerStatus : nginxServerStatusList) {

View file

@ -162,7 +162,7 @@ public class StatusResource {
@GET
@Path("/traffic")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getTrafficStatusBySlb(@Context HttpHeaders hh,
public Response getTrafficStatusBySlb(@Context HttpServletRequest request,@Context HttpHeaders hh,
@QueryParam("slbId") Long slbId) throws Exception {
if (slbId == null) {
throw new ValidationException("Missing parameters.");

View file

@ -0,0 +1,59 @@
package com.ctrip.zeus.service.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
/**
* Created by fanqq on 2015/6/17.
*/
@Aspect
@Component
public class AccessLogAspect implements Ordered {
private static final Logger LOGGER = LoggerFactory.getLogger(AccessLogAspect.class);
@Before("execution(* com.ctrip.zeus.restful.resource.*Resource.*(..))")
public void accessLog(JoinPoint point) throws Throwable {
HttpServletRequest request = findRequestArg(point);
// not found request parameter
if (request == null){
LOGGER.warn("Not Found HttpServletRequest!");
return;
}
addLog(request);
}
private void addLog(HttpServletRequest request)
{
StringBuilder sb = new StringBuilder(256);
sb.append("\nHost:").append(request.getRemoteHost())
.append("\nURI:").append(request.getRequestURI())
.append("\nRemoteAddr:").append(request.getRemoteAddr())
.append("\nMethod:").append(request.getMethod())
.append("\nQueryString:").append(request.getQueryString())
.append("\nRequestURL:").append(request.getRequestURL())
.append("\nContentType:").append(request.getContentType());
LOGGER.info(sb.toString());
}
private HttpServletRequest findRequestArg(JoinPoint point) {
Object[] args = point.getArgs();
for (Object arg : args) {
if (arg instanceof HttpServletRequest){
return (HttpServletRequest)arg;
}
}
return null;
}
@Override
public int getOrder() {
return 10;
}
}

View file

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_DIR" value="${APP_HOME}/logs"/>
<property name="LOG_FILE_NAME" value="zeus.log"/>
<timestamp key="byDay" datePattern="yyyy-MM-dd_HH:mm"/>
<!-- Output to Console -->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
@ -8,8 +11,26 @@
</encoder>
</appender>
<!-- Output to File and Rotate if it's necessary -->
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/${LOG_FILE_NAME}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_DIR}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date [%-5thread] %-5level [%-10logger] %-10msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="Console"/>
<appender-ref ref="ROLLING"/>
</root>
</configuration>

View file

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOG_DIR" value="${APP_HOME}/logs"/>
<property name="LOG_FILE_NAME" value="zeus.log"/>
<timestamp key="byDay" datePattern="yyyy-MM-dd_HH:mm"/>
<!-- Output to Console -->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
@ -7,9 +10,26 @@
<pattern>%date %level [%thread] %10logger [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<!-- Output to File and Rotate if it's necessary -->
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/${LOG_FILE_NAME}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_DIR}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%date [%-5thread] %-5level [%-10logger] %-10msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="Console"/>
<appender-ref ref="ROLLING"/>
</root>
</configuration>