File: UserDataHelper.java

package info (click to toggle)
tomcat7 7.0.56-3%2Bdeb8u11
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 35,688 kB
  • ctags: 41,823
  • sloc: java: 249,464; xml: 51,553; jsp: 3,037; sh: 1,361; perl: 269; makefile: 195
file content (161 lines) | stat: -rw-r--r-- 5,076 bytes parent folder | download | duplicates (5)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tomcat.util.log;

import org.apache.juli.logging.Log;

/**
 * This helper class assists with the logging associated with invalid input
 * data. A developer may want all instances of invalid input data logged to
 * assist with debugging whereas in production it is likely to be desirable not
 * to log anything for invalid data. The following settings may be used:
 * <ul>
 * <li>NOTHING: Log nothing.</li>
 * <li>DEBUG_ALL: Log all problems at DEBUG log level.</li>
 * <li>INFO_THEN_DEBUG: Log first problem at INFO log level and any further
 *     issues in the following TBD (configurable) seconds at DEBUG level</li>
 * <li>INFO_ALL: Log all problems at INFO log level.</li>
 * </ul>
 * By default, INFO_THEN_DEBUG is used with a suppression time of 24 hours.
 *
 * NOTE: This class is not completely thread-safe. When using INFO_THEN_DEBUG it
 * is possible that several INFO messages will be logged before dropping to
 * DEBUG.
 */
public class UserDataHelper {

    private final Log log;

    private final Config config;

    // A value of 0 is equivalent to using INFO_ALL
    // A negative value will trigger infinite suppression
    // The value is milliseconds
    private final long suppressionTime;

    private volatile long lastInfoTime = 0;


    public UserDataHelper(Log log) {
        this.log = log;

        Config tempConfig;
        String configString = System.getProperty(
                "org.apache.juli.logging.UserDataHelper.CONFIG");
        if (configString == null) {
            tempConfig = Config.INFO_THEN_DEBUG;
        } else {
            try {
                tempConfig = Config.valueOf(configString);
            } catch (IllegalArgumentException iae) {
                // Ignore - use default
                tempConfig = Config.INFO_THEN_DEBUG;
            }
        }

        // Default suppression time of 1 day.
        suppressionTime = Integer.getInteger(
                "org.apache.juli.logging.UserDataHelper.SUPPRESSION_TIME",
                60 * 60 * 24).intValue() * 1000L;

        if (suppressionTime == 0) {
            tempConfig = Config.INFO_ALL;
        }

        config = tempConfig;
    }


    /**
     * Returns log mode for the next log message, or <code>null</code> if the
     * message should not be logged.
     *
     * <p>
     * If <code>INFO_THEN_DEBUG</code> configuration option is enabled, this
     * method might change internal state of this object.
     *
     * @return Log mode, or <code>null</code>
     */
    public Mode getNextMode() {
        if (Config.NONE == config) {
            return null;
        } else if (Config.DEBUG_ALL == config) {
            return log.isDebugEnabled() ? Mode.DEBUG : null;
        } else if (Config.INFO_THEN_DEBUG == config) {
            if (logAtInfo()) {
                return log.isInfoEnabled() ? Mode.INFO_THEN_DEBUG : null;
            } else {
                return log.isDebugEnabled() ? Mode.DEBUG : null;
            }
        } else if (Config.INFO_ALL == config) {
            return log.isInfoEnabled() ? Mode.INFO : null;
        }
        // Should never happen
        return null;
    }


    /*
     * Not completely thread-safe but good enough for this use case. I couldn't
     * see a simple enough way to make it completely thread-safe that was not
     * likely to compromise performance.
     */
    private boolean logAtInfo() {

        if (suppressionTime < 0 && lastInfoTime > 0) {
            return false;
        }

        long now = System.currentTimeMillis();

        if (lastInfoTime + suppressionTime > now) {
            return false;
        }

        lastInfoTime = now;
        return true;
    }


    private static enum Config {
        NONE,
        DEBUG_ALL,
        INFO_THEN_DEBUG,
        INFO_ALL
    }

    /**
     * Log mode for the next log message.
     */
    public static enum Mode {
        DEBUG(false), INFO_THEN_DEBUG(true), INFO(false);

        private final boolean fallToDebug;

        Mode(boolean fallToDebug) {
            this.fallToDebug = fallToDebug;
        }

        /**
         * @deprecated Unused, removed in Tomcat 8.
         */
        @Deprecated
        public boolean fallToDebug() {
            return fallToDebug;
        }
    }
}