Geek
Geek

Reputation: 3329

logging the exceptions to a file

I have a stand alone class. I have to provide a logging mechanism for this file to log all the process taking place including exceptions. Later this would get integrated with a jar or so. What is the best logging mechanism which would be compatible even after integration. Since mine is just a class file which is not included in a jar, is it possible to use log4J? more suggestions welcome.

Upvotes: 2

Views: 903

Answers (3)

Marsellus Wallace
Marsellus Wallace

Reputation: 18601

You might want to use LogBack as an implementation of the generic slf4j framework. Note that log4j is an implementation of slf4j as well and this mean that if you program to the slf4 interface you can exchange logback and log4j by just replacing one jar with the other. Go with the first baby steps and note the generic imports of this introduction link. With logback you can instruct your app where to locate the configuration file with the following: -Dlogback.configurationFile=path/filename.ext

The exception that you're receiving (described in your comment) means that you probably did not configure log4j or that your app couldn't locate the configuration file.

Writing your own logger class or using the one provided by the util package is prob ably not a good idea..

Upvotes: 3

Marco Mondini
Marco Mondini

Reputation: 142

You can use java.util.logging.Logger, it's part of java since 1.4.2 (at least).

But the most appropriate thing you can do is using the logging framework you know you are going to use when your class will be integrated in the rest of the project.

Upvotes: 2

Bartvbl
Bartvbl

Reputation: 2928

I can recommend the use of a static class here, which you can define by yourself. For example something like this:

class Logger {
    public Logger() {
        //you can create/open a log file here
    }
    public static void log(String message, int messageType) {
        //write to the file, or whatever you want to do with the message
        System.out.println(message);
    }
}

Call new Logger(); somewhere during the startup of your program. Now you can call anywhere

Logger.log(message);

In my case this was more than sufficient, though very simple. I am not familiar with libraries like log4j, which may include more features.

Upvotes: 1

Related Questions