You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes it is useful to route log messages, created anywhere on the server during a request, to the relevant client, in order to let the client trace the progress of his request.
I wrote a class that accomplishes this, for log4j. It uses the MDC feature. In order to use it, the server should set the MDC value to the actual client, at the beginning of each request, for example,
server.addEventListener("myevent", Request.class, new DataListener<Request>() {
@Override public void onData(SocketIOClient client, Request request, AckRequest ackRequest) {
MDC.put("client", client);
/* code to handle the request and reply to the client */
}
});
The appender can be used from the log4j properties file, for example:
package <package-name>;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import com.corundumstudio.socketio.SocketIOClient;
/**
* Send log messages to web clients.
*
* @author Erel Segal Halevi
* @since 2013-01-01
*/
public class SocketAppender extends AppenderSkeleton {
@Override protected void append(LoggingEvent event) {
SocketIOClient client = (SocketIOClient)event.getMDC("client");
String eventLevel = event.getLevel().toString().toLowerCase();
if (client!=null)
client.sendEvent(eventLevel, event.getLoggerName()+": "+event.getMessage());
}
@Override public boolean requiresLayout() {
return false;
}
@Override public void close() {
// nothing to close
}
}
This will create a different event for each log level, for example, for an "info" log level, the client will receive an "info" event, etc. Possible client code for handling these events:
Sometimes it is useful to route log messages, created anywhere on the server during a request, to the relevant client, in order to let the client trace the progress of his request.
I wrote a class that accomplishes this, for log4j. It uses the MDC feature. In order to use it, the server should set the MDC value to the actual client, at the beginning of each request, for example,
The appender can be used from the log4j properties file, for example:
Here is the appender itself:
This will create a different event for each log level, for example, for an "info" log level, the client will receive an "info" event, etc. Possible client code for handling these events:
The text was updated successfully, but these errors were encountered: