修改日志格式,打印出连接 id和address

This commit is contained in:
远方夕阳 2018-08-23 12:20:03 +08:00
parent a4c242d824
commit f1d06a36d8
11 changed files with 100 additions and 58 deletions

View File

@ -14,6 +14,5 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry combineaccessrules="false" kind="src" path="/cim-server-sdk"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>

View File

@ -0,0 +1,94 @@
/**
* Copyright 2013-2023 Xia Jun(3979434@qq.com).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************************
* *
* Website : http://www.farsunset.com *
* *
***************************************************************************************
*/
package com.farsunset.cim.sdk.server.filter;
import org.apache.mina.core.filterchain.IoFilterAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.core.write.WriteRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 日志打印添加session 的id和ip address
*/
public class CIMLoggingFilter extends IoFilterAdapter {
private final Logger logger = LoggerFactory.getLogger(CIMLoggingFilter.class);
@Override
public void exceptionCaught(NextFilter nextFilter, IoSession session, Throwable cause) throws Exception {
logger.info("EXCEPTION" + getSessionInfo(session) + ":", cause);
nextFilter.exceptionCaught(session, cause);
}
@Override
public void messageReceived(NextFilter nextFilter, IoSession session, Object message) throws Exception {
logger.info("RECEIVED" + getSessionInfo(session) + ": {}", message);
nextFilter.messageReceived(session, message);
}
@Override
public void messageSent(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception {
logger.info("SENT" + getSessionInfo(session) + ": {}", writeRequest.getOriginalRequest().getMessage());
nextFilter.messageSent(session, writeRequest);
}
@Override
public void sessionCreated(NextFilter nextFilter, IoSession session) throws Exception {
logger.info("CREATED" + getSessionInfo(session));
nextFilter.sessionCreated(session);
}
@Override
public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
logger.info("OPENED" + getSessionInfo(session));
nextFilter.sessionOpened(session);
}
@Override
public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
logger.info("IDLE" + getSessionInfo(session));
nextFilter.sessionIdle(session, status);
}
@Override
public void sessionClosed(NextFilter nextFilter, IoSession session) throws Exception {
logger.info("CLOSED" + getSessionInfo(session));
nextFilter.sessionClosed(session);
}
private String getSessionInfo(IoSession session) {
StringBuilder builder = new StringBuilder();
if (session == null) {
return "";
}
builder.append("[");
builder.append("id:").append(session.getId());
if (session.getRemoteAddress() != null) {
builder.append(" address:").append(session.getRemoteAddress().toString());
}
builder.append("]");
return builder.toString();
}
}

View File

@ -35,10 +35,10 @@ import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.executor.ExecutorFilter;
import org.apache.mina.filter.keepalive.KeepAliveFilter;
import org.apache.mina.filter.keepalive.KeepAliveMessageFactory;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.DefaultSocketSessionConfig;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
import com.farsunset.cim.sdk.server.filter.CIMLoggingFilter;
import com.farsunset.cim.sdk.server.filter.ServerMessageCodecFactory;
import com.farsunset.cim.sdk.server.model.HeartbeatRequest;
import com.farsunset.cim.sdk.server.model.HeartbeatResponse;
@ -83,7 +83,7 @@ public class CIMNioSocketAcceptor extends IoHandlerAdapter implements KeepAliveM
acceptor.getFilterChain().addLast("executor", new ExecutorFilter(Executors.newCachedThreadPool()));
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ServerMessageCodecFactory()));
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
acceptor.getFilterChain().addLast("logger", new CIMLoggingFilter());
acceptor.getFilterChain().addLast("heartbeat", keepAliveFilter);
acceptor.setHandler(this);

View File

@ -21,7 +21,6 @@
*/
package com.farsunset.cim.sdk.server.model;
import java.io.UnsupportedEncodingException;
/**
* websocket握手响应结果
@ -36,12 +35,7 @@ public class HandshakerResponse {
}
public byte[] getBytes() {
try {
return toString().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
return toString().getBytes();
}
@Override

View File

@ -34,7 +34,6 @@ public class HeartbeatResponse implements Serializable {
private static HeartbeatResponse object = new HeartbeatResponse();
private HeartbeatResponse() {
}
public static HeartbeatResponse getInstance() {
@ -44,12 +43,5 @@ public class HeartbeatResponse implements Serializable {
public String toString() {
return TAG;
}
public static void main(String[] a) {
byte[] data = "CR".getBytes();
for(byte v: data) {
System.out.println(v);
}
}
}

View File

@ -101,7 +101,6 @@ public class CIMSession implements Serializable {
}
public void setLongitude(Double longitude) {
setAttribute("longitude", longitude);
this.longitude = longitude;
}
@ -110,7 +109,6 @@ public class CIMSession implements Serializable {
}
public void setLatitude(Double latitude) {
setAttribute("latitude", latitude);
this.latitude = latitude;
}
@ -119,7 +117,6 @@ public class CIMSession implements Serializable {
}
public void setLocation(String location) {
setAttribute("location", location);
this.location = location;
}
@ -152,7 +149,6 @@ public class CIMSession implements Serializable {
public void setChannel(String channel) {
this.channel = channel;
setAttribute("channel", channel);
}
public String getDeviceModel() {
@ -161,14 +157,11 @@ public class CIMSession implements Serializable {
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
setAttribute("deviceModel", deviceModel);
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
setAttribute("deviceId", deviceId);
}
public String getHost() {
@ -181,7 +174,6 @@ public class CIMSession implements Serializable {
public void setBindTime(Long bindTime) {
this.bindTime = bindTime;
setAttribute("bindTime", bindTime);
}
public String getClientVersion() {
@ -190,7 +182,6 @@ public class CIMSession implements Serializable {
public void setClientVersion(String clientVersion) {
this.clientVersion = clientVersion;
setAttribute("clientVersion", clientVersion);
}
public String getSystemVersion() {
@ -199,7 +190,6 @@ public class CIMSession implements Serializable {
public void setSystemVersion(String systemVersion) {
this.systemVersion = systemVersion;
setAttribute("systemVersion", systemVersion);
}
public Long getHeartbeat() {
@ -213,8 +203,6 @@ public class CIMSession implements Serializable {
public void setHost(String host) {
this.host = host;
setAttribute("host", host);
}
public int getApnsAble() {
@ -223,7 +211,6 @@ public class CIMSession implements Serializable {
public void setApnsAble(int apnsAble) {
this.apnsAble = apnsAble;
setAttribute("apnsAble", apnsAble);
}
public int getState() {
@ -232,7 +219,6 @@ public class CIMSession implements Serializable {
public void setState(int state) {
this.state = state;
setAttribute("state", state);
}
public void setAttribute(String key, Object value) {
@ -309,7 +295,6 @@ public class CIMSession implements Serializable {
public void setPackageName(String packageName) {
this.packageName = packageName;
setAttribute("packageName", apnsAble);
}
public String getPackageName() {

View File

@ -14,6 +14,5 @@
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry combineaccessrules="false" kind="src" path="/cim-server-sdk"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>

View File

@ -25,7 +25,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -91,7 +91,6 @@ public class CIMSession implements Serializable {
public void setAccount(String account) {
this.account = account;
setAttribute(CIMConstant.SESSION_KEY, account);
}
@ -100,7 +99,6 @@ public class CIMSession implements Serializable {
}
public void setLongitude(Double longitude) {
setAttribute("longitude", longitude);
this.longitude = longitude;
}
@ -109,7 +107,6 @@ public class CIMSession implements Serializable {
}
public void setLatitude(Double latitude) {
setAttribute("latitude", latitude);
this.latitude = latitude;
}
@ -118,7 +115,6 @@ public class CIMSession implements Serializable {
}
public void setLocation(String location) {
setAttribute("location", location);
this.location = location;
}
@ -127,10 +123,7 @@ public class CIMSession implements Serializable {
}
public void setGid(String gid) {
this.gid = gid;
setAttribute("gid", gid);
}
public String getNid() {
@ -151,8 +144,6 @@ public class CIMSession implements Serializable {
public void setChannel(String channel) {
this.channel = channel;
setAttribute("channel", channel);
}
public String getDeviceModel() {
@ -161,14 +152,10 @@ public class CIMSession implements Serializable {
public void setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
setAttribute("deviceModel", deviceModel);
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
setAttribute("deviceId", deviceId);
}
public String getHost() {
@ -181,7 +168,6 @@ public class CIMSession implements Serializable {
public void setBindTime(Long bindTime) {
this.bindTime = bindTime;
setAttribute("bindTime", bindTime);
}
public String getClientVersion() {
@ -190,7 +176,6 @@ public class CIMSession implements Serializable {
public void setClientVersion(String clientVersion) {
this.clientVersion = clientVersion;
setAttribute("clientVersion", clientVersion);
}
public String getSystemVersion() {
@ -199,7 +184,6 @@ public class CIMSession implements Serializable {
public void setSystemVersion(String systemVersion) {
this.systemVersion = systemVersion;
setAttribute("systemVersion", systemVersion);
}
public Long getHeartbeat() {
@ -213,8 +197,6 @@ public class CIMSession implements Serializable {
public void setHost(String host) {
this.host = host;
setAttribute("host", host);
}
public void setChannel(Channel session) {
@ -227,7 +209,6 @@ public class CIMSession implements Serializable {
public void setApnsAble(int apnsAble) {
this.apnsAble = apnsAble;
setAttribute("apnsAble", apnsAble);
}
public int getStatus() {
@ -236,7 +217,6 @@ public class CIMSession implements Serializable {
public void setStatus(int status) {
this.status = status;
setAttribute("status", status);
}
public void setAttribute(String key, Object value) {
@ -269,7 +249,7 @@ public class CIMSession implements Serializable {
}
public boolean write(Object msg) {
if (session != null) {
if (session != null && session.isActive()) {
return session.writeAndFlush(msg).awaitUninterruptibly(5000);
}
@ -307,7 +287,6 @@ public class CIMSession implements Serializable {
public void setPackageName(String packageName) {
this.packageName = packageName;
setAttribute("packageName", apnsAble);
}
public String getPackageName() {