Using Multiple Loggers for SimpleFormatter AND XML

Discussion in 'Java' started by nico, Jan 18, 2008.

  1. nico

    nico Guest

    I posted this over at javaranch but didn't get a reply. It concerns
    Logging XML and simple formats:

    It seems that if I create two Logger objects, one using
    SimpleFormatting for the FileHandler, and the other using
    XMLFormatting for the FileHandler, and try to write to the same file
    (with appending set to true) it creates two separate files:

    logtest.log (Simple formatting)
    logtest.log.1 (xml formatting)

    However, it seems that the .log and .log.1 are always in the order of
    the Logger that I use (so if I put the xml first and the simple
    formatting second it will be .log for xml and .log.1 for simple). I'm
    thinking that it's probably a good idea to just have the simple
    default to .log and xml to .xml. Is it even possible to use both
    simple and xml formmated logging on the same file?

    I'm planning on letting the end-user have the option to set log style
    as a context init paramater and suppose I'd like to also allow them to
    provide the path/name as well. I could let them give a name and then
    manually append .xml or .log based on the log style but that would be
    ugly! ie mylog.log would get turned into mylog.log.xml!

    My system: uname -a
    Linux toshiba 2.6.22-14-generic i686 GNU/Linux

    Here's the code I used to test this:

    code:


    import java.io.*;
    import java.util.logging.*;

    public class LogTest {
    public static void main(String[] args) {

    /* Log using Simple Formatting */
    Logger log = Logger.getLogger("LogTester");
    try{
    FileHandler fileHandler = new FileHandler("logtest.log",
    true);
    fileHandler.setFormatter(new SimpleFormatter());
    log.addHandler(fileHandler);
    log.info("This is an info message logged simple");
    log.warning("This is warning message logged simple");
    }
    catch (IOException e){}

    /*Log using XML formmatting */
    Logger log2 = Logger.getLogger("LogTester");
    try{
    FileHandler xmlHandler= new FileHandler("logtest.log",true);
    xmlHandler.setFormatter(new XMLFormatter());
    log2.addHandler(xmlHandler);
    log2.info("This is info message logged as xml");
    log2.warning("This is warning message logged as xml");
    }
    catch (IOException e){}
    }
    }
     
    nico, Jan 18, 2008
    #1
    1. Advertising

  2. nico

    Arne Vajhøj Guest

    nico wrote:
    > I posted this over at javaranch but didn't get a reply. It concerns
    > Logging XML and simple formats:
    >
    > It seems that if I create two Logger objects, one using
    > SimpleFormatting for the FileHandler, and the other using
    > XMLFormatting for the FileHandler, and try to write to the same file
    > (with appending set to true) it creates two separate files:
    >
    > logtest.log (Simple formatting)
    > logtest.log.1 (xml formatting)
    >
    > However, it seems that the .log and .log.1 are always in the order of
    > the Logger that I use (so if I put the xml first and the simple
    > formatting second it will be .log for xml and .log.1 for simple). I'm
    > thinking that it's probably a good idea to just have the simple
    > default to .log and xml to .xml. Is it even possible to use both
    > simple and xml formmated logging on the same file?
    >
    > I'm planning on letting the end-user have the option to set log style
    > as a context init paramater and suppose I'd like to also allow them to
    > provide the path/name as well. I could let them give a name and then
    > manually append .xml or .log based on the log style but that would be
    > ugly! ie mylog.log would get turned into mylog.log.xml!


    It makes a lot of sense to me that log4j does not allow you to
    mix simple formatting and XML formatting in the same file.

    The result would be almost impossible to parse.

    I can not see why appending the approriate file extension
    should be bad. And it should ofcourse just be mylog.xml.

    But something else. I don't know your app and your users. But
    in many situations the system administrators would prefer
    instead of specifying simple or xml in web.xml and doing
    programatically configuration that the log4j configuration
    was an external file where they could specify formatter,
    filename, rotating and all that stuff.

    Arne
     
    Arne Vajhøj, Jan 19, 2008
    #2
    1. Advertising

  3. nico

    nico Guest

    On Jan 18, 5:31 pm, Arne Vajhøj <> wrote:
    > nico wrote:
    > > I posted this over at javaranch but didn't get a reply. It concerns
    > > Logging XML and simple formats:

    >
    > > It seems that if I create two Logger objects, one using
    > > SimpleFormatting for the FileHandler, and the other using
    > > XMLFormatting for the FileHandler, and try to write to the same file
    > > (with appending set to true) it creates two separate files:

    >
    > > logtest.log (Simple formatting)
    > > logtest.log.1 (xml formatting)

    >
    > > However, it seems that the .log and .log.1 are always in the order of
    > > the Logger that I use (so if I put the xml first and the simple
    > > formatting second it will be .log for xml and .log.1 for simple). I'm
    > > thinking that it's probably a good idea to just have the simple
    > > default to .log and xml to .xml. Is it even possible to use both
    > > simple and xml formmated logging on the same file?

    >
    > > I'm planning on letting the end-user have the option to set log style
    > > as a context init paramater and suppose I'd like to also allow them to
    > > provide the path/name as well. I could let them give a name and then
    > > manually append .xml or .log based on the log style but that would be
    > > ugly! ie mylog.log would get turned into mylog.log.xml!

    >
    > It makes a lot of sense to me that log4j does not allow you to
    > mix simple formatting and XML formatting in the same file.
    >
    > The result would be almost impossible to parse.
    >
    > I can not see why appending the approriate file extension
    > should be bad. And it should ofcourse just be mylog.xml.
    >
    > But something else. I don't know your app and your users. But
    > in many situations the system administrators would prefer
    > instead of specifying simple or xml in web.xml and doing
    > programatically configuration that the log4j configuration
    > was an external file where they could specify formatter,
    > filename, rotating and all that stuff.
    >
    > Arne


    Thanks for your reply Arne. I'm actually not using log4j, I'm using
    the java.util.logging package that's included in the jdk since 1.4
    (although I appreciate that log4j was first and may have better
    features to some).
     
    nico, Jan 19, 2008
    #3
  4. nico

    Arne Vajhøj Guest

    nico wrote:
    > Thanks for your reply Arne. I'm actually not using log4j, I'm using
    > the java.util.logging package that's included in the jdk since 1.4
    > (although I appreciate that log4j was first and may have better
    > features to some).


    My mistake.

    But I don't think it change much for my main points.

    Arne
     
    Arne Vajhøj, Jan 19, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Francois Bouffard

    Loggers and reloads

    Francois Bouffard, May 26, 2004, in forum: Python
    Replies:
    2
    Views:
    296
    =?ISO-8859-1?Q?Fran=E7ois_Bouffard?=
    May 27, 2004
  2. Jeff Shannon

    Logging with multiple loggers/handlers

    Jeff Shannon, Jul 6, 2004, in forum: Python
    Replies:
    3
    Views:
    341
    Jeff Shannon
    Aug 7, 2004
  3. temp00
    Replies:
    0
    Views:
    433
    temp00
    Jun 25, 2008
  4. Rhino
    Replies:
    8
    Views:
    516
    markspace
    Apr 30, 2010
  5. Novice

    SimpleFormatter Question

    Novice, Mar 10, 2012, in forum: Java
    Replies:
    7
    Views:
    2,249
    Novice
    Mar 10, 2012
Loading...

Share This Page