Selective logging

Discussion in 'Java' started by tnorgd, Sep 17, 2009.

  1. tnorgd

    tnorgd Guest

    Hi,

    I am using java.util.logging. I would like to be able to selectively
    change a log-level in a desired branch of code, e.g.: in a class
    my.package.util.* or in my.package.gui.*. I would like to do this as a
    command line switch specified when a program is started. I would
    prefer to avoid editing a logger config file. I expect it is quite
    easy, but no good solution comes to my mind after reading API for
    java.util.logging.LogManager. Could you give me some clues?

    Best,
    Dominik
     
    tnorgd, Sep 17, 2009
    #1
    1. Advertising

  2. tnorgd

    Arne Vajhøj Guest

    tnorgd wrote:
    > I am using java.util.logging. I would like to be able to selectively
    > change a log-level in a desired branch of code, e.g.: in a class
    > my.package.util.* or in my.package.gui.*. I would like to do this as a
    > command line switch specified when a program is started. I would
    > prefer to avoid editing a logger config file. I expect it is quite
    > easy, but no good solution comes to my mind after reading API for
    > java.util.logging.LogManager. Could you give me some clues?


    Logger.getLogger(loggername).setLevel(loglevel);

    Arne
     
    Arne Vajhøj, Sep 17, 2009
    #2
    1. Advertising

  3. tnorgd

    Arne Vajhøj Guest

    Arne Vajhøj wrote:
    > tnorgd wrote:
    >> I am using java.util.logging. I would like to be able to selectively
    >> change a log-level in a desired branch of code, e.g.: in a class
    >> my.package.util.* or in my.package.gui.*. I would like to do this as a
    >> command line switch specified when a program is started. I would
    >> prefer to avoid editing a logger config file. I expect it is quite
    >> easy, but no good solution comes to my mind after reading API for
    >> java.util.logging.LogManager. Could you give me some clues?

    >
    > Logger.getLogger(loggername).setLevel(loglevel);


    I assume your loggernames follow package and class name
    naming convention.

    Arne
     
    Arne Vajhøj, Sep 17, 2009
    #3
  4. "tnorgd" <> wrote in message
    news:
    > Hi,
    >
    > I am using java.util.logging. I would like to be able to
    > selectively change a log-level in a desired branch of
    > code, e.g.: in a class my.package.util.* or in
    > my.package.gui.*. I would like to do this as a command
    > line switch specified when a program is started. I would
    > prefer to avoid editing a logger config file. I expect it
    > is quite easy, but no good solution comes to my mind
    > after reading API for java.util.logging.LogManager. Could
    > you give me some clues?
    >
    > Best,
    > Dominik


    No. I think you need to create/provide an own Handler instance for each
    package in question.

    That is up to you, but the default package does not handle it. Each package
    inherits the Handler from its parent, if it does not have a specific handler
    defined for it.

    Needs your own custom code.
     
    Donkey Hottie, Sep 17, 2009
    #4
  5. tnorgd

    markspace Guest

    Donkey Hottie wrote:

    > No. I think you need to create/provide an own Handler instance for each
    > package in question.
    >
    > That is up to you, but the default package does not handle it. Each
    > package inherits the Handler from its parent, if it does not have a
    > specific handler defined for it.



    I think Arne's solution was more correct. At least if you follow the
    naming conventions, you can manipulate the loggers by name. I would
    have said something similar to you, Donkey, but Arne's solution was
    pretty elegant and straight-forward.


    >
    > Needs your own custom code.



    Should probably experiment with this a bit before rolling your own.
     
    markspace, Sep 18, 2009
    #5
  6. tnorgd

    tnorgd Guest

    I see, if I name my loggers by a class name, e.g.
    Logger l = Logger.getLogger("my.package.SomeClass");

    then it is easy to implement a command-line switch for:
    java MyProg -suppress my.package.SomeClass

    But how to implement this:
    java MyProg -suppress my.package

    One possibility is to ask a LogManager for all loggers and then to
    test which logger fits to a given string.

    This looks interesting, e.g. I can use regular expression to control
    the loggers. But isn't there any simpler solution (i.e. somethin
    already existing in java.util.logging)?

    Thanks a lot for help!

    Best,
    Dominik
     
    tnorgd, Sep 18, 2009
    #6
  7. tnorgd

    Arne Vajhøj Guest

    tnorgd wrote:
    > I see, if I name my loggers by a class name, e.g.
    > Logger l = Logger.getLogger("my.package.SomeClass");
    >
    > then it is easy to implement a command-line switch for:
    > java MyProg -suppress my.package.SomeClass
    >
    > But how to implement this:
    > java MyProg -suppress my.package
    >
    > One possibility is to ask a LogManager for all loggers and then to
    > test which logger fits to a given string.
    >
    > This looks interesting, e.g. I can use regular expression to control
    > the loggers. But isn't there any simpler solution (i.e. somethin
    > already existing in java.util.logging)?


    You do not need to do anything.

    When you set the log level of my.package it is default
    applied to my.package.SomeClass and my.package.AnotherClass
    as well.

    Arne
     
    Arne Vajhøj, Sep 18, 2009
    #7
  8. tnorgd

    Arne Vajhøj Guest

    Arne Vajhøj wrote:
    > tnorgd wrote:
    >> I see, if I name my loggers by a class name, e.g.
    >> Logger l = Logger.getLogger("my.package.SomeClass");
    >>
    >> then it is easy to implement a command-line switch for:
    >> java MyProg -suppress my.package.SomeClass
    >>
    >> But how to implement this:
    >> java MyProg -suppress my.package
    >>
    >> One possibility is to ask a LogManager for all loggers and then to
    >> test which logger fits to a given string.
    >>
    >> This looks interesting, e.g. I can use regular expression to control
    >> the loggers. But isn't there any simpler solution (i.e. somethin
    >> already existing in java.util.logging)?

    >
    > You do not need to do anything.
    >
    > When you set the log level of my.package it is default
    > applied to my.package.SomeClass and my.package.AnotherClass
    > as well.


    Demo:

    import java.util.logging.Level;
    import java.util.logging.Logger;

    public class LogLevel {
    public static void main(String[] args) {
    Logger one = Logger.getLogger("my.package.SomeClass");
    Logger two = Logger.getLogger("my.package.AnotherClass");
    one.info("bla bla 1A");
    two.info("bla bla 2A");
    Logger.getLogger("my.package").setLevel(Level.WARNING);
    one.info("bla bla 1B");
    two.info("bla bla 2B");
    Logger.getLogger("my.package").setLevel(Level.INFO);
    one.info("bla bla 1C");
    two.info("bla bla 2C");
    }
    }

    Arne
     
    Arne Vajhøj, Sep 18, 2009
    #8
  9. tnorgd

    markspace Guest

    tnorgd wrote:
    >
    > But how to implement this:
    > java MyProg -suppress my.package



    You can use a system property for this:

    java -Djava.util.logging.config.class=mylogging.ConfigClass MyProg

    This will by-pass the properties file.

    <http://java.sun.com/javase/6/docs/api/java/util/logging/LogManager.html>


    >
    > One possibility is to ask a LogManager for all loggers and then to
    > test which logger fits to a given string.



    As mentioned, loggers are hierarchical. If "my.package" is set to a
    given level, then all loggers in that package space will initially use
    the same level.
     
    markspace, Sep 18, 2009
    #9
  10. tnorgd

    tnorgd Guest

    That works very well! Thanks a lot,
    Dominik
     
    tnorgd, Sep 18, 2009
    #10
  11. "markspace" <> wrote in message
    news:h8ugb0$gr4$-september.org
    > Donkey Hottie wrote:
    >
    >> No. I think you need to create/provide an own Handler
    >> instance for each package in question.
    >>
    >> That is up to you, but the default package does not
    >> handle it. Each package inherits the Handler from its
    >> parent, if it does not have a specific handler defined
    >> for it.

    >
    >
    > I think Arne's solution was more correct. At least if
    > you follow the naming conventions, you can manipulate the
    > loggers by name. I would have said something similar to
    > you, Donkey, but Arne's solution was pretty elegant and
    > straight-forward.
    >
    >
    >>
    >> Needs your own custom code.

    >
    >
    > Should probably experiment with this a bit before rolling
    > your own.


    Agreed. I don't know what I was thinking.. if anything.
     
    Donkey Hottie, Sep 18, 2009
    #11
    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. Bobby Maul
    Replies:
    2
    Views:
    471
    Eddie Dodd
    May 27, 2004
  2. =?Utf-8?B?TGlsbGlhbg==?=

    Selective DataGrid

    =?Utf-8?B?TGlsbGlhbg==?=, Jun 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    370
    Marina
    Jun 2, 2004
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    selective validation on specific buttons only

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Mar 22, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    405
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Mar 22, 2005
  4. =?Utf-8?B?U29saXR1cw==?=

    Selective databinding

    =?Utf-8?B?U29saXR1cw==?=, Oct 10, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    406
    Karl Seguin
    Oct 10, 2005
  5. yasaswi

    selective page access

    yasaswi, Feb 3, 2005, in forum: Java
    Replies:
    2
    Views:
    320
    yasaswi
    Feb 4, 2005
Loading...

Share This Page