Using same log4j logger in standalone java application -


I have some code that is a standalone Java application that includes 30+ classes.

Most of these succession from any other base class.

This method is for every and every class to get and use a log4j logger

  public static logger getLogger () (if (logger! = Zero) returns Logger; try {propertiesUtil propUtil = PropertyUtil.getInstance ("app-log.properties"); if (propulable! = Null & amp; propipible.getProperties ()! = Null) PropertyConfigurator.configure (propUtil.getProperties ()) ; Logger = logger.getlogger (extractedata.class); return logger;} hold (exception exception) {exception.printStackTrace (); a} my question is whether it is some common law Should be re-applied, which is initially used and used in all classes? Is this a better practice?  

b) If so, how can this be done? is? How can I pass the logger?

C) It is not actually being used in code like Logger.debug () but not getLogger (). Debug () . What is the effect on display? In Log4J, you have a logarithm hierarchy, not a single logger, it is usually a square copy.

A logger is produced up to the square, where the loggers are identified by the classname. The logger is started like this:

  Private Static Last Logger Logger = Logger MetLogger (MyClass.class);  

This is a good practice in which it allows you to have good logging behavior in your app inside the module (or the package) or even for different classes. Therefore you can disable logging for some packages, others can log in at the information level, and for some important classes, log on to DEBUG level eg. When you want to catch a bug

B) However, if you want a single logger everywhere, just use the root logger in each class:

  Personal Static Last Logger Logger = Logger GetLogger ();  

C) There are not many complex methods for calls, the performance difference is probably negligible, because the JIT compiler will call the phone inline in any way. This is more of an open question for complex methods.

Note that the method you show works by doing loading by loading the logger configuration - this log is done automatically by 4J if you have your config file log4j.properties And keep it on the classpath. However, even if you need to use a nonstandard config file name, you can load the log-in configuration at any one place at startup, and erase the lazy loading of the logger. So what's left

  private static last logger logger = logger Tag (extractData.class); Private Static Logger Mill Logger () {Return Logar; }  

And it will definitely be outlined by the compiler.

Note that this is unnecessary for getLogger yes might be public , as all sections Anyway, it is considered for its own logar reference.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -