Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

How to solve the exception logging problem which I have in Java?

I have used logger object and add file handler to log the exceptions in a log file of a Java project. It is working fine, when I run it from NetBeans IDE, but when I run it as a standalone application from console, the exceptions are logged in console and the file is not even created in the location I have specified.

private static final Logger LOGGER = Logger.getLogger(BuyerRegistration.class.getName());

    public static void main(String args[]) {
        
        Formatter simpleFormatter = null;
        Handler fileHandler = null;
        try {
            LOGGER.setUseParentHandlers(false);
            fileHandler = new FileHandler("dist/lib/CurryHouse.log", true);
            simpleFormatter = new SimpleFormatter();
            LOGGER.addHandler(fileHandler);
            fileHandler.setFormatter(simpleFormatter);
            fileHandler.setLevel(Level.ALL);
            LOGGER.setLevel(Level.ALL);
        } catch (IOException exception) {
            LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception);
        }

The above code will come inside a BuyerRegistration class. Also, I have attached the following code in all catch blocks to log the exception in the file.

LOGGER.log(Level.SEVERE, "{0}
", "Bills:352" + e.toString());

I want to the code to log all the exceptions in the location I specify rather than logging in the console


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...