Monday, 26 December 2011

Initialize logger by java

Initialize logger by java
This task use java to initialize logger.
Initialize logger
  1. Create /cfg/log-conf.xml as following
  2. Create initLog method as following
  3. At beginning of program, call initLog method
/cfg/log-conf.xml
1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
3<log4j:configuration>
4 <appender name="fileAppender" class="org.apache.log4j.RollingFileAppender">
5 <param name="File" value="log/bruchia.log" />
6 <param name="Append" value="true" />
7 <param name="MaxFileSize" value="1024KB" />
8 <param name="MaxBackupIndex" value="10240" />
9 <layout class="org.apache.log4j.PatternLayout">
10 <param name="ConversionPattern"
11 value="%d %-5p [%t] %C{2} %M (%F:%L) - %m%n"/>
12 </layout>
13 </appender>
14 <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
15 <layout class="org.apache.log4j.PatternLayout">
16 <param name="ConversionPattern"
17 value="%d %-5p [%t] %C{2} %M (%F:%L) - %m%n"/>
18 </layout>
19 </appender>
20
21 <category name="org.apache.log4j.xml">
22 <priority value="warn" />
23 <appender-ref ref="fileAppender" />
24 <appender-ref ref="STDOUT" />
25 </category>
26
27 <logger name="org.apache" >
28 <level value ="warn" />
29 </logger>
30
31 <root>
32 <priority value ="info" />
33 <appender-ref ref="fileAppender" />
34 <appender-ref ref="STDOUT" />
35 </root>
36</log4j:configuration>
<?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
1private static void initLog() {
2 try {
3 String configDir = new File(System.getProperty("user.dir"), "cfg").getAbsolutePath();
4 String logConfigFile = new File(configDir, "log-conf.xml").getAbsolutePath();
5 String logDir = new File(System.getProperty("user.dir"), "log").getAbsolutePath();
6 String stdoutLogFile = new File(logDir, "stdout.log").getAbsolutePath();
7
8 DOMConfigurator.configure(logConfigFile);
9 System.setErr(new PrintStream(new BufferedOutputStream(new FileOutputStream(stdoutLogFile)), true));
10 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(stdoutLogFile)), true));
11 } catch (Exception e) {
12 e.printStackTrace();
13 }
14}
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();
    }
}

  Protected by Copyscape Online Copyright Protection

No comments:

Post a Comment