0

I've created a web java connect to Database (using MySQL ver5.6). On my web java there are the login panel username & password(the account username & password from DB). Now it's working ok but i want to logging activity ( login true or false) using Apache log4j.properties. Please help me complete it.! Here is my code :

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;

public abstract class DBConnectionDAO {
public static Connection getConnection() {
    String stringConnection = "jdbc:mysql://localhost:3306/database_tai_khoan?user=root&password=password&characterEncoding=UTF-8";
    Connection conn = null;
    try {
        Class.forName("com.mysql.jdbc.Driver");
        // Open connection
        conn = DriverManager.getConnection(stringConnection);
    } catch (ClassNotFoundException e) {
        System.err.println("Class not found! Please review your library");
    } catch (SQLException e) {
        e.printStackTrace();
        System.err.println("Loi truy van");
    }
    return conn;
}
final static Logger logger = Logger.getLogger(DBConnectionDAO.class);

public static void main(String[] args) {

    DBConnectionDAO obj = new DBConnectionDAO() {};
    obj.runMe("hongson");

}

private void runMe(String parameter){

    if(logger.isDebugEnabled()){
        logger.debug("This is debug : " + parameter);
    }

    if(logger.isInfoEnabled()){
        logger.info("This is info : " + parameter);
    }

    logger.warn("This is warn : " + parameter);
    logger.error("This is error : " + parameter);
    logger.fatal("This is fatal : " + parameter);

}

}

1 Answers1

0

Which version of log4j are you using? Did you configure log4.xml or log4j.properties?

You can check this links in order to configure the log:

You also might see the Log4j documentation page in order to understand the appenders.

Community
  • 1
  • 1
dansouza
  • 111
  • 5
  • Thanks bro, now i've done configuring my javaweb and database. But now i cant output logging file ( just output to console ) .Here is my problem : http://stackoverflow.com/questions/26921579/logging-activity-using-apache-log4j – Hồng Sơn Nov 14 '14 at 03:14
  • Have you got this kind of warning? log4j:WARN No appenders could be found for logger. log4j:WARN Please initialize the log4j system properly. If yes, did not initiate log4j properly. You can dig on this post: http://stackoverflow.com/questions/1140358/how-to-initialize-log4j-properly – dansouza Nov 14 '14 at 03:24
  • No i dont receive any warning. It looks still ok without any warning. It just output to console but not output to a file Should i edit the log4j-properties anymore? – Hồng Sơn Nov 14 '14 at 04:05