Dividere il log in base al livello di errore con log4j

Con log4j è possibile creare più file di log a seconda del livello di errore. Ecco un esempio:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
 
        <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
                <layout class="org.apache.log4j.PatternLayout">
                     <param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
                </layout>
        </appender>
 
        <appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
      <param name="File" value="C:\\logs\\app-debug.log"/>
      <param name="Append" value="true"/>
      <param name="MaxFileSize" value="500KB"/>
      <param name="MaxBackupIndex" value="2"/>
                <layout class="org.apache.log4j.PatternLayout">
                     <param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
                </layout>
                <filter class="org.apache.log4j.varia.LevelRangeFilter">
                        <param name="LevelMin" value="DEBUG" />
                        <param name="LevelMax" value="DEBUG" />
                </filter>
        </appender>
 
        <appender name="INFO" class="org.apache.log4j.RollingFileAppender">
      <param name="File" value="C:\\logs\\app-info.log"/>
      <param name="Append" value="true"/>
      <param name="MaxFileSize" value="500KB"/>
      <param name="MaxBackupIndex" value="2"/>
                <layout class="org.apache.log4j.PatternLayout">
                     <param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
                </layout>
                <filter class="org.apache.log4j.varia.LevelRangeFilter">
                        <param name="LevelMin" value="INFO" />
                        <param name="LevelMax" value="INFO" />
                </filter>
   </appender>
 
        <appender name="WARN" class="org.apache.log4j.RollingFileAppender">
      <param name="File" value="C:\\logs\\app-warn.log"/>
      <param name="Append" value="true"/>
      <param name="MaxFileSize" value="500KB"/>
      <param name="MaxBackupIndex" value="2"/>
                <layout class="org.apache.log4j.PatternLayout">
                     <param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
                </layout>
                <filter class="org.apache.log4j.varia.LevelRangeFilter">
                        <param name="LevelMin" value="WARN" />
                        <param name="LevelMax" value="WARN" />
                </filter>
   </appender>
 
        <appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
      <param name="File" value="C:\\logs\\app-error.log"/>
      <param name="Append" value="true"/>
      <param name="MaxFileSize" value="500KB"/>
      <param name="MaxBackupIndex" value="2"/>
                <layout class="org.apache.log4j.PatternLayout">
                     <param name="ConversionPattern" value="%c %d{ISO8601}
-- %p -- %m%n"/>
                </layout>
                <filter class="org.apache.log4j.varia.LevelRangeFilter">
                        <param name="LevelMin" value="ERROR" />
                        <param name="LevelMax" value="ERROR" />
                </filter>
   </appender>
 
        <root>
                <priority value="debug"/>
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="DEBUG"/>
        <appender-ref ref="INFO"/>
        <appender-ref ref="WARN"/>
        <appender-ref ref="ERROR"/>
        </root>
 
</log4j:configuration>

Fonte: mail-archive

Fletto i muscoli e sono nel vuoto.

Leave a Reply