如何解决我在 Java 中遇到的异常日志记录问题?
How to solve the exception logging problem which I have in Java?
我使用了记录器对象并添加了文件处理程序来在 Java 项目的日志文件中记录异常。它工作正常,当我 运行 它来自 NetBeans IDE,但是当我 运行 它作为来自控制台的独立应用程序时,异常记录在控制台中并且甚至没有创建文件在我指定的位置。
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);
}
以上代码将进入 BuyerRegistration class。此外,我在所有 catch 块中附加了以下代码以在文件中记录异常。
LOGGER.log(Level.SEVERE, "{0}\n", "Bills:352\t" + e.toString());
我想让代码在我指定的位置记录所有异常,而不是在控制台中记录
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.
您正在使用 relative path which is altered when the current directory is changed. Use a absolute path, a FileHandler pattern,或者在启动器中设置当前目录。
I want to the code to log all the exceptions in the location I specify rather than logging in the console
将您的处理程序添加到根记录器而不是您命名的记录器。您可以通过提供 logging.properties or a config class.
来做到这一点
private static final Logger ROOT = Logger.getLogger("");
static {
Formatter simpleFormatter = null;
Handler fileHandler = null;
try {
//TODO: Fix file path
fileHandler = new FileHandler("%h/CurryHouse.log", true);
simpleFormatter = new SimpleFormatter();
LOGGER.addHandler(fileHandler);
fileHandler.setFormatter(simpleFormatter);
fileHandler.setLevel(Level.ALL);
ROOT.addHandler(fileHandler);
} catch (IOException exception) {
LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception);
}
}
public static void main(String args[]) {
//Your app.
}
Also, I have attached the following code in all catch blocks to log the exception in the file.
您可能需要考虑记录完整的堆栈跟踪:
LOGGER.log(Level.SEVERE, "Bills:352", e);
我使用了记录器对象并添加了文件处理程序来在 Java 项目的日志文件中记录异常。它工作正常,当我 运行 它来自 NetBeans IDE,但是当我 运行 它作为来自控制台的独立应用程序时,异常记录在控制台中并且甚至没有创建文件在我指定的位置。
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);
}
以上代码将进入 BuyerRegistration class。此外,我在所有 catch 块中附加了以下代码以在文件中记录异常。
LOGGER.log(Level.SEVERE, "{0}\n", "Bills:352\t" + e.toString());
我想让代码在我指定的位置记录所有异常,而不是在控制台中记录
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.
您正在使用 relative path which is altered when the current directory is changed. Use a absolute path, a FileHandler pattern,或者在启动器中设置当前目录。
I want to the code to log all the exceptions in the location I specify rather than logging in the console
将您的处理程序添加到根记录器而不是您命名的记录器。您可以通过提供 logging.properties or a config class.
来做到这一点private static final Logger ROOT = Logger.getLogger("");
static {
Formatter simpleFormatter = null;
Handler fileHandler = null;
try {
//TODO: Fix file path
fileHandler = new FileHandler("%h/CurryHouse.log", true);
simpleFormatter = new SimpleFormatter();
LOGGER.addHandler(fileHandler);
fileHandler.setFormatter(simpleFormatter);
fileHandler.setLevel(Level.ALL);
ROOT.addHandler(fileHandler);
} catch (IOException exception) {
LOGGER.log(Level.SEVERE, "Error occur in FileHandler.", exception);
}
}
public static void main(String args[]) {
//Your app.
}
Also, I have attached the following code in all catch blocks to log the exception in the file.
您可能需要考虑记录完整的堆栈跟踪:
LOGGER.log(Level.SEVERE, "Bills:352", e);