Initialize logger by java
This task use java to initialize logger.
Initialize logger
- Create /cfg/log-conf.xml as following
- Create initLog method as following
- At beginning of program, call initLog method
/cfg/log-conf.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration> <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="log/bruchia.log" /> <param name="Append" value="true" /> <param name="MaxFileSize" value="1024KB" /> <param name="MaxBackupIndex" value="10240" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%t] %C{2} %M (%F:%L) - %m%n"/> </layout> </appender> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p [%t] %C{2} %M (%F:%L) - %m%n"/> </layout> </appender> <category name="org.apache.log4j.xml"> <priority value="warn" /> <appender-ref ref="fileAppender" /> <appender-ref ref="STDOUT" /> </category> <logger name="org.apache" > <level value ="warn" /> </logger> <root> <priority value ="info" /> <appender-ref ref="fileAppender" /> <appender-ref ref="STDOUT" /> </root> </log4j:configuration>initLog method
private static void initLog() { try { String configDir = new File(System.getProperty("user.dir"), "cfg").getAbsolutePath(); String logConfigFile = new File(configDir, "log-conf.xml").getAbsolutePath(); String logDir = new File(System.getProperty("user.dir"), "log").getAbsolutePath(); String stdoutLogFile = new File(logDir, "stdout.log").getAbsolutePath(); DOMConfigurator.configure(logConfigFile); System.setErr(new PrintStream(new BufferedOutputStream(new FileOutputStream(stdoutLogFile)), true)); System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(stdoutLogFile)), true)); } catch (Exception e) { e.printStackTrace(); } }
No comments:
Post a Comment