How to configure Apache Log4j to write to the Bank through Hibernate?

Asked

Viewed 606 times

5

I am starting to use Log4j from Apache and would like to know how to configure it by XML file to record the logs in the database using Hibernate?

  • 2

    Who denied the question could at least say the reason? Let’s help ourselves personally! :)

  • I fully agree

  • the posting was negative probably because it is a very 'silly' question, don’t get me wrong, but half an hour reading the documentation another reason is that it is a very broad question, instead of asking 'how to use such a tool' direct your question, for example 'how to configure tool x with a database y' responding to your main question, you can find how to configure log4j with xml, properties and directly in java in your official manual, also recommend seeing some examples on the github of the project

  • 2

    maybe I can help you, http://answall.com/a/80340/10315

  • Exactly @re22 , basically everything

1 answer

3


Tiago Ferezin

Recording LOG4J1 logs in a table:

log4j.rootLogger = INFO, DB

# Define the DB appender
log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender

# Set JDBC Options
log4j.appender.DB.URL=jdbc:mysql://localhost/test
log4j.appender.DB.driver=com.mysql.jdbc.Driver
log4j.appender.DB.user=root
log4j.appender.DB.password=password

# Set the SQL statement to be executed.
log4j.appender.DB.sql=insert into logging values('%d{yyyy-MM-dd  HH:mm:ss.SSS}','%C','%p','%m')

# Define the layout for file appender
log4j.appender.DB.layout=org.apache.log4j.PatternLayout

You need to have a table already created to write the data in the database the use remains the same, will only write in the specified table.

Using in the Java class

Logger log = LogManager.getLogger(Teste.class);

log.info("Falha na validação dos parâmetros (" + e.getMessage() + ")");

//ou

log.info("Lote " + paramLote);

Log in activities of HIBERNATE (This part is only to record what Hibernate performs)

I pulled out of this reply on Soen how to get the Hibernate logs, I believe that the levels can also be saved in the base

List of LOGS categories:

Category                    Function

org.hibernate.SQL           Log all SQL DML statements as they are executed
org.hibernate.type          Log all JDBC parameters
org.hibernate.tool.hbm2ddl  Log all SQL DDL statements as they are executed
org.hibernate.pretty        Log the state of all entities (max 20 entities) associated with the session at flush time
org.hibernate.cache         Log all second-level cache activity
org.hibernate.transaction   Log transaction related activity
org.hibernate.jdbc          Log all JDBC resource acquisition
org.hibernate.hql.ast.AST   Log HQL and SQL ASTs during query parsing
org.hibernate.secure        Log all JAAS authorization requests
org.hibernate               Log everything (a lot of information, but very useful for troubleshooting) 

Configuration already formatted to put in your log4j file

 <!-- Registra todas as instruções SQL DML que são executados -->
<Logger name="org.hibernate.SQL" level="debug" />
<!-- Registra todas as instruções os parametros JDBC -->
<Logger name="org.hibernate.type" level="debug" />
<!-- Registra todas as instruções SQL DDLque são executados -->
<Logger name="org.hibernate.tool.hbm2ddl" level="debug" />
<!-- Registra o estado de todas as entidades (máximo de 20 entidades) associados com a sessão em tempo de limpeza -->
<Logger name="org.hibernate.pretty" level="debug" />
<!--Registra todas as atividades cache de segundo nível -->
<Logger name="org.hibernate.cache" level="debug" />
<!-- Registra atividades de transação relacionada -->
<Logger name="org.hibernate.transaction" level="debug" />
<!-- Registra toda aquisição de recursos JDBC -->
<Logger name="org.hibernate.jdbc" level="debug" />
<!-- Registra HQL e SQL durante a análise da consulta -->
<Logger name="org.hibernate.hql.ast.AST" level="debug" />
<!-- Registea todos os pedidos de autorização JAAS -->
<Logger name="org.hibernate.secure" level="debug" />
<!-- Registrar tudo (um monte de informações, mas muito útil para a solução de problemas) -->
<Logger name="org.hibernate" level="debug" />

There are several levels of debugging in LOG4J, you can use the:

<logger name="org.hibernate">
    <level value="ALL" />
    <appender-ref ref="FILE"/>
</logger>

This level of LOG ALL will log everything and should be placed before its root element in the file.

Extra information on Jdbcappender

Extra: If you still need to configure LOG4J in your project there is this reply in Sopt

  • I did so but it is still not persisting in the bank.

  • 1

    Did you create the table and specify everything right? I tested it and it worked fine. Do you have an error? @Tiagoferezin

  • 1

    The key point is to configure the first file to log into the database, the last 3 are just to log in the peculiarities of Hibernate

  • 1

    @Tiagoferezin, you did it?

  • Yes I got it right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.