?
Solved

Adding a appender to log4j at runtime

Posted on 2005-03-17
6
Medium Priority
?
1,402 Views
Last Modified: 2012-08-13
Hello,

I am new to the log4j package and have a need to add log files at runtime.   From looking through the documentation I see that all the logs are configured via a config file.   The app will have many users and I would like to create a log file for each user at runtime.  I won’t know in advance who the users are.

I thought that I could create a basic rotating file logger and then add appenders to it, but I don’t see a way via the javadocs to create an appender at runtime, just ways to get references to preconfigured appenders.

Is there a way to do this?   I guess I could create unique loggers from scratch via code each time a user logs on, but I liked being able to configure the root logger in the config file.  The way I understand the docs, a appender will inherit the attributes of the root logger.

Many thanks,
-Scott
0
Comment
Question by:snaef
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 13568124
Try something like:


LogManager.getRootLogger().addAppender(yourNewAppender);
0
 
LVL 15

Accepted Solution

by:
aozarov earned 600 total points
ID: 13570774
Here is an example of how to add Snmp appender
        Logger logger = Logger.getLogger(TestLog4jAdaptor.class); // or call Logger.getRootLogger()

        SNMPTrapAppender appender = new SNMPTrapAppender();
        appender.setImplementationClassName(JoeSNMPTrapSender.class.getName());
        appender.setManagementHost("localhost");
        appender.setManagementHostTrapListenPort(6767);
        appender.setEnterpriseOID(".1.3.6.1.4.1.2.3.4.5");
        appender.setGenericTrapType(3);
        appender.setSpecificTrapType(10);
        appender.setTrapVersion(1);

        PatternLayout layout = new PatternLayout();
        layout.setConversionPattern("=\"%d,%p,[%t],[%c],%m%n");
        appender.setLayout(layout);
       
        logger.addAppender(appender);
0
 
LVL 1

Author Comment

by:snaef
ID: 13587210
Ok, just one quick question.   If I add a new rolling file logger and the filename already exists (i.e its still the same date and the use has returned to the site) will the logger append text to the existing file or overwrite the old file.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:CEHJ
ID: 13587221
Append to the existing i think
0
 
LVL 1

Author Comment

by:snaef
ID: 13592607
Thanks for the help, slit points as both were basically the same answer, but  the latter was more detailed.   Thanks again!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13594386
:-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 5 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question