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

2

I want to generate * .log or * .txt files with different dates (eg file-log-22-10-2015.log) inside 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

But 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    link for more info.

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

Test.java class:

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

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

    
asked by anonymous 27.08.2015 / 20:30

1 answer

2

To configure log4j.properties in the Java Web project, you must first put log4j-1.2.17.jar in the folder WEB-INF> lib, make sure that the Web App Library [FeltexLog4J] is in the dependencies and Maven log4j dependencies must be removed, and the log4j.xml file can not exist in the project because it overrides the settings of the properties, by the priority line.

Web App Libraries [FeltexLog4J]

Thelog4j.propertiesfilemustbecreatedinthe/srcfolderoftheprojectwithinJavaResourcesasshownbelow:

Thesettingsshouldbedonethisway,asshowninlog4j.propertiesbelow:

LowLevel(MoreDetails)======================================HighLevel(lessdetail)ALL=>TRACE=>DEBUG=>INFO=>WARN=>ERROR=>FATAL

log4j.properties

#Níveis:ALL,DEBUG,ERROR,FATAL,INFO,OFF,TRACE,WAR#NíveldeLogINFOSaídasA1eConsolelog4j.rootLogger=ALL,A1,Console#Informandoqualotipodegeraçãodoarquivodelog.Nestecasoserádiáriolog4j.appender.A1=org.apache.log4j.DailyRollingFileAppender#OndeserágravadooarquivodeLogequalseráoseunomelog4j.appender.A1.file=caminhodoarquivoasergeradocomonome.#DefiniçãodeumpadrãodesaídadelogparaA1log4j.appender.A1.layout=org.apache.log4j.PatternLayout#Padrãodedatacomo2050-01-31(AnoAnoAnoAno-MesMes-Dia-Dia)log4j.appender.A1.DatePattern='_'dd-MM-yyyy'.log'#DefiniçãodecomoseráexibidacadalilhadeLOGlog4j.appender.A1.layout.ConversionPattern=%p-%d{dd/MM/yyyy-HH:mm:ss}-ProjetoNome-%m%n#Defineotamanhomáximodoarquivologlog4j.appender.A1.MaxFileSize=5MB#Definequantosbackupteránomáximolog4j.appender.A1.MaxBackupIndex=100000#UmanovasaídaparaoLOG,nestecasoaconsoleparaIDE(EclipseoNetBeans)ouprompt(WindowsouLinux)log4j.appender.Console=org.apache.log4j.ConsoleAppender#DefiniçãodeumpadrãodesaídadelogparaConsolelog4j.appender.Console.layout=org.apache.log4j.PatternLayout#UmanovasaídaparaoLOG,nestecasoaconsoleparaIDE(EclipseoNetBeans)ouprompt(WindowsouLinux)log4j.appender.Console.layout.ConversionPattern=%p-%d{dd/MM/yyyy-HH:mm:ss}-ProjetoNome-%m%n

Ithasasemi-finishedandworkingprojectandisstructuredinthewaythatIwasabletodointhislink. FeltexLog4J + log4j-1.2.17.jar

    
02.09.2015 / 19:15