Log4j and multiple loggers/appenders

Hello,
I have a class that is instantiated multiple times. Each instance needs to write to a separate file which has to be rolled daily.
The example below instantiates SubClass twice; both log files are created successfully -2007-05-09_sc1.log and 2007-05-09_sc2.log-, but the output goes only to 2007-05-09_sc2.log with the following:
09 May 2007 15:00:05,953 INFO [sc2] - writing to class sc1
09 May 2007 15:00:05,953 INFO [sc2] - writing to class sc2
... while 2007-05-09_sc1.log is empty.
Could someone please point out what am I doing wrong?
Thanks in advance.
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.rolling.RollingFileAppender;
import org.apache.log4j.rolling.TimeBasedRollingPolicy;
public class LogTest
    public static void main(String[] args)
        SubClass sc1 = new SubClass("sc1");
        SubClass sc2 = new SubClass("sc2");
        sc1.writeToLog("writing to class sc1");
        sc2.writeToLog("writing to class sc2");
class SubClass
    private String name;
    private static Logger log;
    public SubClass( String name )
        this.name = name;
        // Create policy
        TimeBasedRollingPolicy policy = new TimeBasedRollingPolicy();
        policy.setFileNamePattern("logs/%d{yyyy-MM-dd}_" + name + ".log.zip");
        policy.activateOptions();
        // Create the appender
        RollingFileAppender appender = new RollingFileAppender();
        appender.setLayout(new PatternLayout("%d{DATE} %-5p [" + name+ "] - %m%n"));
        appender.setRollingPolicy(policy);
        appender.activateOptions();
        // Create logger
        log = Logger.getLogger("SubClass." + name);
        log.addAppender(appender);
    public void writeToLog( String txt )
        log.info(txt);
}

Here's my test version that works correctly:class SubClass {
  private Logger log;
  public SubClass(String name) {
    // Create the appender
    RollingFileAppender appender = new RollingFileAppender();
    appender.setLayout(new PatternLayout("%d{DATE} %-5p [" + name
                    + "] - %m%n"));
    appender.setFile("C:/temp/" + name + ".txt");
    appender.activateOptions();
    // Create logger
    log = Logger.getLogger("SubClass." + name);
    log.addAppender(appender);
  public void writeToLog(String txt) {
    log.info(txt);
}I don't have this "policy" business in my version of Log4J so I simplified your code a bit. My version produces two different log files with one line in each file.

Similar Messages

  • Log4J and Multiple Servers

    Hi, I'm just wondering is there a way that I could have multiple servers running and writing/appending to the same log file?? Java seems to lock for writing and no other servers can append.

    Sure. Centralize your logging by setting up another server whose only purpose is to write to that log file. Have all the other servers send their logs to that server. Log4J provides a SimpleSocketServer and a SocketAppender for exactly that purpose.

  • Log4j multiple loggers

    I was curious if anyone has been able to get multiple log4j loggers to work with weblogic? I have configured my domains
    to use log4j for logging and it appears to be functioning correctly. I am trying to use multiple loggers to send send system logs to the localhost and auditing log events to a remote host (using log4j syslogappender). A subset of my log4j.properties is shown below
    system.log.level=DEBUG
    log.dir=Logs
    max.filesize=10MB
    log4j.rootLogger=${system.log.level}, systemLogFile, stdout
    log4j.logger.audit=INFO, syslog
    log4j.additivity.audit=false
    log4j.appender.syslog=org.apache.log4j.net.SyslogAppender
    log4j.appender.syslog.SyslogHost=RemoteAuditRepositoryHost
    log4j.appender.syslog.layout=org.apache.log4j.EnhancedPatternLayout
    log4j.appender.syslog.layout.ConversionPattern=%d{${datestamp}} [%t] %-5p %c : %m%n
    log4j.appender.syslog.Facility=local2
    If I use this configuration the syslogd on my remote host receives nothing. I can get it too work if I move the syslog appender
    into the root logger but then it receives a lot of other logging Id rather not receive. I am using log4j 1.2.16 and Weblogic 10.3.

    Here's my test version that works correctly:class SubClass {
      private Logger log;
      public SubClass(String name) {
        // Create the appender
        RollingFileAppender appender = new RollingFileAppender();
        appender.setLayout(new PatternLayout("%d{DATE} %-5p [" + name
                        + "] - %m%n"));
        appender.setFile("C:/temp/" + name + ".txt");
        appender.activateOptions();
        // Create logger
        log = Logger.getLogger("SubClass." + name);
        log.addAppender(appender);
      public void writeToLog(String txt) {
        log.info(txt);
    }I don't have this "policy" business in my version of Log4J so I simplified your code a bit. My version produces two different log files with one line in each file.

  • Log4j and WAS logging

    Hello all,
    Is there anyway to redirect log4j logging to WAS loggers.
    For example, axis needs log4j and may need to log to some appenders.
    Can we safely use SAP logging and how?
    Is there an example somewhere?
    Antoine

    There's an article "Integrating 3rd party logging Frameworks into SAP NetWeaver" (https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/36085e78-0801-0010-678d-8b4e89ddff3c)
    It mentions sample code and a tutorial for a "Log4j bridge", but I couldn't locate that. I have posted a separate question for that.
    Hope this helps,
    Michael

  • Log4j and log4cxx together

    Hi,
    I'm using RollingFileAppender to append log4j and log4cxx logs in a common log file. This works only for the first log file. But, the subsequent log files which got created when the size exceeds the specified range, does not getting appended the log4cxx logs.
    I have referred http://www.dreamcubes.com/blog/?itemid=59#nucleus_cf
    Here is my config settings
    log4j:
    log4j.rootLogger=debug,console,file
    log4j.category.com.company.project=debug
    # File Appender
    # file is set to be a RollingFileAppender.
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=project.log
    log4j.appender.file.MaxFileSize=100KB
    # Keep one backup file
    log4j.appender.file.MaxBackupIndex=100
    # file uses PatternLayout.
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} - %m%nlog4cxx:
    <?xml version="1.0" encoding="UTF-8" ?>
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
         <!-- Output the log message to system console.
         -->
         <appender name="MyConsoleAppender" class="org.apache.log4j.ConsoleAppender">
              <param name="Target" value="System.out"/>
              <layout class="org.apache.log4j.PatternLayout">
                   <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
              </layout>
         </appender>
         <!-- On application startup, a log file named "project.log" will be create if not exist.
              When the log file reach beyond 100KB, it will be renamed "project.log.1", when the log
              index reach "project.log.5", the next rename will be back to "project.log.1" and
              overite the old log.
         -->     
         <appender name="MyRollingAppenderSize" class="org.apache.log4j.RollingFileAppender">
              <param name="file" value="project.log"/>
              <param name="append" value="true"/>
              <param name="MaxFileSize" value="100KB"/>
              <param name="MaxBackupIndex" value="100"/>
              <layout class="org.apache.log4j.PatternLayout">
                   <param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
              </layout>
         </appender>
         <!-- Setup the root category, add the appenders and set the default level
              5 level of logging,  ALL < DEBUG < INFO < WARN < ERROR < FATAL
              The root level is set with INFO, which mean any message greater or same
              as INFO will be log down, in this case, DEBUG is not logged.
              To log all regardless of logging level, set <priority value="ALL">
         -->
         <root>
              <priority value="all" />
              <appender-ref ref="MyRollingAppenderSize"/>
              <appender-ref ref="MyConsoleAppender"/>
         </root>
    </log4j:configuration>

    Logger experts,
    First of all, trying to point a common logging file from two different log configuration file is possible?
    If this is not at all possible, Keeping the two different log file would work, but it is cumbersome on debugging..
    Thanks and Regards,
    Ramesh

  • Log4j: Logging Errors(No appenders could be found for logger)

    hi,
    i used logging in my java application. i placed log4j.jar and log4j.xml file paths
    given in classpath.
    i am getting 2 warning Messages. Those are as follows
    Log4j:WARN. No appenders could be found for logger (Myclass.class)
    Log4j:WARN. Use Log4j System Properly.
    Here is what i done
    log4j.xml is external configuration file. I specified Appender and layout
    information in this.
    Here is log4j.xml content...
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="appender" class="org.apache.log4j.FileAppender">
    <param name="File" value="C:\LOG\log.txt"/>
    <param name="Append" value="false"/>
    <layout class="org.apache.log4j.SimpleLayout"/>
    </appender>
    <root>
    <priority value ="INFO"/>
    <appender-ref ref="appender"/>
    </root>
    </log4j:configuration>
    I specified appender and layout in log4j.xml file and placed log4j.xml file in
    classpath.
    How can i solve this problem?. Please help me.

    Ok. I'm no log4j expert, but if you try to get a logger called Myclass.class and have not defined one in your xml file, there's a problem? Look at my xml file below, especially the category tag. Instead of putting "All" for the name, you would put "Myclass.class".
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="file" class="org.apache.log4j.FileAppender">
    <param name="File" value="log/log.txt"/>
    <param name="Append" value="true"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%l) - %m\n"/>
    </layout>
    </appender>
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <param name="Target" value="System.Out"/>
    <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="%d %-5p %C{1}.%M - %m\n"/>
    </layout>
    </appender>
    <!-- The All category -->
    <category name="All" additivity="false">
    <priority value="debug"/>
    <appender-ref ref="file"/>
    <appender-ref ref="console"/>
    </category>
    <!-- The one and only root category -->
    <root>
    <priority value="warn"/>
    <appender-ref ref="console"/>
    </root>
    </log4j:configuration>

  • I have one apple ID and multiple family members share this with their devices.  How do we keep pics and messages separate?

    I have one apple ID and multiple family members share this with their individual devices.  How do we keep pics and messages separate for each device?

    For messages
    MacMost Now 653: Setting Up Multiple iOS Devices For Messages and FaceTime
    For other issue:
    How to use multiple iPods, iPads, or iPhones with one computer
    What is the best way to manage multiple...: Apple Support Communities

  • New 13" Macbook Pro trying to connect to a Panasonic VIERA TC-L42U30 as second monitor. I've used multiple hdmi-thunderbolt/mini displayport adapters and multiple hdmi cables and still no success. The Macbook does not sense the second monitor (TV). Help?!

    I've been a Mac since 2008, so I know my way around the system pretty well. This issue, however, has me stumped. I had an iMac until now and only now am I experiencing some difficulty with my new MacBook Pro. The model I have is the newest 13" Macbook Pro model and I'm trying to connect a Panasonic VIERA TC-L42U30 42" HDTV as a second monitor via the Thunderbolt port.
    It worked the first two times and hasn't worked since, after 10-15 attempts with different configurations, turning things on n off, restarting the mac, unplugging the cables, adapters, TV, resetting the P RAM, etc... I've used multiple hdmi-thunderbolt/mini displayport adapters and multiple hdmi cables and still no success. No matter what I do, the Macbook does not sense the TV as a second monitor anymore.
    I took the MacBook Pro to the Apple Store, and their "genius" there had it working fine with a DVI connection to a regular monitor. The Panasonic TV I have has HDMI connections and one VGA connection which does not support HD, but no DVI option. I want an HD connection to mirror or extend my MacBook Pro screen. At the Apple Store, they didn't have a Thunderbolt/Mini DisplayPort to HDMI adapter, so he could not try that out for me.
    Anyone else have this configuration or another similar one with a Panasonic HDTV?
    Ideas? Suggestions? Anything?! Help!!
    P.S. I'm running Mountain Lion, if that wasn't already obvious. Everything is up to date in my App Store as well.
    Thanks!

    Hi There,
    I have had the exact same issue but with a projector.
    The issue lies with Mountian Lion 10.8.2.
    I tried many a combination with no luck to get HDMI working.
    Took my mac into the apple store and came to the conclusion it was the software, so I asked them to install 10.8 onto it (this is destructive so a backup is a must)
    Bought my macbook home and voila, now displaying through my projector.
    There is a small graphics update after 10.8.1 which seems to be the cause.
    Hope this helps.
    Thanks.

  • Hello,  I have iMac from Oct, 2010 and have OS 10.6.7 and multiple users.  Mail icon was accidently removed along with program.  Mail 4.4 is on other users but when I reloaded software my user got Mail 4.3. Mail 4.3 did not work with latest OS. Ideas?Matt

    Hello,  I have iMac Itel i3, from Oct, 2010 and have OS 10.6.7 and multiple users. 
    About 3-4 days ago the Mail icon in dock had question mark and was accidently removed, along with program on my user.  Oops.
    1. Mail 4.4 is on other users and works fine, but for me it does not work and tells me V4.3 does not work with latest OS X. 
    2. I reloaded software from my user with original disks  (Oct, 2010) and got Mail 4.3 again. Mail 4.3 did not work with latest OS.  I am unable to load Mail.  Gives Yellow error message.    
    "  You have Mail version 4.3 (1081/1084). It can’t be used on Mac OS X Version 10.6.7 (Build 10J869). For more information, click the Help button."
    3. Tried to update software and look for update for mail 4.3 on Apple site to no success.
    Hate to go to MS-Entorage ( I may need to learn something new and alien) mail program since contracts are in Mail/Address Book.
    Ideas?
    MattDeeds

    Matt,
    I would download and run the 10.6.7 combo update.
    http://support.apple.com/kb/DL1361
    Regards,
    Captfred

  • Can I use one itunes account for multiple users and multiple devices

    Can I use one itunes account for multiple users and multiple devices?

    The iTunes EULA, as I understand it (I'm not an attorney )for Apps is basically that one person can use an App multiple devices, or multiple people can use an App one device..so one to many or many to one, but not many to many....

  • How to bundle a .FLA and multiple .SWF files in one .EXE?

    Hi,
    I have a .FLA file which uses loadMovieNum() to load various
    .SWF files when each one is called by the user clicking on a
    button. Each .SWF file also uses loadMovieNum() to call a second
    .SWF file. When I publish the .FLA file as a .SWF file it all works
    beautifully.
    Now I need to package the entire project into a Windows
    projector .EXE file for offline use on a computer without the Flash
    Player. When I publish the main .FLA file as a .EXE file, the .SWF
    files still load, although they blink before fully loading and
    displaying steadily.
    What I would really like to do is to bundle all the .SWF
    files into the .EXE file so when I send the finished project to the
    end user, they only have to deal with one file, rather than a
    folder with the .EXE file and multiple .SWF files.
    Is there a way to do this?
    I am using Flash 8 Professional.
    Thanks,
    Cam

    Since this isn't going to be web-based, why not include all
    the external swf files into the same Flash file and then use
    attachMovie?
    This will allow you to create one .exe file that would
    contain EVERYTHING. Since you aren't worried about loading speeds,
    you don't have to load the movie clips, but can simply create one
    large Flash file.

  • One premium account and multiple Skype HD devices

    Will one Premium account and multiple non premium HD accounts work for video calling? Basically the idea is 10 HD skype devices, the set top boxes connected to tvs will dial into one Premium Mac or PC account for multiple video conferencing.

    Yes, one user needs to have a Group Video Calling subscription.
    Unfortunately, not all devices support Group Video calling (TV's for example, do not).
    Please see our website for more information: http://www.skype.com/en/features/group-video-chat/.

  • HT4137 iCloud setup with one Apple ID and multiple devices

    iCloud setup with one Apple ID and multiple devices

    What, exactly, are you trying to do? You can use the same iCloud account on as many devices as you want, as long as you don't turn on iTunes Match. Once you turn on Match, you're limited to 10 devices total.
    Is this what you want to do...same iCloud account, but different iTunes/App store accounts on each device? If so, you can do that also.

  • I have two macs at my house, and multiple iPhones. I want to keep the same apple id as everyone else, but i want to put my own music only on my iPhone. I want to do this with my laptop which is not the family's main iTune's computer.

    I have two macs at my house, and multiple iPhones. I want to keep the same apple id as everyone else, but i want to put my own music only on my iPhone. I want to do this with my laptop which is not the family's main iTune's computer.

    If you use the same Apple ID for iCloud on each device, yes. However, you can use the same Apple ID for iTunes content on each device, but different Apple ID's for iCloud, iMessage, FaceTime, etc., on each device. That way, you can have whatever iTunes content you want on each phone, but keep all of the other data separate. You can create another Apple ID here:
    https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId?loca lang=en_US
    Must be a verified email address.

  • Totally confused with iCloud and multiple devices

    Ok, So we have two each of the iPods, iPads and now iPhones.  Do we all share the same iCloud? Can we share apps through the cloud? How do I switch my iPad to the new one I got to give this one to my son. I am so new to all this apple stuff. Only been a few months and I'm still really confused.  What all goes to the iCloud? How do we see what's in the iCloud?  I really the the idiots instructions I think to figure this all out?  Can anyone help me?  How can I print from the iPad?  Everything I see says iOS 10 something?  I believe I only have 6 something? 

    Do we all share the same iCloud?
    icloud is designed so that only one user uses it to keep his/her devices in sync.  When multiple users use the same icloud account, they will then be sharing the same email address, contacts, calendars, notes, etc.  Usually that is not what two people want to do.
    Can we share apps through the cloud?
    You buy apps from the itunes store, that's different than icloud, and multiple users can use the same itunes account so that they can share apps, music, etc.  For a family, everyone usually uses the same Apple ID for an itunes account and individual Apple IDs for their icloud accounts.
    How do I switch my iPad to the new one I got to give this one to my son.
    What do you mean by "switch.. ipad to the new one"?
    What all goes to the iCloud?
    Backups of iOS devices, Apple services that you turn on to share data (settings>icloud; on an iOS device), third party apps that use icloud to sync their data between devices, email, photo stream, and more.
    How do we see what's in the iCloud? 
    Icloud is designed primarily for syncing data between the devices of the user - like getting all computers and devices to have the same contacts or calendars.  When you set up syncing (settings>icloud, turn on the services you want synced between your devices), some of that data will also be available to a computer's browser when you log into icloud.com.  Otherwise you view the data using the associated app, like the Calendar app to see events.  Other things that may be on icloud, like backups cannot be seen.  Also iCloud does not provide a photo gallary that others can view in a browser.
    How can I print from the iPad? 
    For that you need an "airprint" compatible printer. See,
    http://support.apple.com/kb/ht4356
    You can also print to a printer that's connected to a computer, but that computer needs special software, like Printopia for macs.
    Everything I see says iOS 10 something?  I believe I only have 6 something?
    There is no "10", version 6 is the current one.  You are probably thinking of OSX, whose versions are 10.x.y.  That's for macs, not iOS devices.

Maybe you are looking for