Java:这种用于保存基本JDK日志文件的单例类的正确行为? [英] Java: Is this proper behavior of singleton class used to save basic JDK log file?

查看:139
本文介绍了Java:这种用于保存基本JDK日志文件的单例类的正确行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个单例记录器类的下面的包装器,以便我可以从任何文件和/或类中记录异常。我的灵感来自于这里:记录到一个文件多个java文件



我是java的新手,这是我第一次尝试将异常记录到文件中。虽然以下代码正常工作,但我注意到我想询问他们是否为正常行为或是否存在解决方法的一些怪癖。



(1)一次创建日志文件并将其写入到异常中,如果我编辑日志文件(例如删除一些文本行),那么该日志文件从此不会再次被写入。在再次写入日志文件之前,Web服务器需要重新启动与Web应用程序关联的域。这是正常吗?理想情况下,我只想保留日志文件中的相关错误,并删除不相关的错误,而无需重新启动Web服务器中的Web应用程序域。



2)如果我删除日志文件,那么以后Java程序会发生异常,日志文件不会再次创建,异常丢失。再次,在创建新的日志文件之前,Web服务器需要重新启动与Web应用程序关联的域。这是正常的,还是有办法通过编辑下面的代码吗?理想情况下,我希望能够随时删除日志文件,并让应用程序创建一个新的。



再次,我是新来的,所以感觉自由指出上述意图是不好的设计等。



------代码如下------



这是文件:LoggerWrapper.java

  import java.io.OutputStream; 
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;

public class LoggerWrapper {
public static final Logger myLogger = Logger.getLogger(Test);
private static LoggerWrapper instance = null;

public static LoggerWrapper getInstance(){
if(instance == null){
prepareLogger();
instance = new LoggerWrapper();
}
return instance;
}

private static void prepareLogger(){
try {
FileHandler myFileHandler = new FileHandler(/ path / to / myLogFile.log,true);
myFileHandler.setFormatter(new SimpleFormatter());
myLogger.addHandler(myFileHandler);
myLogger.setUseParentHandlers(false);
myLogger.setLevel(Level.ALL);
} catch(例外e){
...
}
}
}

要在不同的文件或类中调用上述代码,请发行这些行(例如):

  LoggerWrapper loggerWrapper = LoggerWrapper.getInstance(); 
...
loggerWrapper.myLogger.log(Level.SEVERE,这里的一些文本+ e.fillInStackTrace());


解决方案

我假设这是在linux上运行?当您打开该文件时,它指向该文件的引用。当您删除该文件时,引用将被删除,但是由于您将引用保存在静态变量中,因此您的代码正在保存一个无关的文件句柄。相反,您应该执行如下操作:

  cat / dev / null>文件

哪些只会在文件上复制任何内容,而无需更改文件指向的实际inode。 / p>

I created the below wrapper for a singleton logger class so that I can log exceptions from any file and/or class. My inspiration comes from here: Logging to one file from multiple java files

I'm new to java and this is my first use of attempting to log exceptions to a file. While the following code does work, I noticed a few quirks I wanted to ask whether or not they were "normal behavior" or whether a workaround exists.

(1) Once the log file is created and has been written to with exceptions, if I edit the log file (e.g. remove some lines of text) then the log file is never written again from that moment forward. The web server needs to restart the domain associated with the web application before the log file is written to again. Is this normal? Ideally, I would like to retain only relevant errors in the log file, and delete the irrelevant ones, without needing to restart the web application's domain in the web server.

(2) If I delete the log file, then later an exception occurs in the java program(s), the log file does not get created again, and the exception is lost. Again, the web server needs to restart the domain associated with the web application before a new log file is created. Is this normal, or is there a workaround somehow by editing the below code? Ideally, I'd like to be able to delete the log file at any time and have the application create a new one.

Again, I'm new here so feel free to point out of the above intentions are bad design, etc.

------code follows------

Here's the file: LoggerWrapper.java

import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.logging.Logger;
import java.util.logging.Level;   
import java.util.logging.FileHandler;  
import java.util.logging.SimpleFormatter;

public class LoggerWrapper {
public static final Logger myLogger = Logger.getLogger("Test");
private static LoggerWrapper instance = null;  

 public static LoggerWrapper getInstance() {  
    if(instance == null) {  
        prepareLogger();  
        instance = new LoggerWrapper ();  
    }  
    return instance;  
 }  

private static void prepareLogger() {
try {
   FileHandler myFileHandler = new FileHandler("/path/to/myLogFile.log", true);  
   myFileHandler.setFormatter(new SimpleFormatter());  
   myLogger.addHandler(myFileHandler);  
   myLogger.setUseParentHandlers(false);  
   myLogger.setLevel(Level.ALL);
} catch (Exception e) {
   ...
}
}    
} 

To call the above code in a different file or class, issue these lines (for example):

LoggerWrapper loggerWrapper = LoggerWrapper.getInstance();
...
loggerWrapper.myLogger.log(Level.SEVERE, "some text here"+e.fillInStackTrace());

解决方案

I'm assuming this is running on linux? When you open the file it's pointing to a reference to that file. When you delete the file the reference is gone, but since you saved the reference in a static variable, your code is now holding a file handle that points to nothing. Instead you should do something like

cat /dev/null > file

Which will just copy nothing over the file without changing the actual inode that the file points to.

这篇关于Java:这种用于保存基本JDK日志文件的单例类的正确行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆