Adding a appender to log4j at runtime

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
LVL 1
snaefAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Try something like:


LogManager.getRootLogger().addAppender(yourNewAppender);
0
aozarovCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snaefAuthor Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

CEHJCommented:
Append to the existing i think
0
snaefAuthor Commented:
Thanks for the help, slit points as both were basically the same answer, but  the latter was more detailed.   Thanks again!
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.