Separating log messages by level

Discussion in 'Java' started by James, Feb 6, 2009.

  1. James

    James Guest

    Hello all,
    Is there a way to have java.util.logging send LogRecords to different
    handlers based on their level?

    What I'm aiming for is a log file for "everything", and a separate
    file just for errors and above.

    Thanks!
    James
     
    James, Feb 6, 2009
    #1
    1. Advertising

  2. James

    James Guest

    > > What I'm aiming for is a log file for "everything", and a separate
    > > file just for errors and above.

    >
    > Yeah, put TWO FileHandlers in your Logger, set the Levels different, so
    > one logs everything and one logs just errors and up.
    >
    > See the "addHandler()" method (I think that's the name.)


    Ah, I should have said - I'm configuring logging from a properties
    file, and although I can add two FileHandlers:
    handlers= java.util.logging.FileHandler, java.util.logging.FileHandler

    I don't see a way to configure those two Handlers differently. Am I
    missing something?
     
    James, Feb 6, 2009
    #2
    1. Advertising

  3. James

    Arne Vajhøj Guest

    James wrote:
    >>> What I'm aiming for is a log file for "everything", and a separate
    >>> file just for errors and above.

    >> Yeah, put TWO FileHandlers in your Logger, set the Levels different, so
    >> one logs everything and one logs just errors and up.
    >>
    >> See the "addHandler()" method (I think that's the name.)

    >
    > Ah, I should have said - I'm configuring logging from a properties
    > file, and although I can add two FileHandlers:
    > handlers= java.util.logging.FileHandler, java.util.logging.FileHandler
    >
    > I don't see a way to configure those two Handlers differently. Am I
    > missing something?


    If you want to do advanced logging, then us elog4j instead
    of java.util.logging !

    :)

    One possible workaround is:

    package february;

    import java.io.IOException;
    import java.util.logging.FileHandler;

    public class FileHandlerA extends FileHandler {
    public FileHandlerA() throws IOException, SecurityException {
    super();
    }
    }

    package february;

    import java.io.IOException;
    import java.util.logging.FileHandler;

    public class FileHandlerB extends FileHandler {
    public FileHandlerB() throws IOException, SecurityException {
    super();
    }
    }

    february.MultiLog.level = FINE
    handlers = february.FileHandlerA,february.FileHandlerB
    february.FileHandlerA.level = FINE
    february.FileHandlerA.formatter = java.util.logging.SimpleFormatter
    february.FileHandlerA.pattern = C:/log1.log
    february.FileHandlerB.level = SEVERE
    february.FileHandlerB.formatter = java.util.logging.SimpleFormatter
    february.FileHandlerB.pattern = C:/log2.log

    Not particular elegant, but it works.

    Arne
     
    Arne Vajhøj, Feb 7, 2009
    #3
    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. Brian Donovan

    Replacing python: in syslog log messages

    Brian Donovan, Sep 5, 2003, in forum: Python
    Replies:
    1
    Views:
    414
    Jp Calderone
    Sep 6, 2003
  2. pabbu
    Replies:
    8
    Views:
    769
    Marc Boyer
    Nov 7, 2005
  3. Replies:
    3
    Views:
    309
  4. Barak, Ron
    Replies:
    0
    Views:
    290
    Barak, Ron
    Nov 24, 2009
  5. Barak, Ron
    Replies:
    3
    Views:
    311
    Vinay Sajip
    Nov 24, 2009
Loading...

Share This Page