Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Adding a appender to log4j at runtime

Posted on 2005-03-17
6
Medium Priority
?
1,409 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

578 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