Hi,
I send you the code as it is. It sends output from
the first appender
to the console and the second appender to
file:
import com.foo.Bar;
import
org.apache.log4j.Category;
import org.apache.log4j.PropertyConfigurator; public class MyApp {
static Category cat =
Category.getInstance(MyApp.class.getName());
public static void main(String[] args)
{
// BasicConfigurator replaced with PropertyConfigurator. PropertyConfigurator.configure("log.properties"); cat.info("Entering
application.");
Bar bar = new Bar(); bar.doIt(); cat.info("Exiting application."); cat.shutdown(); } } package com.foo;
import org.apache.log4j.Category; public class Bar {
static Category cat = Category.getInstance(Bar.class); public void doIt() {
cat.warn("Did it again!"); } } ******File: log.properties
log4j.rootCategory=DEBUG
log4j.category.MyApp=INHERITED,A1
log4j.category.com.foo=INHERITED,A2 log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=server.log log4j.appender.A2.layout=org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern=[%c{1}] %m%n log4j.appender.A2.Append=false it should work, try it.
Francesco
# Print the date in ISO 8601 format # Print only messages of priority WARN or above in the package com.foo.
|
Title: AW: [beginner] How to send logs to different Appenders?
- [beginner] How to send logs to different Appenders? Francesco Marchioni
- RE: [beginner] How to send logs to different Appe... Thomas Tuft Muller
- RE: [beginner] How to send logs to different Appe... Jon Skeet
- Re: [beginner] How to send logs to different Appe... Francesco Marchioni
- RE: [beginner] How to send logs to different Appe... Jon Skeet
- Re: [beginner] How to send logs to different Appe... Francesco Marchioni
- RE: [beginner] How to send logs to different Appe... Jon Skeet
- Re: [beginner] How to send logs to different Appe... Francesco Marchioni
- RE: [beginner] How to send logs to different Appe... Francesco Marchioni
- RE: [beginner] How to send logs to different Appe... Jon Skeet