Problem with log4j configuration

Asked

Viewed 1,579 times

5

How do I configure the properties of log4j? In my application, I am receiving 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 http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Searching the internet, I found the following suggestion:

https://tomcat.apache.org/tomcat-5.5-doc/config/systemprops.html#Specification

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

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
  • 1

    Put your configuration, you are using log4J 1 or 2 ?

  • I’m using log4j-1.2.17

  • 1

    Do you have trouble switching to logj2? A few months ago I had a lot of problems with the 1 I switched to the 2 and ended the problems :D, put its configuration I put an answer to how to configure it.

  • I have no problems. I just edited my question by adding the property file, hopefully this is it. If you can help me set it up, I really appreciate it! :D

  • 1

    Did you solve it? @Duds

  • Now it’s worked out! Thank you so much @Wellington Avelino

  • If the answer was helpful, mark as right to help other users.

Show 2 more comments

1 answer

5


Duds

Examples of log4j 1 and 2 configuration.

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

log4j1 must be in the file log4j.properties inside the briefcase WEB-INF 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

Configuration of Log4j2, this needs to be herein XML and stay inside 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 the use in the 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 don’t use Maven

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

Browser other questions tagged

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