After many searches on the topic, I finally think that the solution to my problem is to ask you.
So my problem is creating logs when using my web application.
I found the java library log4j, but I do not understand how this works.
Where do I need to create a configuration file?
Where and how to make a link to it?
Is it possible to create a class that connects to postgres and insertin that registers 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 { private static Logger loggerDB = Logger.getLogger("loggerDB"); public static void enregistreLog(String user, String action, String sujet) {
call in my package
LogsInfos.enregistreLog((String)session.getAttribute("cn"),"Suppression",personne.getCn());
Can I use an object of my choice?
Is this code a solution? if so, where do I need to call the configuration file?
EDIT: This is a 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