log4j - different logger instances

J

John Smith

I want to create a new logger instance that will simultaniusly log to
another file. I was able to do that programatically.
So when I instantiate the logger like this:

----------------------------------------------------
Logger logger = Logger.getLogger("AnotherLogger");
----------------------------------------------------

it writes to a separate log file "AnotherLogFile.txt".

I do not like the idea to hard-code the appender instantiation.

---------------------------------------------------
Layout layout = new PatternLayout("%d %-5p %m%n");
RollingFileAppender rfa = new RollingFileAppender(layout,
"AnotherLogFile.txt");
rfa.setMaxFileSize(10MB);
rfa.setMaxBackupIndex(8);

Logger.getLogger("AnotherLogger").setAdditivity(false);
Logger.getLogger("AnotherLogger").addAppender(rfa);
 
L

Lew

John said:
I want to create a new logger instance that will simultaniusly log to
another file. I was able to do that programatically.
So when I instantiate the logger like this:

----------------------------------------------------
Logger logger = Logger.getLogger("AnotherLogger");
----------------------------------------------------

it writes to a separate log file "AnotherLogFile.txt".

I do not like the idea to hard-code the appender instantiation.

---------------------------------------------------
Layout layout = new PatternLayout("%d %-5p %m%n");
RollingFileAppender rfa = new RollingFileAppender(layout,
"AnotherLogFile.txt");
rfa.setMaxFileSize(10MB);
rfa.setMaxBackupIndex(8);


Logger.getLogger("AnotherLogger").setAdditivity(false);
Logger.getLogger("AnotherLogger").addAppender(rfa);

What have you been able to do with log4j.properties so far?

Try (using your own folders, files and package names, of course):

# logj4.properties
log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X

log4j.appender.A = org.apache.log4j.ConsoleAppender
log4j.appender.A.layout = org.apache.log4j.PatternLayout

log4j.appender.F = org.apache.log4j.RollingFileAppender
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.File = /projects/mouser/logs/lewscanon.log
log4j.appender.F.MaxFileSize = 512KB
log4j.appender.F.MaxBackupIndex = 2

log4j.appender.X = org.apache.log4j.RollingFileAppender
log4j.appender.X.layout = org.apache.log4j.PatternLayout
log4j.appender.X.File = /projects/mouser/logs/mouser.log
log4j.appender.X.MaxFileSize = 512KB
log4j.appender.X.MaxBackupIndex = 2

log4j.appender.A.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.F.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.X.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
 
J

John Smith

What have you been able to do with log4j.properties so far?

Try (using your own folders, files and package names, of course):

# logj4.properties
log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X

log4j.appender.A = org.apache.log4j.ConsoleAppender
log4j.appender.A.layout = org.apache.log4j.PatternLayout

log4j.appender.F = org.apache.log4j.RollingFileAppender
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.File = /projects/mouser/logs/lewscanon.log
log4j.appender.F.MaxFileSize = 512KB
log4j.appender.F.MaxBackupIndex = 2

log4j.appender.X = org.apache.log4j.RollingFileAppender
log4j.appender.X.layout = org.apache.log4j.PatternLayout
log4j.appender.X.File = /projects/mouser/logs/mouser.log
log4j.appender.X.MaxFileSize = 512KB
log4j.appender.X.MaxBackupIndex = 2

log4j.appender.A.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.F.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.X.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n

Thank you very much, this works.
I modified a little not to use package logging. I use another logger
instead that covers all the packages, but writes to a different file.


log4j.category.AnotherLogger=WARN, F
log4j.additivity.AnotherLogger=false

log4j.appender.F=org.apache.log4j.RollingFileAppender
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.File = /projects/mouser/logs/lewscanon.log
log4j.appender.F.MaxFileSize = 512KB
log4j.appender.F.MaxBackupIndex = 2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top