我開發使用的Servlet/JSP中的演示應用程序日誌和錯誤信息,並通過使用阿帕奇登錄JAR所謂的log4j從http://logging.apache.org/log4j/1.2/關於在web應用程序
我的問題是有關在哪裏使用記錄。任何最佳實踐。?
1 -應該在try/catch中記錄錯誤消息,並且還會拋出異常。看下面的例子?
2 -應該在需要使用System.out.println(「消息」)進行調試或打印信息的地方使用它嗎?
3 -應該在生產中使用它還是僅在開發中使用?
如何在您的應用程序中使用它?
我是對的還是生成無用的消息?
try{
con = ConnectionManager.getConnection();
ps = prepareStatement(con, SQL_DELETE, false, values);
int affectedRows = ps.executeUpdate();
if(affectedRows == 0){
log.error("delete: Deleting user failed, no rows affected");
throw new DAOException("Deleting user failed, no rows affected.");
}else{
user.setId(null);
}
}catch(SQLException e){
log.error("delete: " + e.getMessage());
throw new DAOException(e);
}finally{
close(con, ps);
}
try{
Class.forName(DRIVER);
try{
Class.forName(DRIVER);
log.info("Connecting database...");
con = DriverManager.getConnection(URL, USERNAME, PASSWORD);
log.info("Database connected!");
}catch(SQLException ex){
throw new RuntimeException("Cannot connect the database!", ex);
}
}catch(ClassNotFoundException e){
log.error("Closing ResultSet failed: " + e.getMessage());
System.exit(-1);
}
謝謝。我有屬性文件,但無法設置優先級。你可以在這裏粘貼行,在屬性文件中使用的日誌級別請。 – Pirzada 2012-08-08 00:19:09
日誌記錄級別可以使用log4j.properties下列行來設置: #根記錄選項 log4j.rootLogger = INFO,由於輸出 #直接日誌消息到stdout log4j.appender.stdout = org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target = System.out的 log4j.appender.stdout.layout = org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%d {ABSOLUTE}%5P%C {1 }:%L - %m%n這個文件的好例子可以在這裏找到 - (http://www.mkyong.com/logging/log4j-log4j-properties-examples/) – AshwinN 2012-08-08 17:27:07