View | Details | Raw Unified | Return to bug 65153
Collapse All | Expand All

(-)a/src/documentation/content/xdocs/components/logging.xml (-55 / +97 lines)
Lines 24-29 Link Here
24
        <title>Apache POI - Logging Framework</title>
24
        <title>Apache POI - Logging Framework</title>
25
        <authors>
25
        <authors>
26
            <person id="DS" name="Dominik Stadler" email="centic@apache.org"/>
26
            <person id="DS" name="Dominik Stadler" email="centic@apache.org"/>
27
            <person id="MV" name="Marius Volkhart" email="marius@volkhart.com"/>
27
        </authors>
28
        </authors>
28
    </header>
29
    </header>
29
30
Lines 31-60 Link Here
31
        <section>
32
        <section>
32
            <title>Introduction</title>
33
            <title>Introduction</title>
33
            <p>
34
            <p>
34
                POI uses a custom logging framework which allows to configure where logs are sent to.
35
                POI uses the <a href="">Log4j 2</a> logging API.
35
            </p>
36
            </p>
36
            <p>
37
            <p>
37
                Logging in POI is used only as a debugging mechanism, not a normal runtime
38
                Logging in POI is used primarily as a debugging mechanism, not a normal runtime
38
                logging system.  Logging on level debug/info is ONLY for autopsie type debugging, and should
39
                logging system.  Logging at levels noisier than WARN is ONLY for autopsy type debugging, and should
39
                NEVER be enabled on a production system.
40
                NEVER be enabled on a production system.
40
            </p>
41
            </p>
41
            <p>
42
                The framework is extensible so that you can send log messages to any logging framework
43
                that your application uses.
44
            </p>
45
            <p>
46
                A number of default logging implementations are supported by POI out-of-the-box and can be selected via a
47
                system property.
48
            </p>
49
        </section>
42
        </section>
50
        <section><title>Enable logging</title>
43
        <section><title>General logging behavior</title>
51
        <p>
44
        <p>
52
            By default, logging is disabled in POI. Sometimes it might be useful
45
            POI tries to name loggers after the canonical name of the containing class. For example,
53
            to enable logging to see some debug messages printed out which can
46
            <code>org.apache.poi.poifs.filesystem.POIFSFileSystem</code>. Use your logging framework's typical mechanisms
54
            help in analyzing problems.
47
            for activating and deactivating logging for specific loggers.
55
        </p>
48
        </p>
56
        <p>
49
        <p>
57
            You can select the logging framework by setting the system property <em>org.apache.poi.util.POILogger</em> during application startup or by calling System.setProperty():
50
            All loggers are named <code>com.apache.poi.*</code>, so rules applied to <code>com.apache.poi</code> will
51
            affect all POI loggers.
58
        </p>
52
        </p>
59
        <source>
53
        <source>
60
            System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.CommonsLogger" );
54
            System.setProperty("org.apache.poi.util.POILogger", "org.apache.poi.util.CommonsLogger" );
Lines 63-115 Link Here
63
            Note: You need to call <em>setProperty()</em> before any POI functionality is invoked as the logger is only initialized during startup.
57
            Note: You need to call <em>setProperty()</em> before any POI functionality is invoked as the logger is only initialized during startup.
64
        </p>
58
        </p>
65
        </section>
59
        </section>
66
        <section><title>Available logger implementations</title>
60
        <section><title>Log4j 2 Core Examples</title>
67
            <p>
61
            <p>
68
                The following logger implementations are provided by POI:
62
                Capturing POI logs using Log4j 2 Core is as simple as including the
63
                <a href="https://logging.apache.org/log4j/2.x/maven-artifacts.html"><code>log4j-core</code></a> JAR in
64
                your project.
69
            </p>
65
            </p>
70
            <table>
71
            <tr>
72
                <th>Class</th>
73
                <th>Type</th>
74
            </tr>
75
            <tr>
76
                <td>org.apache.poi.util.SystemOutLogger</td>
77
                <td>Sends log output to the system console</td>
78
            </tr>
79
            <tr>
80
                <td>org.apache.poi.util.NullLogger</td>
81
                <td>Default logger, does not log anything</td>
82
            </tr>
83
            <tr>
84
                <td>org.apache.poi.util.CommonsLogger</td>
85
                <td>Allows to use <a href="https://commons.apache.org/proper/commons-logging/">Apache Commons Logging</a> for logging. This can use JDK1.4 logging,
86
                    log4j, logkit, etc. The log4j dependency was removed in POI 5.0.0, so you will need to include this dependency yourself if you need it.</td>
87
            </tr>
88
            <tr>
89
                <td>org.apache.poi.util.DummyPOILogger</td>
90
                <td>Simple logger which will keep all log-lines in memory for later analysis (this class is not in the jar, just in the test source).
91
                    Used primarily for testing. Note: this may cause a memory leak if used in production application!</td>
92
            </tr>
93
            </table>
94
        </section>
95
        <section><title>Sending logs to a different log framework</title>
96
            <p>
66
            <p>
97
                You can send logs to other logging frameworks by implementing the interface <em>org.apache.poi.util.POILogger</em>.
67
                The simplest configuration is to capture all POI logs at the same level as your application. You might
68
                want to collect all messages <code>INFO</code> and higher, and are OK with capturing POI messages as well.
69
            </p>
70
            <source>
71
                &lt;Configuration ...&gt;
72
                    &lt;Loggers&gt;
73
                        &lt;Root level="INFO"&gt;
74
                            ...
75
                        &lt;/Root&gt;
76
                    &lt;/Loggers&gt;
77
                &lt;/Configuration&gt;
78
            </source>
79
80
            <p>
81
                A more recommended configuration is to capture only messages from loggers you opt in to. For example,
82
                you might want to capture all messages from <code>com.example.myapplication</code> at <code>INFO</code>
83
                but only POI messages at <code>WARN</code> or more severe.
98
            </p>
84
            </p>
85
            <source>
86
                &lt;Configuration ...&gt;
87
                    &lt;Loggers&gt;
88
                        &lt;Logger name="com.example.myapplication" level="INFO" /&gt;
89
                        &lt;Logger name="org.apache.poi" level="WARN" /&gt;
90
91
                        &lt;Root level="OFF"&gt;
92
                            ...
93
                        &lt;/Root&gt;
94
                    &lt;/Loggers&gt;
95
                &lt;/Configuration&gt;
96
            </source>
97
98
            <p>Another strategy you may decide to use is to capture all messages except those coming from POI.</p>
99
            <source>
100
                &lt;Configuration ...&gt;
101
                    &lt;Loggers&gt;
102
                        &lt;Logger name="org.apache.poi" level="OFF" /&gt;
103
104
                        &lt;Root level="INFO"&gt;
105
                            ...
106
                        &lt;/Root&gt;
107
                    &lt;/Loggers&gt;
108
                &lt;/Configuration&gt;
109
            </source>
99
        </section>
110
        </section>
100
        <section><title>Implementation details</title>
111
        <section><title>Logback Examples</title>
101
            <p>
112
            <p>
102
                Every class uses a <code>POILogger</code> to log, and gets it using a static method
113
                Capturing POI logs using Logback requires adding the
103
                of the <code>POILogFactory</code> .
114
                <a href="https://logging.apache.org/log4j/2.x/log4j-to-slf4j/index.html">Log4j to SLF4J Adapter</a> to
115
                your project, along with the standard Logback dependencies.
104
            </p>
116
            </p>
105
            <p>
117
            <p>
106
                Each class in POI can log using a <code>POILogger</code>, which is an abstract class.
118
                The simplest configuration is to capture all POI logs at the same level as your application. You might
107
                We decided to make our own logging facade because:</p>
119
                want to collect all messages <code>INFO</code> and higher, and are OK with capturing POI messages as well.
108
            <ol>
120
            </p>
109
                <li>we need to log many values and we put many methods in this class to facilitate the
121
            <source>
110
                    programmer, without having him write string concatenations;</li>
122
                &lt;configuration ...&gt;
111
                <li>we need to be able to use POI without any logger package present.</li>
123
                    &lt;root level="INFO"&gt;
112
            </ol>
124
                        ...
125
                    &lt;/root&gt;
126
                &lt;/configuration&gt;
127
            </source>
128
129
            <p>
130
                A more recommended configuration is to capture only messages from loggers you opt in to. For example,
131
                you might want to capture all messages from <code>com.example.myapplication</code> at <code>INFO</code>
132
                but only POI messages at <code>WARN</code> or more severe.
133
            </p>
134
            <source>
135
                &lt;configuration ...&gt;
136
                    &lt;logger name="com.example.myapplication" level="INFO" /&gt;
137
                    &lt;logger name="org.apache.poi" level="WARN" /&gt;
138
139
                    &lt;root level="OFF"&gt;
140
                        ...
141
                    &lt;/root&gt;
142
                &lt;/configuration&gt;
143
            </source>
144
145
            <p>Another strategy you may decide to use is to capture all messages except those coming from POI.</p>
146
            <source>
147
                &lt;configuration ...&gt;
148
                    &lt;logger name="org.apache.poi" level="OFF" /&gt;
149
150
                    &lt;root level="INFO"&gt;
151
                        ...
152
                    &lt;/root&gt;
153
                &lt;/configuration&gt;
154
            </source>
113
        </section>
155
        </section>
114
    </body>
156
    </body>
115
    <footer>
157
    <footer>

Return to bug 65153