I read some articles about how to use log4j. Most of them give below code as a beginning:
我读了一些关于如何使用log4j的文章。他们中的大多数以下面的代码作为开头:
Logger logger = Logger.getLogger("com.foo.Bar");
or
要么
Logger logger = Logger.getLogger(XXX.class);
This will initialize the logger object.But my question is why need send the class type as a parameter? It seems when I use the logger, I don't care in which class I use it.So the Class type seems no effect to logger. If I declare a logger as static and public, I can call this logger at another class, So what's the intention of the author to design it like this? Will the Class type bind something when I use the logger? Or I can send any Class types to the getLogger function.
这将初始化记录器对象。但我的问题是为什么需要将类类型作为参数发送?看来当我使用记录器时,我并不关心我在哪个类中使用它。所以类型似乎对记录器没有影响。如果我将一个logger声明为static和public,我可以在另一个类中调用这个logger,那么作者有意这样设计它吗?当我使用记录器时,Class类型会绑定一些东西吗?或者我可以将任何类类型发送到getLogger函数。
6 个解决方案
#1
51
-
You can always use any string as logger name other than class type. It's definitely ok.
您始终可以使用任何字符串作为类型以外的记录器名称。这绝对可以。
-
The reason why many people use class type, I guess:
很多人之所以使用类型,我猜:
-
Easy to use. You don't need to worry about logger name duplication in a complex Java EE application. If other people also use your logger name, you may have a log file including no only the output of your class;
使用方便。您无需担心复杂Java EE应用程序中的记录器名称重复。如果其他人也使用您的记录器名称,您可能会有一个日志文件,不仅包括您的类的输出;
-
Easy to check the logging class, as the logger name will show in the log file. You can quickly navigate to the specific class;
易于检查日志记录类,因为记录器名称将显示在日志文件中。您可以快速导航到特定的类;
-
When you distribute you class, people may want to redirect the logging from your class to a specific file or somewhere else. In such case, if you use a special logger name, we may need to check the source code or imposssible to do that if souce is unavailable.
当您分发类时,人们可能希望将日志从您的类重定向到特定文件或其他位置。在这种情况下,如果您使用特殊的记录器名称,我们可能需要检查源代码,或者如果源不可用则无法执行此操作。
-
#2
6
From the javadoc: Logger.getLogger(Class)
is a shorthand for getLogger(clazz.getName())
. A convention used with log4j and other logging frameworks is to define a static logger per class. For example,
来自javadoc:Logger.getLogger(Class)是getLogger(clazz.getName())的简写。与log4j和其他日志框架一起使用的约定是为每个类定义一个静态记录器。例如,
public class SomeClass {
private static final Logger LOG = Logger.getLogger(SomeClass.class);
...
}
I have found this convention to work well for organizing logging output. It's certainly not required but is a useful practice.
我发现这个约定适用于组织日志输出。这当然不是必需的,但是是一种有用的做法。
#3
6
1:you can use "class name" or "string name" when you define in log4j.properties before, such as
1:您可以在之前在log4j.properties中定义时使用“类名”或“字符串名称”,例如
log4j.logger.anything=INFO,anything
so,you can record your log as
所以,你可以记录你的日志
Logger logger = Logger.getLogger("anything");
2:If you define some log name,you can check it easily,cus they are separate.
2:如果您定义了一些日志名称,您可以轻松地进行检查,因为它们是分开的。
#4
0
XXX.class is to Name your Logger i.e to flag subsequent log statements . To give you an idea which class certain log statements belongs to / originated from.
XXX.class是命名您的记录器,即标记后续日志语句。让您了解某些日志语句属于/来自哪个类。
#5
0
Logger with class name is not mandatory, you can use your own message. It is convention to use:
具有类名的记录器不是必需的,您可以使用自己的消息。惯例使用:
Logger logger = Logger.getLogger(XXX.class)
and is useful to debug. It will log which line of code is executed.
并且对调试很有用。它将记录执行哪行代码。
#6
-1
You can trace your log by class type.
您可以按类类型跟踪日志。
example1:
例1:
public class Bar {
Logger logger = Logger.getLogger("com.foo.Bar");
...
logger.debug("debug message");
}
Maybe you can see below a log message.
也许你可以在下面看到一条日志消息。
DEBUG: **com.foo.Bar** debug message
example2:
例2:
public class Foo {
Logger logger = Logger.getLogger("com.foo.Foo");
...
logger.debug("debug message");
}
Maybe you can see below a log message.
也许你可以在下面看到一条日志消息。
DEBUG: **com.foo.Foo** debug message
If you have a lot of java class and logger message, It's too difficult to find where log messages are from.
如果你有很多java类和logger消息,那么很难找到日志消息的来源。
#1
51
-
You can always use any string as logger name other than class type. It's definitely ok.
您始终可以使用任何字符串作为类型以外的记录器名称。这绝对可以。
-
The reason why many people use class type, I guess:
很多人之所以使用类型,我猜:
-
Easy to use. You don't need to worry about logger name duplication in a complex Java EE application. If other people also use your logger name, you may have a log file including no only the output of your class;
使用方便。您无需担心复杂Java EE应用程序中的记录器名称重复。如果其他人也使用您的记录器名称,您可能会有一个日志文件,不仅包括您的类的输出;
-
Easy to check the logging class, as the logger name will show in the log file. You can quickly navigate to the specific class;
易于检查日志记录类,因为记录器名称将显示在日志文件中。您可以快速导航到特定的类;
-
When you distribute you class, people may want to redirect the logging from your class to a specific file or somewhere else. In such case, if you use a special logger name, we may need to check the source code or imposssible to do that if souce is unavailable.
当您分发类时,人们可能希望将日志从您的类重定向到特定文件或其他位置。在这种情况下,如果您使用特殊的记录器名称,我们可能需要检查源代码,或者如果源不可用则无法执行此操作。
-
#2
6
From the javadoc: Logger.getLogger(Class)
is a shorthand for getLogger(clazz.getName())
. A convention used with log4j and other logging frameworks is to define a static logger per class. For example,
来自javadoc:Logger.getLogger(Class)是getLogger(clazz.getName())的简写。与log4j和其他日志框架一起使用的约定是为每个类定义一个静态记录器。例如,
public class SomeClass {
private static final Logger LOG = Logger.getLogger(SomeClass.class);
...
}
I have found this convention to work well for organizing logging output. It's certainly not required but is a useful practice.
我发现这个约定适用于组织日志输出。这当然不是必需的,但是是一种有用的做法。
#3
6
1:you can use "class name" or "string name" when you define in log4j.properties before, such as
1:您可以在之前在log4j.properties中定义时使用“类名”或“字符串名称”,例如
log4j.logger.anything=INFO,anything
so,you can record your log as
所以,你可以记录你的日志
Logger logger = Logger.getLogger("anything");
2:If you define some log name,you can check it easily,cus they are separate.
2:如果您定义了一些日志名称,您可以轻松地进行检查,因为它们是分开的。
#4
0
XXX.class is to Name your Logger i.e to flag subsequent log statements . To give you an idea which class certain log statements belongs to / originated from.
XXX.class是命名您的记录器,即标记后续日志语句。让您了解某些日志语句属于/来自哪个类。
#5
0
Logger with class name is not mandatory, you can use your own message. It is convention to use:
具有类名的记录器不是必需的,您可以使用自己的消息。惯例使用:
Logger logger = Logger.getLogger(XXX.class)
and is useful to debug. It will log which line of code is executed.
并且对调试很有用。它将记录执行哪行代码。
#6
-1
You can trace your log by class type.
您可以按类类型跟踪日志。
example1:
例1:
public class Bar {
Logger logger = Logger.getLogger("com.foo.Bar");
...
logger.debug("debug message");
}
Maybe you can see below a log message.
也许你可以在下面看到一条日志消息。
DEBUG: **com.foo.Bar** debug message
example2:
例2:
public class Foo {
Logger logger = Logger.getLogger("com.foo.Foo");
...
logger.debug("debug message");
}
Maybe you can see below a log message.
也许你可以在下面看到一条日志消息。
DEBUG: **com.foo.Foo** debug message
If you have a lot of java class and logger message, It's too difficult to find where log messages are from.
如果你有很多java类和logger消息,那么很难找到日志消息的来源。