Results 1 to 10 of 10
  1. #1
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Logger (static or not)

    Hi,

    I have a small program with several classes.
    Each class can send an error to the errorlog.

    Is it better to make the call to the errorlog static ?

    I want also that this errorlog notifies an observer, but this cannot be done by a static class.

    What is the best way to do ?

    Kind regards
    Dipke

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Logger (static or not)

    There is no such thing as a static class unless you are talking about an inner class. In that case, they are just like regular classes and can be instantiated. The primary difference is that you don't need to have an existing instance of the enclosing class to instantiate them like you do for a non-static inner class.

    I suggest you submit a Short, Self Contained, Correct Example to show what you want to do and why it doesn't work.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Re: Logger (static or not)

    Hi Jim,

    I mean that is use static methods in the logger class and not a static class.
    When i want to log something, i just call the static method sendToLog("message") in the logger class.

    Java Code:
    public static void green(String module, String errorText) {
            sendToLog(Logging.Level.GREEN, module, errorText);
        }
    But i aslo want to notify an observer telling him that there is a change in the logs.

    That's where i have a problem.

    Kiind regards
    Dipke

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Logger (static or not)

    Depends what you want your logger to be able to do.
    If you want to register a name for a particular logger (so the logger for class Foo has a "Foo" name), then static methods won't help.
    If you don't care about that, then they would work.

    Also, for the notification, all the observer functionality would have to be static.
    Please do not ask for code as refusal often offends.

  5. #5
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Re: Logger (static or not)

    The name of the lof file is not important.
    All classes are sending to one logfile.

    But there are about 15 classes that sends to the logfile.
    therefore i use a static method.

    But this example won't work.

    Java Code:
    public class Msg extends Observable {
        
        public static void green(String module, String errorText) {
            sendToLog(Logging.Level.GREEN, module, errorText);
            setChanged();
            notifyObservers();
        }
    I cannot do "setchanged" and 'notifyObservers" in a static method.

    What is then the best way to do ?

    Do i have to pass the class (without static methods) to all of my classes that needs to use the log ?

    kind regards
    Dipke

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Logger (static or not)

    Quote Originally Posted by Dipke View Post

    I cannot do "setchanged" and 'notifyObservers" in a static method.
    So why not pass an instance of Observable to your method?

    Java Code:
    public static void green(String module, String errorText, Observable obs) {
        sendToLog(Logging.Level.GREEN, module, errorText);
        obs.setChanged();
        obs.notifyObservers();
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Re: Logger (static or not)

    Hi,

    Thanks for the answer, but "setChanged" is protected according the api.
    So i get an error message.

    Or do i something wrong ?

    Kind regards
    Dipke

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Logger (static or not)

    You can only call setChanged from a subclass of Observable or from another method in the same package. If you can provide more detail on what you want to accomplish, there may be another way of handling this.

    Regards,
    Jim
    Last edited by jim829; 09-08-2013 at 10:59 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Dipke is offline Member
    Join Date
    Aug 2010
    Location
    Leuven, Belgium
    Posts
    86
    Rep Power
    0

    Default Re: Logger (static or not)

    The program runs 24/7 in the background and checks for some file creations and threats the file.
    So there is no direct interaction with a user and everything has to be logged for troubleshooting.
    This means that a lot of classes sends there action to a log (about 15).
    I have created a small GUI that displays the logs and wants that when an entry is made in the log, the GUI updates.
    Therefore i have created the static methods for the log and i want to use an obeserver for the GUI.

    Dipke

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: Logger (static or not)

    I have not used the Observable interface before but I am not sure why you need to have the static method notify the observers. As you explained you want your GUI to observe all of these classes. So the GUI is the observer and must implement the Observer interface.

    For each of your other classes, they must extend the Observable class. This means they can do a setChanged() without problems when they want to report a status change. For each of those classes, the GUI would do an addObserver(this) which registers it as an observer of that class.

    Now when the classes change, they can call your static method, then do a setChanged(), and then notifyObservers(). So you don't need to pass an instance of the Observable class to your static method as I had once suggested.

    Regards,
    Jim
    Last edited by jim829; 09-09-2013 at 01:58 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Logger
    By Dipke in forum New To Java
    Replies: 4
    Last Post: 05-12-2013, 06:46 PM
  2. A better Java logger
    By tony-walker in forum Reviews / Advertising
    Replies: 0
    Last Post: 09-09-2010, 01:32 PM
  3. Logger + FileHandler problems
    By JonJacobs in forum New To Java
    Replies: 11
    Last Post: 08-19-2010, 02:19 PM
  4. Java Logger ( How to control it better)
    By itaipee in forum New To Java
    Replies: 3
    Last Post: 03-03-2009, 10:35 PM
  5. Help with key Logger in java
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 05:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •