How to use java.util.logger in a web application?

I am trying to use the registrar through a web application. I added FileHandler to write the log to the file. Now I need to use the same handler for other classes / servlets in the project so that the logs from all classes are written to the same text file. How can I achieve this?

/*** * Initialize a logger */ public static Logger logger; static { try { FileHandler fh = new FileHandler("log.txt", true); fh.setFormatter(new SimpleFormatter()); logger = Logger.getLogger(MyClass.class.getName()); logger.addHandler(fh); } catch (IOException e) { e.printStackTrace(); } } 

Do I need to initialize the registrar and add a handler in each class, as in the above code? Any other methods?

+4
source share
3 answers

I would consider using a logging framework such as Log4J.

Using it just comes down to setting up additives (for example, FileAppender) and log levels in the central file (.xml or .properties) and in each class that the logger you just made should define Log l = LogFactory.getLog(clazz); (where clazz is the class you define for the registrar).

You can make a public static log and use it for other classes as well, but I would not recommend it, since you usually want to know which registrar (i.e. which class for which the log was defined) created a journal entry .

+3
source

You can use the logging.properties file to define your handlers around the world for the entire application. In this file you can fine-tune your registration needs.

Have a look here or just google for .properties logging.

An example from the above link:

 handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler java.util.logging.ConsoleHandler.level = INFO java.util.logging.FileHandler.level = ALL java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter 

You can even set up different logging rules for each of your web applications by placing logging.properties in the WEB-INF / classes of your web application.

+3
source
 package package_name; import java.io.IOException; import java.util.Properties; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class Log { public Log() { Properties props = new Properties(); try { props.load(getClass().getResourceAsStream("/log4j.properties")); } catch (IOException e) { e.printStackTrace(); } PropertyConfigurator.configure(props);//PropertyConfigurator.configure("log4j.properties"); } public Logger getLogger(Object obj) { Logger logger = Logger.getLogger(Object.class); return logger; } 

}

then we must support the log4j.properties file in one of our packages, and the file should be as follows:

log4j.properties

 log4j.rootLogger=DEBUG, R,CA log4j.appender.R = org.apache.log4j.DailyRollingFileAppender log4j.appender.R.File = c:\\our project name+LOGSLIVE\\logs\\project short name.log log4j.appender.R.Append = true log4j.appender.R.DatePattern = '_'yyyy-MM-dd'.log' log4j.appender.R.layout = org.apache.log4j.PatternLayout #log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} [%t] [%p] %m%n log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %5p [%t] (%F:%L) - %m%n #Console Appender log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout log4j.appender.CA.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n 
+3
source

All Articles