Problem with log4j configuration

5

How do I configure log4j properties? In my application, I'm getting the following message:

  

log4j: WARN No appenders could be found for logger (org.docx4j.jaxb.Context).

     

log4j: WARN Please initialize the log4j system properly.

     

log4j: WARN See link for more info.

Looking for the internet, I found the following suggestion:

link

However, how do I change the property below to false?

org.apache.catalina.loader.WebappClassLoader.ENABLE_CLEAR_REFERENCES

Below is my configuration file for log4j.properties :

# Set root logger level to DEBUG and its only appender to A1. log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender. log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout. log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.appender.A1.Target=System.out

# An alternative logging format:
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n


#Prevent internal log4j DEBUG messages from polluting the output. log4j.logger.org.apache.log4j.PropertyConfigurator=INFO log4j.logger.org.apache.log4j.config.PropertySetter=INFO log4j.logger.org.apache.log4j.FileAppender=INFO
    
asked by anonymous 12.08.2015 / 15:11

1 answer

5

Duds

Examples of the log4j 1 and 2 configuration.

There is the possibility of doing it only via Java code, if it is of interest to you.

Log4j1 must be in the log4j.properties file within the WEB-INF folder of your project (if it is a web project).

log4j.rootCategory=INFO, stdout, fileout //define o nivel 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%c{1}:%L %-5p %d{yyyy-MM-dd HH:mm:ss} %t - %m%n 

log4j.appender.fileout=org.apache.log4j.DailyRollingFileAppender //eu configurei para todo dia gerar um novo arquivo
log4j.appender.fileout.File=\caminho onde você deseja salvar o arquivo
log4j.appender.file.DatePattern='.'yyyy-MM-dd \configuração para criação do arquivo 
log4j.appender.fileout.Append=true
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %c:%L - %m%n

Log4j2 configuration, this needs to be in XML and be within the source folder of your project

src 
|-pacotes
|-arquivo log4j2.xml

LOG4J.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug">
<Properties>
<Property name="log-path">logs</Property>
</Properties>

<Appenders>
<RollingFile name="file-log" fileName=""// caminho pra o arquivo a ser gerado
filePattern="c:/hrwms/log-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
</Policies>
</RollingFile>

<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="//geralmente uso o nome do pacote ou classe aqui" level="info" additivity="false">
<appender-ref ref="file-log" level="info"/>
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="console"/>
</Root>
</Loggers>
</Configuration>

To illustrate usage in classes:

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

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

//ou

log.info("Lote " + paramLote);

JARS:

log4j-api-2.3.jar

log4j-core-2.3.jar

If you do not use Maven

The settings are very similar from version 1 to version 2, I do not know what your strategy is for the logs, but official site they show right everything you need for a good configuration.

    
12.08.2015 / 15:42