使用 java.util.logg.Logger 时如何在文本文件中写入日志

在这种情况下,我希望将自己创建的所有日志写入一个文本文件。

我们使用 java.util.loggs.LoggerAPI 来生成日志。

我试过:

private static Logger logger = Logger.getLogger(className.class.getName());
FileHandler fh;
fh = new FileHandler("C:/className.log");
logger.addHandler(fh);

但还是只能在控制台上看到我的日志。

471486 次浏览

试试这个样品,对我很有效。

public static void main(String[] args) {


Logger logger = Logger.getLogger("MyLog");
FileHandler fh;


try {


// This block configure the logger with handler and formatter
fh = new FileHandler("C:/temp/test/MyLogFile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);


// the following statement is used to log any messages
logger.info("My first log");


} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


logger.info("Hi How r u?");


}

在 MyLogFile.log 上生成输出

Apr 2, 2013 9:57:08 AM testing.MyLogger main
INFO: My first log
Apr 2, 2013 9:57:08 AM testing.MyLogger main
INFO: Hi How r u?

编辑:

若要删除控制台处理程序,请使用

logger.setUseParentHandlers(false);

因为 Console Handler 是在所有日志记录器派生的父日志记录器中注册的。

一个名为 用于 Java 的 log4j的好库。
这将提供许多功能。通过链接,您将找到您的解决方案。

int SIZE = "<intialize-here>"
int ROTATIONCOUNT = "<intialize-here>"


Handler handler = new FileHandler("test.log", SIZE, LOG_ROTATIONCOUNT);
logger.addHandler(handler);     // for your code..


// you can also set logging levels
Logger.getLogger(this.getClass().getName()).log(Level.[...]).addHandler(handler);
import java.io.IOException;
import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;


/**
* @author Kiran
*
*/
public class MyLogger {


public MyLogger() {
}


public static void main(String[] args) {
Logger logger = Logger.getLogger("MyLog");
Appender fh = null;
try {
fh = new FileAppender(new SimpleLayout(), "MyLogFile.log");
logger.addAppender(fh);
fh.setLayout(new SimpleLayout());
logger.info("My first log");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Hi How r u?");
}
}

也许 这是你需要什么..。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


/**
* LogToFile class
* This class is intended to be use with the default logging class of java
* It save the log in an XML file  and display a friendly message to the user
* @author Ibrabel <ibrabel@gmail.com>
*/
public class LogToFile {


protected static final Logger logger=Logger.getLogger("MYLOG");
/**
* log Method
* enable to log all exceptions to a file and display user message on demand
* @param ex
* @param level
* @param msg
*/
public static void log(Exception ex, String level, String msg){


FileHandler fh = null;
try {
fh = new FileHandler("log.xml",true);
logger.addHandler(fh);
switch (level) {
case "severe":
logger.log(Level.SEVERE, msg, ex);
if(!msg.equals(""))
JOptionPane.showMessageDialog(null,msg,
"Error", JOptionPane.ERROR_MESSAGE);
break;
case "warning":
logger.log(Level.WARNING, msg, ex);
if(!msg.equals(""))
JOptionPane.showMessageDialog(null,msg,
"Warning", JOptionPane.WARNING_MESSAGE);
break;
case "info":
logger.log(Level.INFO, msg, ex);
if(!msg.equals(""))
JOptionPane.showMessageDialog(null,msg,
"Info", JOptionPane.INFORMATION_MESSAGE);
break;
case "config":
logger.log(Level.CONFIG, msg, ex);
break;
case "fine":
logger.log(Level.FINE, msg, ex);
break;
case "finer":
logger.log(Level.FINER, msg, ex);
break;
case "finest":
logger.log(Level.FINEST, msg, ex);
break;
default:
logger.log(Level.CONFIG, msg, ex);
break;
}
} catch (IOException | SecurityException ex1) {
logger.log(Level.SEVERE, null, ex1);
} finally{
if(fh!=null)fh.close();
}
}


public static void main(String[] args) {


/*
Create simple frame for the example
*/
JFrame myFrame = new JFrame();
myFrame.setTitle("LogToFileExample");
myFrame.setSize(300, 100);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLocationRelativeTo(null);
JPanel pan = new JPanel();
JButton severe = new JButton("severe");
pan.add(severe);
JButton warning = new JButton("warning");
pan.add(warning);
JButton info = new JButton("info");
pan.add(info);


/*
Create an exception on click to use the LogToFile class
*/
severe.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent ae) {
int j = 20, i = 0;
try {
System.out.println(j/i);
} catch (ArithmeticException ex) {
log(ex,"severe","You can't divide anything by zero");
}


}


});


warning.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent ae) {
int j = 20, i = 0;
try {
System.out.println(j/i);
} catch (ArithmeticException ex) {
log(ex,"warning","You can't divide anything by zero");
}


}


});


info.addActionListener(new ActionListener(){


@Override
public void actionPerformed(ActionEvent ae) {
int j = 20, i = 0;
try {
System.out.println(j/i);
} catch (ArithmeticException ex) {
log(ex,"info","You can't divide anything by zero");
}


}


});


/*
Add the JPanel to the JFrame and set the JFrame visible
*/
myFrame.setContentPane(pan);
myFrame.setVisible(true);
}
}

首先,您在哪里定义您的日志记录器,从哪个类方法调用它?有一个很有效的例子,新鲜出炉的:

public class LoggingTester {
private final Logger logger = Logger.getLogger(LoggingTester.class
.getName());
private FileHandler fh = null;


public LoggingTester() {
//just to make our log file nicer :)
SimpleDateFormat format = new SimpleDateFormat("M-d_HHmmss");
try {
fh = new FileHandler("C:/temp/test/MyLogFile_"
+ format.format(Calendar.getInstance().getTime()) + ".log");
} catch (Exception e) {
e.printStackTrace();
}


fh.setFormatter(new SimpleFormatter());
logger.addHandler(fh);
}


public void doLogging() {
logger.info("info msg");
logger.severe("error message");
logger.fine("fine message"); //won't show because to high level of logging
}
}

在你的代码中你忘了定义格式化程序,如果你需要简单的格式化程序,你可以像我上面提到的那样做,但是还有另外一个选项,你可以自己格式化它,这里有一个例子(只需要插入它而不是下面的代码行 fh.setFormatter (new SimpleFormatter ())) :

fh.setFormatter(new Formatter() {
@Override
public String format(LogRecord record) {
SimpleDateFormat logTime = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
Calendar cal = new GregorianCalendar();
cal.setTimeInMillis(record.getMillis());
return record.getLevel()
+ logTime.format(cal.getTime())
+ " || "
+ record.getSourceClassName().substring(
record.getSourceClassName().lastIndexOf(".")+1,
record.getSourceClassName().length())
+ "."
+ record.getSourceMethodName()
+ "() : "
+ record.getMessage() + "\n";
}
});

或者任何你喜欢的修改,希望能有帮助。

可以通过 logging.properties 文件控制日志文件的位置。它可以作为 JVM 参数 ex: java -Djava.util.logging.config.file=/scratch/user/config/logging.properties传递

详情: https://docs.oracle.com/cd/E23549_01/doc.1111/e14568/handler.htm

配置文件处理程序

要将日志发送到文件,请将 FileHandler 添加到 logging.properties 文件中的 Handlers 属性。这将启用全局文件日志记录。

通过设置以下属性配置处理程序:

java.util.logging.FileHandler.pattern=<home directory>/logs/oaam.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

Pattern 指定输出文件的位置和模式。

Limit 以字节为单位指定日志记录器写入任何一个文件的最大数量。

Count 指定要循环访问多少个输出文件。

Util.log.Formatter 指定文件处理程序类用于格式化日志消息的 java.util.log 格式化程序类。SimpleFormatter 编写日志记录的简短“人类可读”摘要。


要指示 java 使用这个配置文件而不是 $JDK _ HOME/jre/lib/logging.properties:

java -Djava.util.logging.config.file=/scratch/user/config/logging.properties

这是我的日志类 基于公认的答案:

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;


public class ErrorLogger
{
private Logger logger;


public ErrorLogger()
{
logger = Logger.getAnonymousLogger();


configure();
}


private void configure()
{
try
{
String logsDirectoryFolder = "logs";
Files.createDirectories(Paths.get(logsDirectoryFolder));
FileHandler fileHandler = new FileHandler(logsDirectoryFolder + File.separator + getCurrentTimeString() + ".log");
logger.addHandler(fileHandler);
SimpleFormatter formatter = new SimpleFormatter();
fileHandler.setFormatter(formatter);
} catch (IOException exception)
{
exception.printStackTrace();
}


addCloseHandlersShutdownHook();
}


private void addCloseHandlersShutdownHook()
{
Runtime.getRuntime().addShutdownHook(new Thread(() ->
{
// Close all handlers to get rid of empty .LCK files
for (Handler handler : logger.getHandlers())
{
handler.close();
}
}));
}


private String getCurrentTimeString()
{
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
return dateFormat.format(new Date());
}


public void log(Exception exception)
{
logger.log(Level.SEVERE, "", exception);
}
}

希望大家觉得这个有用

public static void writeLog(String info) {
String filename = "activity.log";
String FILENAME = "C:\\testing\\" + filename;
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(FILENAME, true);
bw = new BufferedWriter(fw);
bw.write(info);
bw.write("\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

下面是如何从代码中覆盖 Logger 配置的示例。不需要外部配置文件。

Java:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;


public class FileLoggerTest {


public static void main(String[] args) {


try {
String h = MyLogHandler.class.getCanonicalName();
StringBuilder sb = new StringBuilder();
sb.append(".level=ALL\n");
sb.append("handlers=").append(h).append('\n');
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(sb.toString().getBytes("UTF-8")));
} catch (IOException | SecurityException ex) {
// Do something about it
}


Logger.getGlobal().severe("Global SEVERE log entry");
Logger.getLogger(FileLoggerTest.class.getName()).log(Level.SEVERE, "This is a SEVERE log entry");
Logger.getLogger("SomeName").log(Level.WARNING, "This is a WARNING log entry");
Logger.getLogger("AnotherName").log(Level.INFO, "This is an INFO log entry");
Logger.getLogger("SameName").log(Level.CONFIG, "This is an CONFIG log entry");
Logger.getLogger("SameName").log(Level.FINE, "This is an FINE log entry");
Logger.getLogger("SameName").log(Level.FINEST, "This is an FINEST log entry");
Logger.getLogger("SameName").log(Level.FINER, "This is an FINER log entry");
Logger.getLogger("SameName").log(Level.ALL, "This is an ALL log entry");


}
}

我的 LogHandler.java

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.SimpleFormatter;


public final class MyLogHandler extends FileHandler {


public MyLogHandler() throws IOException, SecurityException {
super("/tmp/path-to-log.log");
setFormatter(new SimpleFormatter());
setLevel(Level.ALL);
}


@Override
public void publish(LogRecord record) {
System.out.println("Some additional logic");
super.publish(record);
}


}