Updated LogUtil and added example properties file
This commit is contained in:
parent
63f191db4c
commit
3419ba8864
5 changed files with 64 additions and 33 deletions
BIN
Zutil.jar
BIN
Zutil.jar
Binary file not shown.
|
|
@ -24,6 +24,10 @@
|
|||
|
||||
package zutil.log;
|
||||
|
||||
import zutil.io.file.FileUtil;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.util.Enumeration;
|
||||
import java.util.logging.*;
|
||||
|
||||
/**
|
||||
|
|
@ -40,13 +44,13 @@ public class LogUtil {
|
|||
* @return a new Logger for the calling class
|
||||
*/
|
||||
public static Logger getLogger(){
|
||||
return Logger.getLogger(getCalingClass());
|
||||
return Logger.getLogger(getCallingClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the parent class other than Logger in the stack
|
||||
*/
|
||||
public static String getCalingClass(){
|
||||
public static String getCallingClass(){
|
||||
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
|
||||
for(int i=1; i<stackTraceElements.length ;++i){
|
||||
String name = stackTraceElements[i].getClassName();
|
||||
|
|
@ -108,21 +112,36 @@ public class LogUtil {
|
|||
*/
|
||||
public static void setLevel(String name, Level level){
|
||||
logger.fine("Changing log level of \""+name+"\" to \""+level.getLocalizedName()+"\"");
|
||||
Logger logger = Logger.getLogger(name);
|
||||
logger.setLevel(level);
|
||||
Logger newLogger = Logger.getLogger(name);
|
||||
newLogger.setLevel(level);
|
||||
newLogger.info("Changing log level of \""+name+"\" to \""+level.getLocalizedName()+"\"");
|
||||
// Check if the logger has a handler
|
||||
if( logger.getHandlers().length == 0 ){
|
||||
if( newLogger.getHandlers().length == 0 ){
|
||||
// Create a new console handler
|
||||
ConsoleHandler handler = new ConsoleHandler();
|
||||
handler.setLevel( level );
|
||||
logger.addHandler( handler );
|
||||
logger.setUseParentHandlers( false );
|
||||
newLogger.addHandler( handler );
|
||||
newLogger.setUseParentHandlers( false );
|
||||
}
|
||||
else{
|
||||
// Set the level on the handlers
|
||||
for (Handler handler : logger.getHandlers()) {
|
||||
for (Handler handler : newLogger.getHandlers()) {
|
||||
handler.setLevel(level);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isLoggable(Class clazz, Level level){
|
||||
return Logger.getLogger(clazz.getName()).isLoggable(level);
|
||||
}
|
||||
|
||||
public static void readConfiguration(String file){
|
||||
try{
|
||||
FileInputStream in = new FileInputStream(FileUtil.find(file));
|
||||
LogManager.getLogManager().readConfiguration(in);
|
||||
in.close();
|
||||
} catch (Exception e){
|
||||
logger.log(Level.SEVERE, null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
10
src/zutil/log/logging.properties.example
Executable file
10
src/zutil/log/logging.properties.example
Executable file
|
|
@ -0,0 +1,10 @@
|
|||
# logging.properties
|
||||
# LogUtil.readConfiguration("logging.properties");
|
||||
|
||||
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
|
||||
java.util.logging.ConsoleHandler.level = ALL
|
||||
java.util.logging.ConsoleHandler.formatter = zutil.log.CompactLogFormatter
|
||||
java.util.logging.FileHandler.level = ALL
|
||||
java.util.logging.FileHandler.formatter = zutil.log.CompactLogFormatter
|
||||
|
||||
zutil.level = ALL
|
||||
|
|
@ -230,11 +230,14 @@ public class HttpServer extends ThreadedTCPNetworkServer{
|
|||
out.setCookie( "session_id", ""+client_session.get("session_id") );
|
||||
|
||||
if( parser.getRequestURL() != null && pages.containsKey(parser.getRequestURL()) ){
|
||||
pages.get(parser.getRequestURL()).respond(out, parser, client_session, cookie, request);
|
||||
HttpPage page = pages.get(parser.getRequestURL());
|
||||
page.respond(out, parser, client_session, cookie, request);
|
||||
if(LogUtil.isLoggable(page.getClass(), Level.FINER))
|
||||
logRequest(parser, client_session, cookie, request, time);
|
||||
}
|
||||
else if( parser.getRequestURL() != null && defaultPage != null ){
|
||||
defaultPage.respond(out, parser, client_session, cookie, request);
|
||||
if(LogUtil.isLoggable(defaultPage.getClass(), Level.FINER))
|
||||
logRequest(parser, client_session, cookie, request, time);
|
||||
}
|
||||
else{
|
||||
|
|
@ -262,7 +265,6 @@ public class HttpServer extends ThreadedTCPNetworkServer{
|
|||
}
|
||||
|
||||
try{
|
||||
//logger.finest("Closing Connection: "+socket.getInetAddress().getHostName());
|
||||
out.close();
|
||||
in.close();
|
||||
socket.close();
|
||||
|
|
@ -270,8 +272,9 @@ public class HttpServer extends ThreadedTCPNetworkServer{
|
|||
logger.log(Level.WARNING, "Could not close connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void logRequest(HttpHeaderParser parser,
|
||||
protected static void logRequest(HttpHeaderParser parser,
|
||||
Map<String,Object> client_session,
|
||||
Map<String,String> cookie,
|
||||
Map<String,String> request,
|
||||
|
|
@ -291,4 +294,3 @@ public class HttpServer extends ThreadedTCPNetworkServer{
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
2
test/zutil/test/SSDPClientTest.java
Normal file → Executable file
2
test/zutil/test/SSDPClientTest.java
Normal file → Executable file
|
|
@ -35,7 +35,7 @@ import java.util.logging.Level;
|
|||
public class SSDPClientTest {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
System.out.println(LogUtil.getCalingClass());
|
||||
System.out.println(LogUtil.getCallingClass());
|
||||
LogUtil.setGlobalLevel(Level.FINEST);
|
||||
SSDPClient ssdp = new SSDPClient();
|
||||
ssdp.requestService("upnp:rootdevice");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue