mirror of
https://github.com/ctripcorp/zeus.git
synced 2024-09-22 08:36:24 +08:00
update log add access log
This commit is contained in:
parent
fedc904ca0
commit
0e8a167604
|
@ -8,6 +8,7 @@ import org.springframework.stereotype.Component;
|
||||||
import org.unidal.dal.jdbc.DalException;
|
import org.unidal.dal.jdbc.DalException;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
|
@ -31,7 +32,7 @@ public class LockResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/status")
|
@Path("/status")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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();
|
LockList ll = new LockList();
|
||||||
for (LockStatus ls : lockService.getLockStatus()) {
|
for (LockStatus ls : lockService.getLockStatus()) {
|
||||||
ll.addLockStatus(ls);
|
ll.addLockStatus(ls);
|
||||||
|
@ -41,7 +42,7 @@ public class LockResource {
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/unlock/{key}")
|
@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);
|
lockService.forceUnlock(key);
|
||||||
return Response.status(200).build();
|
return Response.status(200).build();
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import com.ctrip.zeus.service.nginx.NginxService;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.ws.rs.*;
|
import javax.ws.rs.*;
|
||||||
import javax.ws.rs.core.Context;
|
import javax.ws.rs.core.Context;
|
||||||
import javax.ws.rs.core.HttpHeaders;
|
import javax.ws.rs.core.HttpHeaders;
|
||||||
|
@ -29,7 +30,7 @@ public class NginxResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/load")
|
@Path("/load")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||||
public Response load(@Context HttpHeaders hh) {
|
public Response load(@Context HttpServletRequest request,@Context HttpHeaders hh) {
|
||||||
try {
|
try {
|
||||||
NginxResponse result = nginxService.load();
|
NginxResponse result = nginxService.load();
|
||||||
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
|
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
|
||||||
|
@ -45,7 +46,7 @@ public class NginxResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/write")
|
@Path("/write")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||||
public Response write(@Context HttpHeaders hh )
|
public Response write(@Context HttpServletRequest request,@Context HttpHeaders hh )
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
NginxResponse result = nginxService.writeToDisk();
|
NginxResponse result = nginxService.writeToDisk();
|
||||||
|
@ -61,7 +62,7 @@ public class NginxResource {
|
||||||
@POST
|
@POST
|
||||||
@Path("/dyups/{upStreamName:[a-zA-Z0-9_-]+}")
|
@Path("/dyups/{upStreamName:[a-zA-Z0-9_-]+}")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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 {
|
try {
|
||||||
NginxResponse result = nginxService.dyopsLocal(upsName,upsCommands);
|
NginxResponse result = nginxService.dyopsLocal(upsName,upsCommands);
|
||||||
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
|
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
|
||||||
|
@ -78,7 +79,7 @@ public class NginxResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/status")
|
@Path("/status")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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();
|
NginxServerStatus status = nginxService.getStatus();
|
||||||
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
|
if (MediaType.APPLICATION_XML_TYPE.equals(hh.getMediaType())) {
|
||||||
return Response.status(200).entity(String.format(NginxServerStatus.XML, status)).type(MediaType.APPLICATION_XML).build();
|
return Response.status(200).entity(String.format(NginxServerStatus.XML, status)).type(MediaType.APPLICATION_XML).build();
|
||||||
|
@ -90,7 +91,7 @@ public class NginxResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/trafficStatus")
|
@Path("/trafficStatus")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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();
|
TrafficStatusList l = new TrafficStatusList();
|
||||||
for (TrafficStatus status : nginxService.getLocalTrafficStatus()) {
|
for (TrafficStatus status : nginxService.getLocalTrafficStatus()) {
|
||||||
l.addTrafficStatus(status);
|
l.addTrafficStatus(status);
|
||||||
|
@ -101,7 +102,7 @@ public class NginxResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/loadAll/slb/{slbId:[0-9]+}")
|
@Path("/loadAll/slb/{slbId:[0-9]+}")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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);
|
List<NginxResponse> nginxResponseList = nginxService.loadAll(slbId);
|
||||||
NginxResponseList result = new NginxResponseList();
|
NginxResponseList result = new NginxResponseList();
|
||||||
for (NginxResponse nginxResponse : nginxResponseList) {
|
for (NginxResponse nginxResponse : nginxResponseList) {
|
||||||
|
@ -117,7 +118,7 @@ public class NginxResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/allStatus/slb/{slbId:[0-9]+}")
|
@Path("/allStatus/slb/{slbId:[0-9]+}")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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);
|
List<NginxServerStatus> nginxServerStatusList = nginxService.getStatusAll(slbId);
|
||||||
NginxServerStatusList result = new NginxServerStatusList();
|
NginxServerStatusList result = new NginxServerStatusList();
|
||||||
for (NginxServerStatus nginxServerStatus : nginxServerStatusList) {
|
for (NginxServerStatus nginxServerStatus : nginxServerStatusList) {
|
||||||
|
|
|
@ -162,7 +162,7 @@ public class StatusResource {
|
||||||
@GET
|
@GET
|
||||||
@Path("/traffic")
|
@Path("/traffic")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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 {
|
@QueryParam("slbId") Long slbId) throws Exception {
|
||||||
if (slbId == null) {
|
if (slbId == null) {
|
||||||
throw new ValidationException("Missing parameters.");
|
throw new ValidationException("Missing parameters.");
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,8 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<configuration>
|
<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"/>
|
<timestamp key="byDay" datePattern="yyyy-MM-dd_HH:mm"/>
|
||||||
<!-- Output to Console -->
|
<!-- Output to Console -->
|
||||||
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
@ -8,8 +11,26 @@
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</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">
|
<root level="INFO">
|
||||||
<appender-ref ref="Console"/>
|
<appender-ref ref="Console"/>
|
||||||
|
<appender-ref ref="ROLLING"/>
|
||||||
</root>
|
</root>
|
||||||
|
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<configuration>
|
<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"/>
|
<timestamp key="byDay" datePattern="yyyy-MM-dd_HH:mm"/>
|
||||||
<!-- Output to Console -->
|
<!-- Output to Console -->
|
||||||
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
|
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
@ -7,9 +10,26 @@
|
||||||
<pattern>%date %level [%thread] %10logger [%file:%line] %msg%n</pattern>
|
<pattern>%date %level [%thread] %10logger [%file:%line] %msg%n</pattern>
|
||||||
</encoder>
|
</encoder>
|
||||||
</appender>
|
</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">
|
<root level="INFO">
|
||||||
<appender-ref ref="Console"/>
|
<appender-ref ref="Console"/>
|
||||||
|
<appender-ref ref="ROLLING"/>
|
||||||
</root>
|
</root>
|
||||||
|
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
Loading…
Reference in a new issue