log4j - how to log to multiple files not based on level

Posted on 2006-04-14
Last Modified: 2008-01-09
I'd like to be able to log multiple types of information in my web application:

1) Performance timings to one log file
2) Typical diagnostic information to another file
3) "Hit" or "site usage" numbers to another file (e.g., how often a particular service is called and by whom)

I'm aware of being able to log to different files based on level, but that's not what I want to do. I want to be able to choose my destination file based on what I'm logging. So, within a single method I may want to log performance timings and diagnostic information - but to different files.


Question by:FeralCTO
    LVL 1

    Accepted Solution

    You need to use a combination appenders and filters. Basically you can do this in the Log4j configuration itself. you don't need to code them. Probably can use the String match filter (
    LVL 86

    Expert Comment

    You just need to set up different loggers for different classes, each with its own file appender
    LVL 1

    Author Comment

    What about categories? Would categories help me accomplish what I want to do?
    LVL 86

    Expert Comment

    Categories are history. The methodology i suggested is perfectly standard and requires no special treatment
    LVL 92

    Expert Comment

    Instead of having a seperate logger for each class as it typically done, you would have seperate logger based on function

    Logger timing= Logger.getLogger("PerformanceTiming");
    Logger diagnostics= Logger.getLogger("Diagnostics");

    With the different looger being pointed at different output files in your config (similiar to how you do it for level)


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    linearIn  challenge 23 54
    Basics of Drool Rule Engine with few samples 2 31
    noX challenge 17 54
    nestparen challenge 4 35
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    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…
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now