Java 自定义记录器:记录标准或/和最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1770442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java custom logger: logging standards or/and best practices
提问by Trick
I am developing a framework and I want the jar to be light weight and independent as much as it can be.
我正在开发一个框架,我希望 jar 尽可能轻巧且独立。
So I wrote a logging class:
所以我写了一个日志类:
import java.util.Date;
import java.util.Properties;
public class Logger {
private static final Logger me = new Logger();
private static boolean info = false;
private static boolean debug = false;
private static boolean error = false;
private static String className = null;
public static Logger getInstance(Class<?> clazz) {
className = clazz.getCanonicalName();
try {
Properties props = new CustProps().load(clazz);
if(props.get(CustProps.NAME_LOG_MODE) != null) {
String devMode = props.getProperty(CustProps.NAME_LOG_MODE)
.toLowerCase();
if("info".equals(devMode)) {
info = true;
debug = true;
} else if("debug".equals(devMode)) {
debug = true;
}
}
} catch (Exception e) {
// debug is error by default
}
error = true;
return me;
}
public void logError(Object msg) {
if(isError()) {
System.out.println(new Date().toString()
+ " ERROR ["+Logger.className+"] - " + msg);
}
}
public void logDebug(Object msg) {
if(isDebug()) {
System.out.println(new Date().toString()
+ " DEBUG ["+Logger.className+"] - " + msg);
}
}
public void logInfo(Object msg) {
if(isInfo()) {
System.out.println(new Date().toString()
+ " INFO ["+Logger.className+"] - " + msg);
}
}
public boolean isInfo() { return Logger.info; }
public boolean isDebug() { return Logger.debug; }
public boolean isError() { return Logger.error; }
}
- What are the best practices to make this logging better?
- Is making your own logger even worth it?
- Will the use of this logger make my framework worse than choosing something existing (like
log4j)?
- 使此日志记录更好的最佳实践是什么?
- 制作自己的记录器是否值得?
- 使用这个记录器会让我的框架比选择现有的东西(比如
log4j)更糟糕吗?
回答by Thorbj?rn Ravn Andersen
I would STRONGLY recommend that you use slf4j as your logging API, as it is designed to be able to switch backends at deployment time. In other words, if you ever outgrow your own logging framework or has to interface with others using something else, it is simple to change your mind.
我强烈建议您使用 slf4j 作为日志 API,因为它旨在能够在部署时切换后端。换句话说,如果您已经超出了自己的日志记录框架,或者不得不使用其他工具与其他人交互,那么改变主意很简单。
It also allows you to use the {}-construction for easy inserting objects in your log strings without overhead if that string is not actually logged anyway (which is really nice).
它还允许您使用 {} 结构轻松地在日志字符串中插入对象,而不会产生开销,如果该字符串实际上没有被记录(这真的很好)。
I'll suggest you consider adapting the "Simple" backend to your needs since it probably provides 90% of what you want.
我建议您考虑根据您的需要调整“简单”后端,因为它可能提供您想要的 90%。
http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html
http://www.slf4j.org/apidocs/org/slf4j/impl/SimpleLogger.html
Note: Do not use any backend directly (like log4j or java.util.logging) as it will essentially lock your code to that backend. Use a facade.
注意:不要直接使用任何后端(如 log4j 或 java.util.logging),因为它本质上会将您的代码锁定到该后端。使用立面。
回答by Dmitry
Don't you like java.util.logging.Logger? It is included into JDK, you don't need anything else.
你不喜欢 java.util.logging.Logger 吗?它包含在 JDK 中,您不需要其他任何东西。
回答by CPerkins
You ask three questions:
你问三个问题:
- What are best practices to make this logging better?
- 使此日志记录更好的最佳实践是什么?
As others have noted, the best practice would be to replace it entirely, either with something like log4j or slf4j, or, to meet your "independence" goal, simply with java.util.Logger.
正如其他人所指出的,最佳实践是完全替换它,或者使用 log4j 或 slf4j 之类的东西,或者,为了满足您的“独立”目标,只需使用 java.util.Logger。
- Is making your own logger even worth it?
- 制作自己的记录器是否值得?
No, except in very specialized circumstances, where for some reason, the existing frameworks don't meet your needs. You've said nothing to indicate that that might be the case.
不,除非在非常特殊的情况下,由于某种原因,现有框架不能满足您的需求。你没有说什么表明可能是这种情况。
- Will the use of this logger make my framework worse than choosing something existing (like log4j)?
- 使用这个记录器会让我的框架比选择现有的东西(比如 log4j)更糟糕吗?
Yes, if for no other reason than that it will make other maintainers spend a few minutes reading your logging class when they come into your codebase. Using an existing framework means they already know what it does.
是的,如果没有其他原因,它会让其他维护人员在进入您的代码库时花几分钟阅读您的日志记录类。使用现有框架意味着他们已经知道它的作用。
回答by Alberto Zaccagni
Use log4j. It is not a heavy weight logging framework, what do you mean by "independent as much as it can be"?
Just add the log4j jar to your framework and you are good to go.
使用 log4j。它不是一个重量级的日志框架,“尽可能独立”是什么意思?
只需将 log4j jar 添加到您的框架中,您就可以开始使用了。
回答by Kaleb Brasee
Nothing's more lightweight than using java.util.logging.Logger, since it's already available in the JRE.
没有什么比使用java.util.logging.Logger更轻量级的了,因为它已经在 JRE 中可用。
回答by djna
Why re-invent the wheel?
为什么要重新发明轮子?
If you use the Java util logging library then it's already in the JDK and so you have no extra code to provide.
如果您使用 Java util 日志库,那么它已经在 JDK 中,因此您无需提供额外的代码。
Otherwise, log4j is surely pretty good.
否则,log4j 肯定很不错。
回答by Armadillo
I would agree with the other comments - use a COTS tool (either Logger, Log4j or other). Writing and maintaining your own is (usually) not worth it.
我同意其他评论 - 使用 COTS 工具(Logger、Log4j 或其他)。编写和维护你自己的(通常)是不值得的。
One more point to consider: If your framework contains other 3rd party software that you'd like integrated into one log file, you'd have to use some shared tool (e.g. the one supported by that 3rd party software). It is often very helpful to have all pieces of the system consolidate logs into one place.
还有一点需要考虑:如果您的框架包含您希望集成到一个日志文件中的其他 3rd 方软件,则您必须使用一些共享工具(例如,该 3rd 方软件支持的工具)。让系统的所有部分将日志整合到一个地方通常非常有帮助。

