java在其他类中调用Logger类 [英] java calling Logger class in other classes
问题描述
我用静态方法编写了一个单独的类MyLogger
I wrote a separate class MyLogger with static method
static public void setup(String className, Exception e, Level level) {
System.out.println("className = " + className);
Logger logger = Logger.getLogger(className);
logger.setLevel(Level.INFO);
try {
fileTxt = new FileHandler("Logging.%u.%g.txt",1024 * 1024, 10, true );
// create a TXT formatter
formatterTxt = new SimpleFormatter();
fileTxt.setFormatter(formatterTxt);
logger.addHandler(fileTxt);
logger.log(level, e.toString(), e);
} catch (IOException | SecurityException ex) {
System.out.println("SecurityException ");
Logger logger2=Logger.getLogger(MyLogger.class.getName());
logger2.addHandler(new ConsoleHandler());
logger2.log(Level.SEVERE, null, ex);
}
}
此静态方法接收一个字符串className,我将它作为参数放入Logger.getLogger().
This static method receives a string className which I put as a parameter into Logger.getLogger().
在另一个类中,我称为MyLogger.setup(CommonFrame.class.getName(),例如Level.SEVERE). 一切正常.但是问题是,在文件中我得到"2014年5月25日下午2:05:30 javagui.MyLogger设置",我认为应该像这样"2014年5月25日下午2:05:30 javagui.CommonFrame ",因为我已将该名称分配给记录器. 我对吗?如果是,该如何解决?
In another class I call MyLogger.setup(CommonFrame.class.getName(), e, Level.SEVERE). Everything works. But the problem is that in the file I get "May 25, 2014 2:05:30 PM javagui.MyLogger setup" and I thought that it should be instead like this "May 25, 2014 2:05:30 PM javagui.CommonFrame" because I assigned that name to the logger. Am I right? If yes, how can I fix it?
推荐答案
您已使用 format 属性/api/java/util/logging/SimpleFormatter.html"rel =" nofollow> java.util.logging.SimpleFormatter 以使用源类名.
You have use the format property of the java.util.logging.SimpleFormatter to use the logger name instead of the source classname.
修改启动脚本以设置以下系统属性:
Modify your launch script to set the following system property:
-Djava.util.logging.SimpleFormatter.format="%1$tc %3$s%n%4$s: %5$s%6$s%n"
或修改您的logging.properties以包括:
Or modify your logging.properties to include:
java.util.logging.SimpleFormatter.format=%1$tc %3$s%n%4$s: %5$s%6$s%n
否则,您可以创建自定义格式化程序并使用它提取信息您要在日志文件中看到的.
Otherwise, you can create a custom formatter and use that extract the information you want to see in the log file.
这篇关于java在其他类中调用Logger类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!