How to configure Log4j to generate a file in the WEB-INF folder with different dates?

Asked

Viewed 3,456 times

2

I want to generate *.log or *.txt files with different dates (e.g., file-log-22-10-2015.log) within the WEB-INF folder of my Javaweb project I created the log4j.proprieties file as follows:

log4j.properties

# Root logger option
log4j.rootLogger=DEBUG, stdout, file

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n

# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=/WEB-INF/logs
log4j.appender.file.DatePattern='.'dd-MM-yyyy
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MM-yyyy HH:mm:ss} %-5p %c{1}:%L - %m%n

However it does not generate and gives the following error in the console:

log4j:WARN No appenders could be found for logger (br.com.meusistema.Classeteste). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

The properties file is in the WEB-INF folder and I am calling the logger inside the normal class as below.

Classeteste.java:

package br.com.meusistema;

import org.apache.log4j.Logger;

public class ClasseTeste{
    private static final Logger log = Logger.getLogger(ClasseTeste.class);

    public void metodoTeste(){
        BasicConfigurator.configure();
        log.info("iniciando Teste");
    }
}

The structure is thus using log4j1

inserir a descrição da imagem aqui

And even after all this does not generate the file. I removed this method from the documentation, and I am using Vraptor.

  • If you are using log4j1 it needs to be inside , if you are using log4j2 the file needs to be in your project’s SRC, and it is missing log4j.appender.file.Append=true I don’t remember if he recognizes the relative path from where you want to record I believe it would be something like log4j.appender.file.File=seucaminho inteiro para o web inf /WEB-INF/logs

  • @Wellingtonavelino ta according to the above structure

  • Changes the File= I didn’t find it in the documentation saying that he can do as you put it, I always speak the way I want. @Tiagoferezin

  • ok tried another way and it worked soon dps of the tests I will post the solution I found @Wellingtonavelino

  • How did you pass the way?

1 answer

2


To configure log4j.properties in a Java Web project first you must place log4j-1.2.17.jar in the folder WEB-INF>lib, you must make sure that the Web App Library library [Feltexlog4j] is in the dependencies and the Maven log4j dependencies must be removed, and there can be no log4j.xml file in the project, because this overrides the properties settings, by the priority line.

Web App Libraries[Feltexlog4j]

inserir a descrição da imagem aqui

The log4j.properties file should be created in the /src folder of the project within Java Resources as represented below:

Estrutura do projeto

The settings should be done this way, as an example of log4j.properties below:

Low Level (More details) ==================================== High Level (Less detail) ALL => TRACE => DEBUG => INFO => WARN => ERROR => FATAL

log4j.properties

# Níveis: ALL, DEBUG, ERROR, FATAL, INFO, OFF,TRACE, WAR

#Nível de Log INFO Saídas A1 e Console
log4j.rootLogger= ALL, A1, Console

#Informando qual o tipo de geração do arquivo de log. Neste caso será diário
log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender

#Onde será gravado o arquivo de Log e qual será o seu nome
log4j.appender.A1.file=caminho do arquivo a ser gerado com o nome.

#Definição de um padrão de saída de log para A1
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

#Padrão de data como 2050-01-31 (AnoAnoAnoAno-MesMes-Dia-Dia)
log4j.appender.A1.DatePattern='_'dd-MM-yyyy'.log'

#Definição de como será exibida cada lilha de LOG
log4j.appender.A1.layout.ConversionPattern=%p - %d{dd/MM/yyyy - HH:mm:ss} - ProjetoNome - %m %n

#Define o tamanho máximo do arquivo log
log4j.appender.A1.MaxFileSize=5MB

#Define quantos backup terá no máximo
log4j.appender.A1.MaxBackupIndex=100000

#Uma nova saída para o LOG, neste caso a console para IDE (Eclipse o NetBeans) ou prompt (Windows ou Linux)
log4j.appender.Console=org.apache.log4j.ConsoleAppender

#Definição de um padrão de saída de log para Console
log4j.appender.Console.layout=org.apache.log4j.PatternLayout

#Uma nova saída para o LOG, neste caso a console para IDE (Eclipse o NetBeans) ou prompt (Windows ou Linux)
log4j.appender.Console.layout.ConversionPattern=%p - %d{dd/MM/yyyy - HH:mm:ss} - ProjetoNome - %m %n

There is a semi project done and working and structured in the way that I was able to do, in this link. Feltexlog4j + log4j-1.2.17.jar

Browser other questions tagged

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