Which is the best way to declare logger variable i

2019-06-15 21:01发布

I just wantd to know which is the best way declare logger variable in java. Following are some declaration.

1> private static final Logger logger = Logger.getLogger(ServiceImpl.class);

2> private static Logger logger = Logger.getLogger(ServiceImpl.class);

3> private static final Logger LOGGER= Logger.getLogger(ServiceImpl.class);

4> private static Logger LOGGER= Logger.getLogger(ServiceImpl.class);

P.S I really appreciate if anybody knows another best alternative ways to declare looger variable.

标签: java logging
9条回答
ゆ 、 Hurt°
2楼-- · 2019-06-15 21:45

Nobody here uses LOG or log? I have found it to be nicer in practice. (Surely I'm not the first one to work on a place where that's the standard, because @Log4j in Lombok generates a log field as well. Of course it's also static final. And BTW, that's the best way to declare that field... add a @Log4j annotation. Done.)

查看更多
Fickle 薄情
3楼-- · 2019-06-15 21:47

It is good programming practice to share a single logger object between all of the instances of a particular class and to use the same logger for the duration of the program, so we use static and final for loggers.

It is not recommended to use multiple loggers (poor logging practice) rather than logging levels.

I think that

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

is the better option.

查看更多
可以哭但决不认输i
4楼-- · 2019-06-15 21:49

All upper-case variable names are IMO out because you really aren't declaring/defining a constant but a static variable. Uppercase names are more suitable for "constants". That said, I'd personally go with the first approach.

private static final Logger logger = Logger.getLogger(ServiceImpl.class);
查看更多
登录 后发表回答