Log4j postgres in Java EE

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 { /** 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); } } } 

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#noconfig for more info. 
+6
source share
1 answer

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

Where do I need to create a configuration file?

Most people create it at the root of the application , but I'm sure your reads just fine.

Where and how to make a link to it?

You really don't need to if the configuration file and libraries are in the classpath, all you need is an instance of Logger.

Is it possible to create a class that connects to postgres and insertin that registers my three parameters?

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

Greetings

+3
source

Source: https://habr.com/ru/post/887718/


All Articles