How do I configure Apache Log4J to write to the Bank via Hibernate?

5

I'm starting to use Apache's Log4J and would like to know how to configure it by the XML file to write the logs to the bank using hibernate?

    
asked by anonymous 26.08.2015 / 20:05

1 answer

3

Tiago Ferezin

Saving the logs LOG4J1 into 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, it will only write to the specified table.

Using 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 HIBERNATE activities (This part is just for registering what hibernate does)

I removed this answer in SOen to get the hibernate logs, I believe that the levels can also be written to the

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:

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

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

Extra information about JDBCappender

Extra: If you still need to configure LOG4J in your project there is this response in SOpt

    
26.08.2015 / 21:08