java util 日志消息文件未在用户目录中生成
java util logging message file not generating in users directory
我已经在我的项目中配置了util.logging
。在 logging.properties
文件中记录格式化程序和模式详细信息,并尝试从 java 代码中读取属性文件。但是在 logging.properties
文件中声明时,日志并未写入文件位置。
请在下面找到我的代码。
问题是:myApp.log
文件没有在我的 users.home
目录中生成。但是日志消息正在我的 eclipse 控制台中打印。
AEvent.java
package com.jnlp;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Logger;
class AEvent extends Frame implements ActionListener{
private static final long serialVersionUID = 1L;
private LoadLogPropertiesFile logProp=LoadLogPropertiesFile.getInstance();
private static Logger logger = Logger.getLogger(LoadLogPropertiesFile.class.getName());
TextField tf;
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
public static void main(String args[]){
logger.info("Test logging....");
new AEvent();
}
}
加载日志属性文件
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
public class LoadLogPropertiesFile {
private static LoadLogPropertiesFile instance = new LoadLogPropertiesFile();
public static LoadLogPropertiesFile getInstance() {
return instance;
}
static {
System.out.println("Loading log properties file -->");
String path = LoadLogPropertiesFile.class.getClassLoader().getResource("logging.properties").getFile();
}
}
logging.properties
# Logging
handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=INFO
# File Logging
java.util.logging.FileHandler.pattern=%h/myApp.log
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=INFO
# Console Logging
java.util.logging.ConsoleHandler.level=INFO
请找到我的项目结构。
您似乎没有将配置应用到 LogManager。你想做这样的事情:
static {
System.out.println("Loading log properties file -->");
try (InputStream in = LoadLogPropertiesFile.class.getResourceAsStream("logging.properties")) {
if (in != null) {
LogManager.getLogManager().readConfiguration(in);
} else {
System.err.println("Configuration file not found.");
}
}
}
我已经在我的项目中配置了util.logging
。在 logging.properties
文件中记录格式化程序和模式详细信息,并尝试从 java 代码中读取属性文件。但是在 logging.properties
文件中声明时,日志并未写入文件位置。
请在下面找到我的代码。
问题是:myApp.log
文件没有在我的 users.home
目录中生成。但是日志消息正在我的 eclipse 控制台中打印。
AEvent.java
package com.jnlp;
import java.awt.*;
import java.awt.event.*;
import java.util.logging.Logger;
class AEvent extends Frame implements ActionListener{
private static final long serialVersionUID = 1L;
private LoadLogPropertiesFile logProp=LoadLogPropertiesFile.getInstance();
private static Logger logger = Logger.getLogger(LoadLogPropertiesFile.class.getName());
TextField tf;
AEvent(){
//create components
tf=new TextField();
tf.setBounds(60,50,170,20);
Button b=new Button("click me");
b.setBounds(100,120,80,30);
//register listener
b.addActionListener(this);//passing current instance
//add components and set size, layout and visibility
add(b);add(tf);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
}
public static void main(String args[]){
logger.info("Test logging....");
new AEvent();
}
}
加载日志属性文件
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.LogManager;
public class LoadLogPropertiesFile {
private static LoadLogPropertiesFile instance = new LoadLogPropertiesFile();
public static LoadLogPropertiesFile getInstance() {
return instance;
}
static {
System.out.println("Loading log properties file -->");
String path = LoadLogPropertiesFile.class.getClassLoader().getResource("logging.properties").getFile();
}
}
logging.properties
# Logging
handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=INFO
# File Logging
java.util.logging.FileHandler.pattern=%h/myApp.log
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=INFO
# Console Logging
java.util.logging.ConsoleHandler.level=INFO
请找到我的项目结构。
您似乎没有将配置应用到 LogManager。你想做这样的事情:
static {
System.out.println("Loading log properties file -->");
try (InputStream in = LoadLogPropertiesFile.class.getResourceAsStream("logging.properties")) {
if (in != null) {
LogManager.getLogManager().readConfiguration(in);
} else {
System.err.println("Configuration file not found.");
}
}
}