Reassign stderr/stdout to a Log4J appender.

Does anyone know if there is a 'quick-and-dirty' way to reassign Java stderr and stdout to a Log4J Appender? Both Google and the log4j mailing lists are pretty sparse on this subject...

This is the dirty way of doing it. If anyone knows
the better way please mention it here.This is just putting a band-aid on a wound which will not be cured by a band-aid. It only masks the problem, and may introduce further problems (such as: What if you actually don't want to "log" certain things that were only intended for System.out?)
The better way would be to re-design the code so that it logs messages to loggers rather than writing to System.out.
I suspect the OP and you both know that, but just pointing it out in case.

Similar Messages

  • Log4j appender question

    I need some log4j help. Hope anyone can help me.
    Question 1:
    I need to log information to 4 different files. As far as I can tell from the log4j docs, I will then need to create 4 different appenders in the log4j.properties file. How can I do that?
    Looking at examples, it looks like I will need to add 4 new sections similar to this one. Is that correct?
    # Logfile 1
    log4j.appender.log1=org.apache.log4j.RollingFileAppender
    log4j.appender.log1.File=C:/Sun/logs/logme1.log
    Question 2:
    After that is done, how can I actually write to the files? When logging I was thinking just using: logger.info("log something").
    How can I specify which file that will log to? Is that even the correct way to log?
    Hope someone can help!

    After that is done, how can I actually write to the files? When logging I
    was thinking just using: logger.info("log something").
    How can I specify which file that will log to? Is that even the correct
    way to log?apperantly, it doesn't seem like you have understand how log4j works. that's probably why warnerja tell you to perform some search.
    understand the technology first..then ask specific question here.
    it will be useless just handling out answers to everyone. i think they're here just not to provides answers, but to educate other along the way...but they need to see an effort was made first.
    you're right..there are time when one doesn't have enough time to understand the whole technology..or the technology is too difficult to comprehend..or the documentation is poor., but i think there are plenty of good documentation on log4j. Futhermore..what you are asking is not usually shown in the documentation, but a google search would probably provides you with the answer you seek..you just need to find a good keyword.
    Futhermore, I suggest you read some tutorial on log4j if you plan to use the technology..it will not takes all day..maybe an hour or so..just to get how the technology works
    anyway,
    your appender tell the logger where the log the message to (depends on the level you specified) .. so if you have 3 appender that specified
    the level to be INFO, and you have logger.info("This is an INFO message"), then the message will be log to all three files.
    log4j.threshold=ALL
    log4j.rootLogger=ALL, stdout, DebugAppender, InfoAppender,
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%n
    log4j.logger.DebugAppender.access=DEBUG
    log4j.appender.DebugAppender=org.apache.log4j.RollingFileAppender
    log4j.appender.DebugAppender.File=.${file.separator}log${file.separator}ex_debug.log
    log4j.appender.DebugAppender.MaxFileSize=10KB
    log4j.appender.DebugAppender.MaxBackupIndex=1
    log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.DebugAppender.layout.ConversionPattern=%c %d{ISO8601} -- %p --
    %m%n
    log4j.logger.InfoAppender.access=INFO
    log4j.appender.InfoAppender=org.apache.log4j.RollingFileAppender
    log4j.appender.InfoAppender.File=.${file.separator}log${file.separator}ex_info.log
    log4j.appender.InfoAppender.MaxFileSize=10MB
    log4j.appender.InfoAppender.MaxBackupIndex=2
    log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.InfoAppender.layout.ConversionPattern=%c %d{ISO8601} -- %p -- %m%nthe
    log4j.rootLogger=
    tell what appender to use...so include all your appender in this property.
    you appender specified the logging level INFO, WARN, DEBUG,
    ERROR, or FATAL. I'm not sure how to specified the min and max level
    for an appender using a property file..for xml configuartion file..look at
    the example provide in previous reply.
    now if you do :
    logger.info("This is an info message") and want it to log to file1 and file2
    and log.info("This is another info message") and you want this to log to file3
    you can't do that...a way around this is to create your own level..and the
    appender will specifies that level.
    if you plan to use an XML configuration file, then make sure you include the <root> node
    <root>
       <priority value="debug"/>
       <appender-ref ref="DEBUG"/> 
       <appender-ref ref="INFO"/>
       <appender-ref ref="MY_APPENDER_NAME"/>
    </root>
    </log4j:Configuration>

  • Log4j, appender and InstantiationException

    Hi,
    I have written my own appender in an Eclipse project that uses log4j. When I try to use the appender I get the following exception:
    log4j:ERROR Could not instantiate class [test.appender.NewFileAppender].
    java.lang.InstantiationException: test.appender.NewFileAppender
         at java.lang.Class.newInstance0(Unknown Source)
         at java.lang.Class.newInstance(Unknown Source)
         at org.apache.log4j.helpers.OptionConverter.instantiateByClassName(OptionConverter.java:329)
         at org.apache.log4j.helpers.OptionConverter.instantiateByKey(OptionConverter.java:120)
         at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:629)
         at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:612)
         at org.apache.log4j.PropertyConfigurator.parseCatsAndRenderers(PropertyConfigurator.java:533)
         at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:417)
         at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:316)
         at org.apache.log4j.PropertyWatchdog.doOnChange(PropertyConfigurator.java:682)
         at org.apache.log4j.helpers.FileWatchdog.checkAndConfigure(FileWatchdog.java:88)
         at org.apache.log4j.helpers.FileWatchdog.<init>(FileWatchdog.java:57)
         at org.apache.log4j.PropertyWatchdog.<init>(PropertyConfigurator.java:674)
         at org.apache.log4j.PropertyConfigurator.configureAndWatch(PropertyConfigurator.java:382)
         at se.micronic.argos.main.Main.initLogging(Main.java:305)
         at se.micronic.argos.main.Main.<init>(Main.java:272)
         at se.micronic.argos.main.Main.main(Main.java:1339)
    log4j:ERROR Could not instantiate appender named "argos".My property file looks like this:
    log4j.appender.argos=test.appender.NewFileAppender
    log4j.appender.argos.file=logs/MArgos.logThe other appenders in the property file work fine.
    Any ideas?
    Thanks
    Markus

    could be you don't have a no-args constructor? show us the NewFileAppender class
    is that the entire stack trace? there's no other underlying exception being thrown?

  • LOG4J Appender  to Webservices

    Hello,
    I have a question.
    Is there an LOG4J Appender to Webservices?
    I founded a lot of kinds of appenders on www.apache.org, but nothing about webservices.
    Thanks for any help!!

    Thanks. I'm using a startup class. I could easily check if log4j is in the server classpath, and attach my appender. However, if log4j is not in server classpath, and in the application classpath, I need to check for this. I was using ApplicationContainerFactory in weblogic 8 like so:
    appContainerFactory.getApplicationContainer(applicationName).getApplicationClassLoader()
    which is not available in weblogic 9. And I can't find in the docs anywhere to indicate it's replacement, or any type of alternative.

  • Piping stderr & stdout to /dev/null

    Hi,
    I've never worked with stderr and stdout before. I have an application that sends an obscene number of messages to the Console log, so implemented the following shell script to launch it in future:
    /Volumes/Storage/Shared/Games/UrbanTerror/ioUrbanTerror.app/Contents/MacOS/ioUrb anTerror.ub >> 2&>1 /dev/null
    No further entries appeared in the Console so I thought I was sorted, but then I discovered two files in the root of my Home directory called 1 and 2 respectively, and these contained the log outputs. I thought /dev/null was supposed to be the computer equivalent of "oblivion," so have I implemented the shell script incorrectly?
    Also, can someone further modify my script so that stdout goes to /dev/null and stderr goes to a log file in a location of my choosing?
    Many thanks,
    S.

    No further entries appeared in the Console so I thought I was sorted, but then I discovered two files in the root of my Home directory called 1 and 2 respectively, and these contained the log outputs. I thought /dev/null was supposed to be the computer equivalent of "oblivion," so have I implemented the shell script incorrectly?
    The >> should have been next to /dev/null, the & was in the wrong place, and the 2>&1 should follow the >>/dev/null. Understanding the 2>&1 behavior is a bit tricky.
    /Volumes/Storage/Shared/Games/UrbanTerror/ioUrbanTerror.app/Contents/MacOS/ioUrbanTerror.ub >/dev/null 2>&1
    You could have also just redirected stdout and stderr separately to /dev/null
    /Volumes/Storage/Shared/Games/UrbanTerror/ioUrbanTerror.app/Contents/MacOS/ioUrbanTerror.ub >/dev/null 2>/dev/null
    Also, can someone further modify my script so that stdout goes to /dev/null and stderr goes to a log file in a location of my choosing?
    /Volumes/Storage/Shared/Games/UrbanTerror/ioUrbanTerror.app/Contents/MacOS/ioUrbanTerror.ub >/dev/null 2>/location/of/your/choosing/file.log
    This will overwrite file.log every time you invoke the script. Or if you want to append to the file you can use:
    /Volumes/Storage/Shared/Games/UrbanTerror/ioUrbanTerror.app/Contents/MacOS/ioUrbanTerror.ub >/dev/null 2>>/location/of/your/choosing/file.log
    Or if you want to use dated log files for each running of the script:
    /Volumes/Storage/Shared/Games/UrbanTerror/ioUrbanTerror.app/Contents/MacOS/ioUrbanTerror.ub >/dev/null 2>/location/of/your/choosing/file.$(date +%Y%m%d%H%M).log
    Message was edited by: BobHarris

  • Regsitering log4j appender to a logger in a class in a web app

    During weblogic 9.2 server startup I would like to register a Handler with any number of log4j Loggers in all deployed web apps. I was thinking I could somehow get a reference to the applications classloader, and see if it has log4j, then register with the root logger. I used to do this in WLS 8, using ApplicationContainerFactory, but this is no longer available in WLS 9. Is there another way to do this in WLS 9? Thank for any help.

    Thanks. I'm using a startup class. I could easily check if log4j is in the server classpath, and attach my appender. However, if log4j is not in server classpath, and in the application classpath, I need to check for this. I was using ApplicationContainerFactory in weblogic 8 like so:
    appContainerFactory.getApplicationContainer(applicationName).getApplicationClassLoader()
    which is not available in weblogic 9. And I can't find in the docs anywhere to indicate it's replacement, or any type of alternative.

  • JMX-Log4j Appender problem

    Dear listers
    we wrote thefollowing code to log the loglevel's dynamically.
    Following is the code
    public class MyApp implements MyAppMBean {     
    private static Logger logger = Logger.getLogger(MyApp.class);
    public void go() throws Exception{
    while(true){
    logger.debug("DEBUG") ;
    logger.info("INFO") ;
    logger.warn("WARN");
    logger.error("ERROR");
    logger.fatal("FATAL");
    Thread.sleep(2000);
    public void setLoggingLevel(String level){
    logger.info("Setting logging level to: " + level);
    Level newLevel = Level.toLevel(level, Level.INFO);
    Logger.getRootLogger().setLevel(newLevel);
    public String getLoggingLevel(){                       
    return Logger.getRootLogger().getLevel().toString() ;
    public static void main(String[] args) throws Exception
    DOMConfigurator.configure("log4j.xml");
    MyApp app = new MyApp();
    //Lookup for the mbean server
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    //Create and register the top level log4j MBean
    HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
    ObjectName mbo = new ObjectName("log4j:logger=root");
    System.out.println("Debug10");
    server.registerMBean(hdm, mbo);
    LoggerRepository r = LogManager.getLoggerRepository();
    Enumeration<Logger> elogs = r.getCurrentLoggers();
    Logger logger = null;
    while(elogs.hasMoreElements())
    //need to show the elements of teamcenter only.
    logger = (Logger)elogs.nextElement();
    hdm.addLoggerMBean(logger.getName());
    } System.out.println(Logger.getRootLogger().getLevel().toString());
    int portNumber=9999;
    ObjectName htmlName = new ObjectName("log4j:logger=root,port=" + portNumber) ;
    HtmlAdaptorServer html = new HtmlAdaptorServer(portNumber);
    html.setPort(portNumber);
    server.registerMBean(html, htmlName);
    html.start();
    app.go();
    When setting the attributes for the appenders under log4j from htmladapter it gives the following message.
    *477 All Attributes Not Set*
    *1/8 attribute(s) were not set:*
    appender%3DTaskAppender%2Clayout%3Dorg.apache.log4j.PatternLayout
    Please advice how can we solve this issue ~Vilas

    Dear listers
    we wrote thefollowing code to log the loglevel's dynamically.
    Following is the code
    public class MyApp implements MyAppMBean {     
    private static Logger logger = Logger.getLogger(MyApp.class);
    public void go() throws Exception{
    while(true){
    logger.debug("DEBUG") ;
    logger.info("INFO") ;
    logger.warn("WARN");
    logger.error("ERROR");
    logger.fatal("FATAL");
    Thread.sleep(2000);
    public void setLoggingLevel(String level){
    logger.info("Setting logging level to: " + level);
    Level newLevel = Level.toLevel(level, Level.INFO);
    Logger.getRootLogger().setLevel(newLevel);
    public String getLoggingLevel(){                       
    return Logger.getRootLogger().getLevel().toString() ;
    public static void main(String[] args) throws Exception
    DOMConfigurator.configure("log4j.xml");
    MyApp app = new MyApp();
    //Lookup for the mbean server
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    //Create and register the top level log4j MBean
    HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
    ObjectName mbo = new ObjectName("log4j:logger=root");
    System.out.println("Debug10");
    server.registerMBean(hdm, mbo);
    LoggerRepository r = LogManager.getLoggerRepository();
    Enumeration<Logger> elogs = r.getCurrentLoggers();
    Logger logger = null;
    while(elogs.hasMoreElements())
    //need to show the elements of teamcenter only.
    logger = (Logger)elogs.nextElement();
    hdm.addLoggerMBean(logger.getName());
    } System.out.println(Logger.getRootLogger().getLevel().toString());
    int portNumber=9999;
    ObjectName htmlName = new ObjectName("log4j:logger=root,port=" + portNumber) ;
    HtmlAdaptorServer html = new HtmlAdaptorServer(portNumber);
    html.setPort(portNumber);
    server.registerMBean(html, htmlName);
    html.start();
    app.go();
    When setting the attributes for the appenders under log4j from htmladapter it gives the following message.
    *477 All Attributes Not Set*
    *1/8 attribute(s) were not set:*
    appender%3DTaskAppender%2Clayout%3Dorg.apache.log4j.PatternLayout
    Please advice how can we solve this issue ~Vilas

  • Log4j appender

    hi,
    i have requirement wherein i must have logs generated for each 100 Mb but the rollover must not happen based on teh index. it must happen every 6 hours.
    so the logs must be generated as ,
    logging.log
    logging.log.1
    logging.log.2
    logging.log.3
    logging.log.15
    and after 6 hours, the rollover must start again.
    i am new to log4j, i want somebody to give some input or any pointerw where i can learn to do this sooner will be appreciated..
    thanks,
    aravind

    I seem to remember that someone contributed a TimeAndSizeRollingAppender to the Apache project. hava a google for it. ;-)

  • Log4j appender pipelining

    Hi there,
    Can I pipeline log4j appenders? Essentially, I have a custom AsyncAppender which I want feeding into DailyRollingFileAppender for file rollover purposes.
    How can I do that?
    Kind Regards,
    Zalle

    Hi there,
    Can I pipeline log4j appenders? Essentially, I have a custom AsyncAppender which I want feeding into DailyRollingFileAppender for file rollover purposes.
    How can I do that?
    Kind Regards,
    Zalle

  • Log4j - logging to multiple files

    Hi,
    Is there a way to do this? I have a class in a shared package in which I want to log into a separate log file.
    I'm using the text configuration file - not the xml one.
    I tried this (a suggestion from google):
    log4j.rootLogger = ERROR, WebLog
    log4j.category.se.aftonbladet.elitserien=ERROR
    log4j.category.org.apache.struts2=ERROR
    log4j.category.org.apache=ERROR
    log4j.category.catalia=ERROR
    # An extra category to a log file
    log4j.category.AlternativeLog=A3
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Threshold = DEBUG
    log4j.appender.stdout.Target   = System.out
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n
    log4j.appender.WebLog = org.apache.log4j.RollingFileAppender
    log4j.appender.WebLog.file = /opt/tomcat/logs/elitserienservice.log
    log4j.appender.WebLog.MaxFileSize=2048KB
    log4j.appender.WebLog.MaxBackupIndex = 5
    log4j.appender.WebLog.layout = org.apache.log4j.PatternLayout
    log4j.appender.WebLog.layout.ConversionPattern = %d{ISO8601} %-5p [%F:%L] : %m%n
    # A3 is set to be a FileAppender which will log all actions in the application.
    log4j.appender.A3=org.apache.log4j.RollingFileAppender
    log4j.appender.A3.file=/opt/tomcat/logs/elitserienservice_alt.log
    log4j.appender.A3.MaxFileSize=2048KB
    log4j.appender.A3.MaxBackupIndex = 5
    log4j.appender.A3.layout=org.apache.log4j.PatternLayout
    log4j.appender.A3.layout.ConversionPattern=%d{ISO8601} %-5p [%F:%L] : %m%nAnd in my class I get my logger like this:
    private static final Logger logger = Logger.getLogger("AlternativeLog");But it doesnt work. All logging still ends up in the 'elitserienservice.log'.
    Any help much appreciated!
    /best regards, Håkan Jacobsson

    log4j loggers/categories are additive by default. If you write a message to the "AlternativeLog" logger, log4j will first pass that message to all appenders that are attached to the "AlternativeLog" appender. After that, log4j will pass that same message to all appenders that are attached to any 'parent' loggers/categories. In your example, the only 'parent' logger above the "AlternativeLog" is the root logger.
    To alter this behavior, you'll need to set the additivity property to false on the "AlternativeLog" logger. Something like this should do the trick:
    log4j.additivity.AlternativeLog=false
    FYI... For better responses to log4j inquiries, you should post your questions to the log4j users mailing list ([email protected])
    http://logging.apache.org/mail-lists.html

  • Log4j:ERROR setFile(null,true) call failed.

    Hello
    I have Log4j.properties
    log4j.rootCategory = DEBUG, file ,ImportFileAppender , console
    log4j.appender.stdout.threshold = ERROR
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p [%t] %c - %m%n
    log4j.appender.file.threshold = DEBUG
    log4j.appender.file.File = ${qtl.log.location}
    log4j.appender.file.MaxFileSize = 10MB
    log4j.appender.file.MaxBackupIndex = 10
    log4j.appender.file = org.apache.log4j.RollingFileAppender
    log4j.appender.file.layout = org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern =%-5p|%-80m|%d%n
    log4j.appender.ImportFileAppender.threshold = DEBUG
    log4j.appender.ImportFileAppender.File = ${qtl.log.import}
    log4j.appender.ImportFileAppender = org.apache.log4j.RollingFileAppender
    log4j.appender.ImportFileAppender.layout = org.apache.log4j.PatternLayout
    log4j.appender.ImportFileAppender.layout.ConversionPattern = %-5p|%-80m|%d%n
    and i am giving ${qtl.log.location} through java file like this
    private static String LOG_4J_PROPERTY_FILE = "/src/com/impact/qtl12/commons/log4j.properties";
    logFileName     = "log-" + new SimpleDateFormat("yyyyMMdd-hhmm").format(new Date()) + ".log";
              logFilePath = dirString + "\\" + logFileName;;
    System.setProperty("qtl.log.location", logFilePath); //replacing ${qtl.log.location} by the logFilePath in log4j.properties String directory = System.getProperty("user.dir");
    PropertyConfigurator.configure(directory + LOG_4J_PROPERTY_FILE);
    when i run my program i get error of this type
    log4j:ERROR setFile(null,true) call failed.
    java.io.FileNotFoundException:
         at java.io.FileOutputStream.openAppend(Native Method)
         at java.io.FileOutputStream.<init>(Unknown Source)
         at java.io.FileOutputStream.<init>(Unknown Source)
         at org.apache.log4j.FileAppender.setFile(FileAppender.java:289)
         at org.apache.log4j.RollingFileAppender.setFile(RollingFileAppender.java:165)
         at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:163)
         at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:256)
         at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:132)
         at org.apache.log4j.config.PropertySetter.setProperties(PropertySetter.java:96)
         at org.apache.log4j.PropertyConfigurator.parseAppender(PropertyConfigurator.java:654)
         at org.apache.log4j.PropertyConfigurator.parseCategory(PropertyConfigurator.java:612)
         at org.apache.log4j.PropertyConfigurator.configureRootCategory(PropertyConfigurator.java:509)
         at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:415)
         at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurator.java:316)
         at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.java:324)
         at com.impact.qtl12.imports.UI.CSVFile.QTL.main(QTL.java:964)
    log4j:ERROR setFile(null,true) call failed.
    Edited by: Mayur Mitkari on Jan 2, 2013 12:53 AM
    Edited by: Mayur Mitkari on Jan 2, 2013 12:54 AM
    Edited by: Mayur Mitkari on Jan 2, 2013 12:55 AM

    Mayur Mitkari wrote:
    I checked it out the directory existsReally? You have a directory 'src' in the root of your filesystem?
    /src/com/...EDIT: never mind, moving along, I missed a bit of code there... In my own defense, use \ formatting tags to make it slightly more inviting to read it.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Getting log4j's SocketNode and SocketAppender to work

    For the past week I've been struggeling hard to get remote logging working using the log4j SocketNode and SocketAppender, but failed to succeed. Unfortunately there is not so much information on the log4j.net package to be found on the internet, lets forget about a working example.
    This is what I've been doing so far:
    SimpleSocketServer
    On the server I'm running a SimpleSocketServer instance on port 4445 through the command:
    java -classpath D:\RemoteLog4JServer\lib\log4j-1.2.12.jar org.apache.log4j.net.SimpleSocketServer 4445 simplesocketserver.properties
    The simplesocketserver.properties file looks like:
    log4j.rootLogger=debug,stdout,R
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    # Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=remote_log.log
    log4j.appender.R.MaxFileSize=1024KB
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%nOn the system console I see this server is running fine.
    Next step is to start a SocketNode-instance through a Java application.
    The main code of this class looks like:
    //-- Start a new SocketNode
    try {
    System.out.println("Accessing socket on host " + m_hostName + " through port " + m_port);
    Socket socket = new Socket(m_hostName, new Integer(m_port).intValue());
    //-- All right so far, now create the SocketNode
    System.out.println("Create the SocketNode listener");
    new SocketNode(socket, new Hierarchy(Logger.getRootLogger()));
    catch ..... Host and port are passed through application parameters and are 127.0.0.1 and 4445
    Again on the server console it confirms the listener is bound to the socket on port 4445 ...
    The last part of the puzzle is a test client application which sends remote logging events to the server.
    The body of this class looks like:
    static Logger logger = Logger.getRootLogger();
    public static void main(String argv[]) {
    // Try sending logging events through the SocketAppender in the log4j_remote.properties file
    System.out.println("Try sending logs through the SocketAppender in the log4j.properties file");
    PropertyConfigurator.configure("log4j_remote.properties"); //Logs to a SocketAppender; host 127.0.0.1, port 4445
    logger.debug("Hello world");
    logger.info("What a beatiful day.");
    // And try sending logging events through SocketAppender instance
    System.out.println("Try sending logs through a SocketAppender instance");
    SocketAppender sa = new SocketAppender("171.21.241.122", 4445);
    System.out.println("Created SocketAppender instance");
    String logMsg = "Writing log event through SocketAppender instance";
    LoggingEvent le = new LoggingEvent("fox.teststuff.TestLog4J",Logger.getRootLogger(),Level.DEBUG,logMsg, new Throwable());
    sa.append(le);
    sa.close();
    }As you can see I tried remote logging on two ways:
    1.
    Through the log4j_remote.properties file by configuring a SocketAppender. This file looks like:
    log4j.rootCategory=DEBUG,stdout,A1
    # ConsoleAppender
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    # Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    # A1 is set to be a SocketAppender sending its output to the server running on the remote host, port 12345.
    log4j.appender.A1=org.apache.log4j.net.SocketAppender
    log4j.appender.A1.Port=4445
    log4j.appender.A1.RemoteHost=[IP_ADDRESS_SERVER]2.
    By using a SocketAppender instance.
    But unfortunately neither work. The log remote_log.log gets created as the SimpleSocketServer is started. It contains entries of the successfull starting of the server and the SocketNode.
    I also see the logs coming in the client console on running the test applicattion.
    There is no firewall between client and server, and I am able to setup a tellnet session from client to server and vice versa through port 4445 (after launching the SimpleSocketServer).
    Furthermore I used some IP-sniffing tools to check the IP-traffic from client to server, and this shows me the TCP/IP packet is successfully sent on the client, and successfully received on the server.
    But no client-entries appear in the server log :-(
    I would be very gratefull if someone has got a clue. And you can be sure I will pass this knowledge on to the community.
    Thanks in advance, kind greetings,
    Harro de Vos

    UPDATE:
    Meanwhile I discovered 2 problems, with many thanks to my collegue Pattrick.
    1.
    The SimpleSocketServer is a server/listener which makes use of the SocketNode-class. Therefore it was needless and wrong for me to launch a SocketNode-instance in my StartSocketNode-class. This probably only caused conflicts.
    2.
    In my testclass I tried to create remote logging events on 2 ways, though the SocketAppender in the log4j.properties file and through the use of a programmed instance of the SocketAppender-class.
    It could very well be running these two methods in the same thread also causes conflicts, so I tried them seperately.
    I didn't get the first one to work (through the properties file), but the second solution (SocketAppender instance) did the trick for me!!
    Only remaining issue is this solution causes an EOFException.
    Anybody any ideas about this?

  • Log4j : logging logs to different appenders

    Hello,
    i have this as log4j.properties
    log4j.rootCategory = DEBUG,file,Logfile,stdout
    log4j.category.MUTE = OFF
    log4j.appender.stdout.threshold = ERROR
    log4j.appender.stdout = org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern = %d{ABSOLUTE} %5p [%t] %c - %m%n
    log4j.appender.file.threshold = ${qtl.log.level}
    log4j.appender.file.File = ${qtl.log.operation}
    log4j.appender.file.MaxFileSize = 10MB
    log4j.appender.file.MaxBackupIndex = 10
    log4j.appender.file = org.apache.log4j.RollingFileAppender
    log4j.appender.file.layout = org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern =%-5p|%-80m|%d%n
    log4j.appender.Logfile.File = ${qtl.log.application}
    log4j.appender.Logfile.MaxFileSize = 10MB
    log4j.appender.Logfile.MaxBackupIndex = 10
    log4j.appender.Logfile = org.apache.log4j.RollingFileAppender
    log4j.appender.Logfile.layout = org.apache.log4j.PatternLayout
    log4j.appender.Logfile.layout.ConversionPattern =%-5p|%-80m|%d%n
    for these two different file appenders , i want to instantiate two different loggers so how can i do it
    at present moment i am instantiating log like this
    String callingClassName = Thread.currentThread().getStackTrace()[2].getClass().getCanonicalName();
    return Logger.getLogger(callingClassName);

    log4j.logger.myFile1=debug,file
    log4j.logger.myFile2=debug,Logfilein the Java Code you have to select one or the other logger:Logger fl1= Logger.getLogger("myFile1");// loggs to {qtl.log.operation}
    Logger fl2= Logger.getLogger("myFile2"); // loggs to ${qtl.log.application}bye
    TPD

  • Log4J - file appender (2/more file for same package/class)

    Hi All,
    My requirement is i want to create 2 log file for same package.
    EG: com.foo.bar
    my code:
    log4j.rootCategory= ALL, fileAppender,stdout
    log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
    log4j.appender.fileAppender.File=c:\\temp\\test.log
    log4j.appender.fileAppender.MaxFileSize=64KB
    log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.file1=org.apache.log4j.RollingFileAppender
    log4j.appender.file1.File= file1.log
    log4j.appender.file1.MaxFileSize=64KB
    log4j.appender.file1.MaxBackupIndex=1
    log4j.appender.file1.layout=org.apache.log4j.PatternLayout
    log4j.appender.file1.layout.ConversionPattern=%p %t %c - %m%n
    log4j.appender.file2=org.apache.log4j.RollingFileAppender
    log4j.appender.file2.File= file2.log
    log4j.appender.file2.MaxFileSize=64KB
    log4j.appender.file2.MaxBackupIndex=1
    log4j.appender.file2.layout=org.apache.log4j.PatternLayout
    log4j.appender.file2.layout.ConversionPattern=%p %t %c - %m%n
    log4j.appender.file3=org.apache.log4j.RollingFileAppender
    log4j.appender.file3.File= file3.log
    log4j.appender.file3.MaxFileSize=64KB
    log4j.appender.file3.MaxBackupIndex=1
    log4j.appender.file3.layout=org.apache.log4j.PatternLayout
    log4j.appender.file3.layout.ConversionPattern=%p %t %c - %m%n
    // THIS IS FOR ClIENT
    log4j.logger.com.ls.es.service=DEBUG, file1
    // THIS IS FOR SUPPORT
    log4j.logger.com.ls.es.service=DEBUG, file2
    // THIS IS ERROR MESSAGE
    log4j.logger.com.ls.es.service=ERROR, file3
    Here file1 is for Client(Client Admin)and file2 is for Support.
    but am getting only file3.log file which is for Error Message..
    some where configuration is missing...
    Thanks,
    by
    dhana

    I have created a class that allow to create easily new logs:
    1.- Switch the Root Logger output
    2.- Create as many logs as desired (I build a simple web server, each new client has a log associated to it).
    Main problem: code and comments in spanish.
    Are you intereseted on it? Let me know, I can send it to you by mail.
    Cesar

  • Log4j is not picking up an appender

    I configured a logger "mailLogger" in the properties file, and I defined an appender "mail".
    In the code, I get the logger by name, and list all the appenders.
    It tells me there are no appenders.
    Shouldn't the "mail" appender be added by putting it in log4j.properties?
    My log4j properties file:
    log4j.rootLogger=INFO, xmlout
    log4j.logger.mailLogger=ALL, mail
    log4j.appender.xmlout=biz.minaret.log4j.DatedFileAppender
    log4j.appender.xmlout.threshold=DEBUG
    log4j.appender.xmlout.Directory=/app/usrlogs
    log4j.appender.xmlout.Prefix=sfgpo.
    log4j.appender.xmlout.Suffix=.log
    log4j.appender.xmlout.Append=true
    log4j.appender.xmlout.layout=org.apache.log4j.xml.XMLLayout
    log4j.appender.mail=org.apache.log4j.net.SMTPAppender
    log4j.appender.mail.To=[email protected]
    log4j.appender.mail.From="SFG Application Mailer"
    log4j.appender.mail.SMTPHost=myhost.myorg.com
    log4j.appender.mail.Threshold=ERROR
    log4j.appender.mail.BufferSize=1
    log4j.appender.mail.Subject=SFG PO event notice
    log4j.appender.mail.layout=com.moodys.sfg.utility.logger.MoodysHTMLLayout
    log4j.additivity.mailLogger=true
    log4j.appender.mail.layout.LocationInfo=true
    My java code:
    import java.util.Enumeration;
    import org.apache.log4j.Appender;
    import org.apache.log4j.Logger;
    import org.apache.log4j.net.SMTPAppender;
    Logger logger = Logger.getLogger("mailLogger");
    Enumeration en = logger.getAllAppenders();
    System.out.println("Listing all appenders for the logger");
    while (en.hasMoreElements())
         Appender a = (Appender)en.nextElement();
         String apName = a.getName();
         System.out.println(" Appender: name = " + apName);
    System.out.println("End of appender list");
    The console output is:
    Llisting all appenders for the logger
    End of appender list
    Why does it not find my appender "mail"?
    Thanks in advance.
    Elliot

    Hi Elliot.
    I had the same trouble and I resolved when I commented out the "rootLogger line", as follow below:
    # disabling line
    # log4j.rootLogger=INFO, xmlout
    I hope that helps you.
    Gui Pereira.

Maybe you are looking for