java - log4j not writing to file -
the log file has been created in specified location contents not entered in file. empty.
import org.apache.log4j.logger; public class logfile { public static void log(string msg){ system.out.println("logfileee==="+msg); logger logger = logger.getlogger(logfile.class); logger.info(msg); } } log4j.rootlogger=info, r log4j.appender.a1=org.apache.log4j.consoleappender log4j.appender.a1.layout=org.apache.log4j.patternlayout log4j.appender.a1.layout.conversionpattern=%d{hh:mm:ss,sss} %-5p [%c] - %m%n log4j.appender.r = org.apache.log4j.dailyrollingfileappender log4j.appender.r.append = true log4j.appender.r.datepatter n = '.'yyy-mm-dd log4j.appender.r.file = d:/logs/testing.log log4j.appender.r.layout.conversionpattern = %d{yyyy-mm-dd hh:mm:ss} %c{1} [%p] %m%n log4j.category.datanucleus.jdo=warn, a1 log4j.category.datanucleus.persistence=warn, a1 log4j.category.datanucleus.connection=warn, a1 log4j.category.datanucleus.cache=warn, a1 log4j.category.datanucleus.metadata=warn, a1 log4j.category.datanucleus.general=warn, a1 log4j.category.datanucleus.utility=warn, a1 log4j.category.datanucleus.transaction=warn, a1 log4j.category.datanucleus.datastore=warn, a1 log4j.category.datanucleus.classloading=warn, a1 log4j.category.datanucleus.plugin=warn, a1 log4j.category.datanucleus.valuegeneration=warn, a1 log4j.category.datanucleus.enhancer=warn, a1 log4j.category.datanucleus.schematool=warn, a1 log4j.category.datanucleus.query=warn, a1 log4j.category.datanucleus.reachability=warn, a1 log4j.category.datanucleus.lifecycle=warn, a1 log4j.logger.org.springframework=info, a1
iam gettting file testing.log created in specified location knows have done wrong????
your properties file having many entries. suggest create clean file 2 appender: file , console , see going on. try turn file appender off , see if messages getting printed on console. , further turn on file appender. make sure properties file in classpath.
here tried , working both console , log file appender.
log4j properties file:
# root logger option log4j.rootlogger=info, file, stdout # direct log messages log file log4j.appender.file=org.apache.log4j.rollingfileappender log4j.appender.file.file=/tmp/logging.log log4j.appender.file.maxfilesize=10mb log4j.appender.file.maxbackupindex=10 log4j.appender.file.layout=org.apache.log4j.patternlayout log4j.appender.file.layout.conversionpattern=%d{yyyy-mm-dd hh:mm:ss} %-5p %c{1}:%l - %m%n # direct log messages stdout 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{yyyy-mm-dd hh:mm:ss} %-5p %c{1}:%l - %m%n
sample program:
import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.util.properties; import org.apache.log4j.logger; import org.apache.log4j.propertyconfigurator; public class log4jtest { static final string log_properties_file = "config/log4j.properties"; private final static logger logger = logger.getlogger(log4jtest.class); public static void main(string[] args) { // call our constructor log4jtest log4jtest = new log4jtest(); log4jtest.initializelogger(); // log4j loaded; try logger.info("leaving main method of log4jdemo"); } private void initializelogger() { properties logproperties = new properties(); try { classloader classloader = getclass().getclassloader(); file file = new file(classloader.getresource(log_properties_file).getfile()); // load our log4j properties / configuration file logproperties.load(new fileinputstream(file)); propertyconfigurator.configure(logproperties); logger.info("logging initialized."); } catch (ioexception e) { throw new runtimeexception("unable load logging property " + log_properties_file); } } }
pom file
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>com.mysample</groupid> <artifactid>java-samples</artifactid> <version>0.0.1-snapshot</version> <name>java-samples</name> <description>java-samples</description> <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> <dependencies> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> </dependencies>
Comments
Post a Comment