Reputation: 155
I am learning log4j still and I facing this problem.
As in my current example I not able to print System.out.println statements in logfiles.
I have gone through other question and answers in the Stack Overflow and it is a duplicate question, but I still did not understand those.
Below is Java class
public class Testing
{
private final static Logger logger = Logger.getLogger(Testing.class);
static
{
PropertyConfigurator.configure("log4j.properties");
}
public static void main(String args[])
{
System.out.println("Entered main method...............");
logger.error("An error has occurred might");
}
}
log4j.properties file:
log4j.rootLogger=DEBUG, file, console
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=logs/justfortesting.log
log4j.appender.file.Append=true
log4j.appender.file.ImmediateFlush=true
log4j.appender.FILE.filter.b=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.FILE.filter.b.LevelToMatch=ERROR
log4j.appender.FILE.filter.b.AcceptOnMatch=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d %d{Z} [%t] %-5p (%F:%L) - %m%n
log4j.logger.com.log4j=DEBUG, file, console
log4j.additivity.com.log4j=false
So anyone suggest, what I need to change/add new things to this??
Upvotes: 2
Views: 14817
Reputation: 155
I got the solution for above problem, by modifying my Testing class and added one new class with the existing code.
public class Testing
{
private final static Logger logger = Logger.getLogger(Testing.class);
static
{
PropertyConfigurator.configure("log4j.properties");
}
public static void main(String args[])
{
Logger out = Logger.getLogger("SystemOut");
Level info = Level.INFO;
System.setOut(new PrintStream(new LoggingOutputStream(out, info),true));
System.out.println("Entered main method...............");
System.out.println("Printing 2nd...............");
}
}
Added LoggingOutputStream class for printing System.out.println statements in logfiles.
Upvotes: 2
Reputation: 19451
System.out.println()
writes to the standard output. If you want write these messages to the log, you'll have to call the logger methods:
public static void main(String args[])
{
logger.info("Entered main method...............");
logger.error("An error has occurred might");
}
Upvotes: 0