Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log4j postgres in Java EE

After many search on the subject I finally think the solution of my problem is to ask you.

So my problem is in the way to create logs on my web application utilisation.

I found the log4j java library but I don't understand how that works..

Where I have to create configuration file ?

Where and how to make reference on it?

Can I create a class that connect to postgres and insertin log my three parameters?

Here is what I found on the net :

./src/log4j/log4j.properties

log4j.rootCategory = FATAL, CONSOLE
# definition de l'appender console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c - %m%n

# definition de l'appender JDBC
log4j.appender.JDBC=org.apache.log4j.jdbcplus.JDBCAppender
log4j.appender.JDBC.layout=org.apache.log4j.PatternLayout
# appender pour base postgresql
log4j.appender.JDBC.dbclass=org.postgresql.Driver
# parametres de la base
log4j.appender.JDBC.url=jdbc:postgresql://127.0.0.1:5432/baseSQL
log4j.appender.JDBC.username=user
log4j.appender.JDBC.password=password
# requete sql qui decoupe le message suivant les barres verticales et fait l'insert dans la table
log4j.appender.JDBC.sql=INSERT INTO logs (id, user, info1, info2, timestamp) VALUES (nextval('sequence_logs'), split_part('@MSG@','|',1), split_part('@MSG@','|',2), split_part('@MSG@','|',3), '@TIMESTAMP@')

#declaration des loggers de l'application
log4j.logger.paquetage.de.mon.appli=FATAL, CONSOLE
log4j.logger.loggerDB=INFO,JDBC
# definition de non additivite des loggers
log4j.additivity.loggerDB=false

src/log4j/LogsInfos.java

package log4j;

import org.apache.log4j.Logger;

public final class LogsInfos {

    /** Declaration du Logger DB. */
    private static Logger loggerDB = Logger.getLogger("loggerDB");

    /**
     * Enregistre le log.
     * @param param parametres du log
     */
    public static void enregistreLog(String user, String action, String sujet) {
//      Date date=new Date();
        if (loggerDB.isInfoEnabled()) {
            // creation du message final
            final String log = new StringBuffer(user).append(action).append(sujet).toString();
            // envoi du log au logger base
            loggerDB.info(log);
        }
    }
}

the call in my package

LogsInfos.enregistreLog((String)session.getAttribute("cn"),"Suppression",personne.getCn());

Can I use the object of my choice?

This code is the solution? if it is, where I have to call the configuration file?

EDIT: This is the console error message on execution:

log4j:ERROR Could not instantiate class [org.apache.log4j.jdbcplus.JDBCAppender].
java.lang.ClassNotFoundException: org.apache.log4j.jdbcplus.JDBCAppender
...
log4j:ERROR Could not instantiate appender named "JDBC".
log4j:WARN No appenders could be found for logger (loggerDB).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
like image 994
a11r Avatar asked May 10 '11 12:05

a11r


1 Answers

You are missing jdbcappender.jar in your classpath. I think you are looking for this one. Or you can go with the official JDBCAppender, but I'm not sure if is powerful engough for your needs.

Where I have to create configuration file ?

Most people create it at the application root, but I'm sure yours is being read just fine.

Where and how to make reference on it?

You really dont need to, if the configuration file and the libraries are in the classpath, all you need is a Logger instance.

Can I create a class that connect to postgres and insertin log my three parameters?

Yes. You are in the right track. Just put the required jars in the classpath and make sure your log4j.appender.JDBC.sql line works. You will use the Logger class methods which will take care of everything for you.

Cheers,

like image 74
Anthony Accioly Avatar answered Oct 30 '22 00:10

Anthony Accioly