在Java中,静态final变量是常量,按照惯例它们应该是大写的。然而,我看到大多数人都用小写来声明记录器,这在PMD中是一种违反。

e.g:

private static final Logger logger = Logger.getLogger(MyClass.class);

只要搜索谷歌或“静态最终记录器”,你会看到这自己。

我们应该使用LOGGER吗?


当前回答

我更喜欢“logger”,即小写。原因不在于它是常量还是非常量(可变的还是不可变的)。如果我们使用这个推理,那么如果我们改变了日志框架(或者框架改变了日志记录器的可变性),我们就必须重命名变量。

对我来说,其他原因更重要。

A logger is a shadow object in the class and should not be very prominent as it does not implement the main logic. If we use 'LOGGER', it's an eye catcher in the code that attracts too much attention. Sometimes loggers are declared at instance level (i.e. not as static), and even are injected as a dependency. I wouldn't like to change my code if I decide to change the way I obtain the logger. The code stability wrt. this (hypothetical in many cases) change is the other reason why I prefer the lower case.

其他回答

记录器引用不是一个常量,而是一个最终引用,不应该是大写的。常量值应该是大写的。

private static final Logger logger = Logger.getLogger(MyClass.class);

private static final double MY_CONSTANT = 0.0;

别忘了,PMD会尊重评论

// NOPMD

在里面。这将导致PMD跳过它的检查行,这将允许你选择任何你想要的样式。

我更喜欢“logger”,即小写。原因不在于它是常量还是非常量(可变的还是不可变的)。如果我们使用这个推理,那么如果我们改变了日志框架(或者框架改变了日志记录器的可变性),我们就必须重命名变量。

对我来说,其他原因更重要。

A logger is a shadow object in the class and should not be very prominent as it does not implement the main logic. If we use 'LOGGER', it's an eye catcher in the code that attracts too much attention. Sometimes loggers are declared at instance level (i.e. not as static), and even are injected as a dependency. I wouldn't like to change my code if I decide to change the way I obtain the logger. The code stability wrt. this (hypothetical in many cases) change is the other reason why I prefer the lower case.

我喜欢谷歌的做法(谷歌Java风格)

每个常量都是一个静态最终字段,但并不是所有的静态最终字段都是常量。在选择常数情况之前,考虑场是否真的像常数。例如,如果该实例的任何可观察状态可以改变,那么它几乎肯定不是常量。仅仅打算永远不改变对象通常是不够的。

例子:

// Constants
static final int NUMBER = 5;
static final ImmutableList<String> NAMES = ImmutableList.of("Ed", "Ann");
static final Joiner COMMA_JOINER = Joiner.on(',');  // because Joiner is immutable
static final SomeMutableType[] EMPTY_ARRAY = {};
enum SomeEnum { ENUM_CONSTANT }

// Not constants
static String nonFinal = "non-final";
final String nonStatic = "non-static";
static final Set<String> mutableCollection = new HashSet<String>();
static final ImmutableSet<SomeMutableType> mutableElements = ImmutableSet.of(mutable);
static final Logger logger = Logger.getLogger(MyClass.getName());
static final String[] nonEmptyArray = {"these", "can", "change"};

从有效的java,第二版。

The sole exception to the previous rule concerns “constant fields,” whose names should consist of one or more uppercase words separated by the underscore character, for example, VALUES or NEGATIVE_INFINITY. A constant field is a static final field whose value is immutable. If a static final field has a primitive type or an immutable reference type (Item 15), then it is a constant field. For example, enum constants are constant fields. If a static final field has a mutable reference type, it can still be a constant field if the referenced object is immutable.

总之,常量==静态final,如果它是引用(相对于简单类型),则加上不可变性。

查看slf4j记录器, http://www.slf4j.org/api/org/slf4j/Logger.html

它是不可变的。另一方面,JUL记录器是可变的。log4j日志记录器也是可变的。因此,为了正确起见,如果您正在使用log4j或JUL,它应该是“logger”,如果您正在使用slf4j,它应该是logger。

请注意,上面链接的slf4j javadocs页面有一个使用“logger”而不是“logger”的示例。

当然,这些只是惯例,不是规则。如果您碰巧正在使用slf4j,并且您想要使用“logger”,因为您已经习惯了其他框架中的“logger”,或者如果它更容易输入,或者为了可读性,那么请继续使用。