Permalink
Browse files

Issue #34. Add code to configure logging. AdminServlet looks in the c…

…onfiguration file for a "logLevel" property and applies to the "org.coweb" namespace logger. Also convert all commented out System.out calls to log.fine calls.
  • Loading branch information...
1 parent ca2aa17 commit 23e7decb3000cf0d9e888fa6a6630022d99c1f67 @rbackhouse rbackhouse committed Mar 15, 2012
View
18 servers/java/coweb-server/src/main/java/org/coweb/LateJoinHandler.java
@@ -124,7 +124,7 @@ public void onClientJoin(ServerSession client, Message message) {
// ArrayList<Object>data = new ArrayList<Object>();
Object[] data = new Object[0];
- // System.out.println("data = " + data);
+ log.fine("data = " + data);
boolean sendState = false;
Map<String, Object> ext = message.getExt();
@@ -178,8 +178,8 @@ public void onUpdaterSendState(ServerSession client, Message message) {
this.updatees.remove(token);
if (this.cacheState) {
this.lastState = (Object[]) data.get("state");
- System.out.println("got state from client");
- System.out.println(JSON.toString(this.lastState));
+ log.fine("got state from client");
+ log.fine(JSON.toString(this.lastState));
}
@@ -239,7 +239,7 @@ protected void addUpdater(Session serverSession, boolean notify) {
}
// serverSession.setAttribute("username", clientId);
- // System.out.println("adding " + clientId + " to list of updaters");
+ log.fine("adding " + clientId + " to list of updaters");
this.updaters.put(clientId, new ArrayList<String>());
if (notify) {
@@ -267,7 +267,7 @@ private void sendRosterAvailable(Session client) {
}
protected void sendRosterUnavailable(ServerSession client) {
- // System.out.println("CollabSessionHandler::sendRosterAvailable");
+ log.fine("CollabSessionHandler::sendRosterAvailable");
/* create channel */
BayeuxServer server = this.sessionManager.getBayeux();
ServerChannel.Initializer initializer = new ServerChannel.Initializer() {
@@ -294,7 +294,7 @@ public void configureChannel(ConfigurableServerChannel channel) {
data.put("siteId", siteId);
data.put("username", username);
- // System.out.println(data);
+ log.fine(data.toString());
channel.publish(from, data, null);
}
@@ -412,7 +412,7 @@ private void assignUpdater(Session updatee, String updaterType) {
}
protected void removeUpdater(ServerSession client) {
- // System.out.println("CollabDelegate::removeUpdater " + client);
+ log.fine("CollabDelegate::removeUpdater " + client);
this.removeSiteForClient(client);
List<String> tokenList = this.updaters.get(client.getId());
@@ -425,10 +425,10 @@ protected void removeUpdater(ServerSession client) {
}
}
} else {
- // System.out.println("sending roster unavailable");
+ log.fine("sending roster unavailable");
this.sendRosterUnavailable(client);
if (!tokenList.isEmpty()) {
- // System.out.println("this updater was updating someone");
+ log.fine("this updater was updating someone");
for (String token : tokenList) {
Session updatee = this.updatees.get(token);
if (updatee == null)
View
14 servers/java/coweb-server/src/main/java/org/coweb/ServiceHandler.java
@@ -23,20 +23,20 @@
private Map<String, Object> cowebConfig = null;
public ServiceHandler(String sessionId, Map<String, Object> config) {
- System.out.println("ServiceHandler new Instance " + sessionId);
+ log.info("ServiceHandler new Instance " + sessionId);
this.sessionId = sessionId;
this.cowebConfig = config;
}
@SuppressWarnings("unchecked")
public Transport getServiceBroker(String serviceName) {
- //log.info("ServiceHandler::getServiceBroker for " + serviceName);
+ log.fine("ServiceHandler::getServiceBroker for " + serviceName);
Transport broker = this.brokers.get(serviceName);
if (broker != null)
return broker;
- //log.info(this.cowebConfig.get("bots").toString());
+ log.fine(this.cowebConfig.get("bots").toString());
Object[] botConfigs = (Object[]) this.cowebConfig.get("bots");
if (botConfigs == null) {
return null;
@@ -94,7 +94,7 @@ public void removeUserFromAll(ServerSession client) {
public void shutdown() {
- // System.out.println("ServiceHandler::shutdown");
+ log.fine("ServiceHandler::shutdown");
for (Transport transport : this.brokers.values()) {
transport.shutdown();
@@ -106,7 +106,7 @@ public void shutdown() {
public void subscribeUser(ServerSession client, Message message)
throws IOException {
- // System.out.println("ServiceHandler::subscribeUser");
+ log.fine("ServiceHandler::subscribeUser");
String channel = (String) message.get(Message.SUBSCRIPTION_FIELD);
boolean pub = true;
@@ -130,7 +130,7 @@ public void subscribeUser(ServerSession client, Message message)
public void unSubscribeUser(ServerSession client, Message message)
throws IOException {
- // System.out.println("ServiceHandler::unSubscribeUser");
+ log.fine("ServiceHandler::unSubscribeUser");
String channel = (String) message.get(Message.SUBSCRIPTION_FIELD);
boolean pub = true;
@@ -169,7 +169,7 @@ public void forwardUserRequest(ServerSession client, Message message)
public void forwardSyncEvent(ServerSession client, Message message)
throws IOException {
- // System.out.println("fowardSyncEvent");
+ log.fine("fowardSyncEvent");
for (Transport t : this.brokers.values()) {
t.syncEvent(client, message);
View
2 servers/java/coweb-server/src/main/java/org/coweb/SessionHandler.java
@@ -342,7 +342,7 @@ public void removeBadClient(ServerSession client) {
}
public void endSession() {
- // System.out.println("SessionHandler::endSession ***********");
+ log.fine("SessionHandler::endSession ***********");
log.info("end session");
ServerChannel sync = this.server.getChannel(this.syncAppChannel);
View
16 servers/java/coweb-server/src/main/java/org/coweb/SessionManager.java
@@ -96,7 +96,7 @@ public static String getSessionIdFromMessage(Message message) {
public SessionHandler getSessionHandler(Message message) {
String sessionId = getSessionIdFromMessage(message);
- // System.out.println("sessionId = " + sessionId);
+ log.fine("sessionId = " + sessionId);
return this.getSessionHandler(sessionId);
}
@@ -139,11 +139,11 @@ public SessionHandler getSessionHandler(String sessionId) {
public void handleSubscribed(ServerSession serverSession, Message message)
throws IOException {
- // System.out.println("SessionManager::handleSubscribed");
- // System.out.println(message);
+ log.fine("SessionManager::handleSubscribed");
+ log.fine(message.toString());
SessionHandler handler = this.getSessionHandler(message);
- // System.out.println("handler = " + handler);
+ log.fine("handler = " + handler);
if (handler != null)
handler.onSubscribe(serverSession, message);
@@ -167,11 +167,11 @@ public void handleMessage(ServerSession remote, Message message) {
else
handler = this.getSessionHandler(sessionId);
- // System.out.println(handler);
if (handler != null) {
+ log.fine(handler.toString());
handler.onPublish(remote, message);
} else {
- // System.out.println("could not find handler");
+ log.fine("could not find handler");
}
}
@@ -230,15 +230,15 @@ public void disconnectClient(String sessionId, String siteId) {
@Override
public void sessionAdded(ServerSession client) {
- // System.out.println("session added " + client);
+ log.fine("session added " + client);
return;
// TODO Auto-generated method stub
}
@Override
public void sessionRemoved(ServerSession client, boolean timeout) {
- // System.out.println("SessionManager::sessionRemoved");
+ log.fine("SessionManager::sessionRemoved");
String sessionId = (String) client.getAttribute("sessionid");
SessionHandler handler = this.getSessionHandler(sessionId);
View
36 servers/java/coweb-server/src/main/java/org/coweb/bots/transport/LocalTransport.java
@@ -8,16 +8,20 @@
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
+import java.util.logging.Logger;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.server.ConfigurableServerChannel;
import org.cometd.bayeux.server.ServerChannel;
import org.cometd.bayeux.server.ServerSession;
+import org.coweb.SessionHandler;
import org.coweb.bots.Bot;
import org.coweb.bots.Proxy;
public class LocalTransport extends Transport implements Proxy {
+ private static final Logger log = Logger.getLogger(LocalTransport.class
+ .getName());
private Bot bot = null;
private Map<String, ServerSession> clients = new HashMap<String, ServerSession>();
@@ -54,7 +58,7 @@ public boolean subscribeUser(ServerSession client, Message message,
public boolean unsubscribeUser(ServerSession client, Message message,
boolean pub) throws IOException {
- // System.out.println("LocalTransport::unSubscribeUser");
+ log.fine("LocalTransport::unSubscribeUser");
if (this.bot == null) {
this.bot = this.getBotInstance();
@@ -74,7 +78,7 @@ public boolean unsubscribeUser(ServerSession client, Message message,
@Override
public void shutdown() {
- // System.out.println("LocalTransport::shutdown");
+ log.fine("LocalTransport::shutdown");
this.clients.clear();
this.subscribers.clear();
if (this.bot != null)
@@ -84,8 +88,8 @@ public void shutdown() {
@Override
public boolean userRequest(ServerSession client, Message message)
throws IOException {
- // System.out.println("LocalTransport::userRequest");
- // System.out.println("message = " + message);
+ log.fine("LocalTransport::userRequest");
+ log.fine("message = " + message);
Map<String, Object> data = message.getDataAsMap();
@SuppressWarnings("unchecked")
@@ -109,8 +113,8 @@ public boolean userRequest(ServerSession client, Message message)
@Override
public boolean syncEvent(ServerSession client, Message message)
throws IOException {
- // System.out.println("LocalTransport::syncEvent");
- // System.out.println("message = " + message);
+ log.fine("LocalTransport::syncEvent");
+ log.fine("message = " + message);
Map<String, Object> data = message.getDataAsMap();
@@ -131,12 +135,12 @@ public boolean syncEvent(ServerSession client, Message message)
@Override
public void reply(Bot bot, String replyToken, Map<String, Object> obj) {
- // System.out.println("LocalTransport::reply");
- // System.out.println("reply data = " + obj);
+ log.fine("LocalTransport::reply");
+ log.fine("reply data = " + obj);
ServerSession client = this.clients.get(replyToken);
if (client == null) {
- // System.out.println("LocalTransport::error sending bot reply client not found");
+ log.fine("LocalTransport::error sending bot reply client not found");
// TODO send error.
return;
}
@@ -148,9 +152,9 @@ public void reply(Bot bot, String replyToken, Map<String, Object> obj) {
// HashMap<String, Object> payload = new HashMap<String, Object>();
// payload.put("data", data);
- // System.out.println("LocalTransport::reply");
- // System.out.println("payload = " + data);
- // System.out.println("replyToken = " + replyToken);
+ log.fine("LocalTransport::reply");
+ log.fine("payload = " + data);
+ log.fine("replyToken = " + replyToken);
client.deliver(this.server, "/service/bot/" + this.serviceName
+ "/response", data, null);
@@ -159,7 +163,7 @@ public void reply(Bot bot, String replyToken, Map<String, Object> obj) {
@Override
public void publish(Bot bot, Map<String, Object> obj) {
- // System.out.println("LocalTransport::publish");
+ log.fine("LocalTransport::publish");
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("value", obj);
@@ -172,9 +176,9 @@ public void publish(Bot bot, Map<String, Object> obj) {
client.deliver(this.server, channel.getId(), data, null);
}
- // System.out.println("LocalTransport::publish");
- // System.out.println(data);
- // System.out.println("channel = " + channel);
+ log.fine("LocalTransport::publish");
+ log.fine(data.toString());
+ log.fine("channel = " + channel);
/*
* Set<? extends ServerSession> clients = channel.getSubscribers();
* for(ServerSession client: clients) {
View
39 servers/java/coweb-server/src/main/java/org/coweb/servlet/AdminServlet.java
@@ -13,6 +13,8 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.logging.ConsoleHandler;
+import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
@@ -58,7 +60,6 @@
public void init() throws ServletException {
super.init();
- log.info("servlet init");
ServletContext servletContext = this.getServletContext();
// get the bayeux server and register the bayeux ack extension.
@@ -75,7 +76,35 @@ public void init() throws ServletException {
e1.printStackTrace();
cowebConfig = new HashMap<String, Object>();
}
+
+ int logLevel = 0;
+ if (cowebConfig.get("logLevel") != null) {
+ Long level = (Long)cowebConfig.get("logLevel");
+ logLevel = level.intValue();
+ }
+
+ Logger parentLogger = Logger.getLogger("org.coweb");
+ parentLogger.setUseParentHandlers(false);
+ ConsoleHandler consoleHandler = new ConsoleHandler();
+ parentLogger.addHandler(consoleHandler);
+
+ switch (logLevel) {
+ case 0:
+ parentLogger.setLevel(Level.WARNING);
+ consoleHandler.setLevel(Level.WARNING);
+ break;
+ case 1:
+ parentLogger.setLevel(Level.INFO);
+ consoleHandler.setLevel(Level.INFO);
+ break;
+ case 2:
+ parentLogger.setLevel(Level.FINE);
+ consoleHandler.setLevel(Level.FINE);
+ break;
+ }
+
+ log.info("servlet init");
log.info("cowebConfig = " + cowebConfig.toString());
// setup any debug options for capturing incoming and outgoing
@@ -120,8 +149,8 @@ public void init() throws ServletException {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
- // System.out.println("AdminServlet::gotGet ***********");
- // System.out.println(req.getRequestURL());
+ log.fine("AdminServlet::gotGet ***********");
+ log.fine(req.getRequestURL().toString());
log.info("received admin rest call");
if (req.getRequestURL().indexOf("disconnect") != -1) {
log.info("received disconnect rest call");
@@ -255,9 +284,9 @@ public void doPost(HttpServletRequest req, HttpServletResponse resp)
private void _handleDisconnect(HttpServletRequest req,
HttpServletResponse resp) {
- // System.out.println("AdminServlet::_handleDisconnect ***********");
+ log.fine("AdminServlet::_handleDisconnect ***********");
String path = req.getPathInfo();
- // System.out.println("path info = " + path);
+ log.fine("path info = " + path);
if (path == null)
return;

0 comments on commit 23e7dec

Please sign in to comment.