|
| 1 | +/* |
| 2 | + * Copyright 2001-2004 The Apache Software Foundation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.technosf.slf4.interceptor; |
| 17 | + |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.Marker; |
| 20 | +import org.slf4j.helpers.FormattingTuple; |
| 21 | +import org.slf4j.helpers.MessageFormatter; |
| 22 | + |
| 23 | +/** |
| 24 | + * Abstract implementation of Interceptor calls |
| 25 | + * |
| 26 | + * @author technosf |
| 27 | + * @since 0.0.1 |
| 28 | + * @version 0.0.1 |
| 29 | + */ |
| 30 | +public abstract class AbstractInterceptor |
| 31 | + implements Interceptor |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Intercept mode, defaulting to PASSTHROUGH |
| 35 | + */ |
| 36 | + private Mode mode = Mode.PASSTHROUGH; |
| 37 | + |
| 38 | + |
| 39 | + /** |
| 40 | + * @param logLeve |
| 41 | + * @param msg |
| 42 | + * @return true if log should be filtered |
| 43 | + */ |
| 44 | + abstract boolean filter(LogLevel logLeve, String msg); |
| 45 | + |
| 46 | + |
| 47 | + /** |
| 48 | + * @param logLeve |
| 49 | + * @param tuple |
| 50 | + * @return true if log should be filtered |
| 51 | + */ |
| 52 | + abstract boolean filter(LogLevel logLeve, FormattingTuple tuple); |
| 53 | + |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritDoc} |
| 57 | + * |
| 58 | + * @see com.github.technosf.slf4.interceptor.Interceptor#getMode() |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public final Mode getMode() |
| 62 | + { |
| 63 | + return mode; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * Sets the Interceptor mode |
| 69 | + * |
| 70 | + * @param mode |
| 71 | + * the mode |
| 72 | + */ |
| 73 | + public final void setMode(Mode mode) |
| 74 | + { |
| 75 | + this.mode = mode; |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + /** |
| 80 | + * {@inheritDoc} |
| 81 | + * |
| 82 | + * @see com.github.technosf.slf4.interceptor.Interceptor#intercept(com.github.technosf.slf4.interceptor.LogLevel, |
| 83 | + * org.slf4j.Logger, java.lang.String) |
| 84 | + */ |
| 85 | + @Override |
| 86 | + public void intercept(LogLevel logLevel, Logger log, String msg) |
| 87 | + { |
| 88 | + if (mode.logToLogger |
| 89 | + || (mode.logToInterceptor && formatAndLog(logLevel, msg))) |
| 90 | + { |
| 91 | + logLevel.log(log, msg); |
| 92 | + } |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + /** |
| 98 | + * {@inheritDoc} |
| 99 | + * |
| 100 | + * @see com.github.technosf.slf4.interceptor.Interceptor#intercept(com.github.technosf.slf4.interceptor.LogLevel, |
| 101 | + * org.slf4j.Logger, java.lang.String, java.lang.Object) |
| 102 | + */ |
| 103 | + @Override |
| 104 | + public void intercept(LogLevel logLevel, Logger log, String format, |
| 105 | + Object arg) |
| 106 | + { |
| 107 | + if (mode.logToLogger |
| 108 | + || (mode.logToInterceptor |
| 109 | + && formatAndLog(logLevel, format, arg))) |
| 110 | + { |
| 111 | + logLevel.log(log, format, arg); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + /** |
| 117 | + * {@inheritDoc} |
| 118 | + * |
| 119 | + * @see com.github.technosf.slf4.interceptor.Interceptor#intercept(com.github.technosf.slf4.interceptor.LogLevel, |
| 120 | + * org.slf4j.Logger, java.lang.String, java.lang.Object, |
| 121 | + * java.lang.Object) |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public void intercept(LogLevel logLevel, Logger log, String format, |
| 125 | + Object arg1, Object arg2) |
| 126 | + { |
| 127 | + if (mode.logToLogger |
| 128 | + || (mode.logToInterceptor |
| 129 | + && formatAndLog(logLevel, format, arg1, arg2))) |
| 130 | + { |
| 131 | + logLevel.log(log, format, arg1, arg2); |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + /** |
| 138 | + * {@inheritDoc} |
| 139 | + * |
| 140 | + * @see com.github.technosf.slf4.interceptor.Interceptor#intercept(com.github.technosf.slf4.interceptor.LogLevel, |
| 141 | + * org.slf4j.Logger, java.lang.String, java.lang.Object[]) |
| 142 | + */ |
| 143 | + @Override |
| 144 | + public void intercept(LogLevel logLevel, Logger log, String format, |
| 145 | + Object... arguments) |
| 146 | + { |
| 147 | + if (mode.logToLogger |
| 148 | + || (mode.logToInterceptor |
| 149 | + && formatAndLog(logLevel, format, arguments))) |
| 150 | + { |
| 151 | + logLevel.log(log, format, arguments); |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + /** |
| 158 | + * {@inheritDoc} |
| 159 | + * |
| 160 | + * @see com.github.technosf.slf4.interceptor.Interceptor#intercept(com.github.technosf.slf4.interceptor.LogLevel, |
| 161 | + * org.slf4j.Logger, java.lang.String, java.lang.Throwable) |
| 162 | + */ |
| 163 | + @Override |
| 164 | + public void intercept(LogLevel logLevel, Logger log, String msg, |
| 165 | + Throwable t) |
| 166 | + { |
| 167 | + if (mode.logToLogger |
| 168 | + || (mode.logToInterceptor && formatAndLog(logLevel, msg, t))) |
| 169 | + { |
| 170 | + logLevel.log(log, msg, t); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + @Override |
| 176 | + public void intercept(LogLevel logLevel, Logger log, Marker marker, |
| 177 | + String msg) |
| 178 | + { |
| 179 | + if (mode.logToLogger |
| 180 | + || (mode.logToInterceptor |
| 181 | + && formatAndLog(logLevel, msg))) |
| 182 | + { |
| 183 | + logLevel.log(log, marker, msg); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + @Override |
| 189 | + public void intercept(LogLevel logLevel, Logger log, Marker marker, |
| 190 | + String format, Object arg) |
| 191 | + { |
| 192 | + if (mode.logToLogger |
| 193 | + || (mode.logToInterceptor |
| 194 | + && formatAndLog(logLevel, format, arg))) |
| 195 | + { |
| 196 | + logLevel.log(log, marker, format, arg); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + |
| 201 | + @Override |
| 202 | + public void intercept(LogLevel logLevel, Logger log, Marker marker, |
| 203 | + String format, Object arg1, Object arg2) |
| 204 | + { |
| 205 | + if (mode.logToLogger |
| 206 | + || (mode.logToInterceptor |
| 207 | + && formatAndLog(logLevel, format, arg1, arg2))) |
| 208 | + { |
| 209 | + logLevel.log(log, marker, format, arg1, arg2); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + |
| 214 | + @Override |
| 215 | + public void intercept(LogLevel logLevel, Logger log, Marker marker, |
| 216 | + String format, Object... arguments) |
| 217 | + { |
| 218 | + if (mode.logToLogger |
| 219 | + || (mode.logToInterceptor |
| 220 | + && formatAndLog(logLevel, format, arguments))) |
| 221 | + { |
| 222 | + logLevel.log(log, marker, format, arguments); |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + |
| 227 | + @Override |
| 228 | + public void intercept(LogLevel logLevel, Logger log, Marker marker, |
| 229 | + String msg, Throwable t) |
| 230 | + { |
| 231 | + if (mode.logToLogger |
| 232 | + || (mode.logToInterceptor |
| 233 | + && formatAndLog(logLevel, msg, t))) |
| 234 | + { |
| 235 | + logLevel.log(log, marker, msg, t); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + |
| 240 | + /* ---------------------------------------------------------------- |
| 241 | + * |
| 242 | + * SLF4J Simple Logger Helpers |
| 243 | + * |
| 244 | + * ---------------------------------------------------------------- |
| 245 | + */ |
| 246 | + |
| 247 | + /** |
| 248 | + * For formatted messages, first substitute arguments and then log. |
| 249 | + * |
| 250 | + * @param level |
| 251 | + * @param format |
| 252 | + * @param arg1 |
| 253 | + * @param arg2 |
| 254 | + */ |
| 255 | + private boolean formatAndLog(LogLevel level, String msg) |
| 256 | + { |
| 257 | + return !filter(level, msg); |
| 258 | + } |
| 259 | + |
| 260 | + |
| 261 | + /** |
| 262 | + * For formatted messages, first substitute arguments and then log. |
| 263 | + * |
| 264 | + * @param level |
| 265 | + * @param format |
| 266 | + * @param arg1 |
| 267 | + * @param arg2 |
| 268 | + */ |
| 269 | + private boolean formatAndLog(LogLevel level, String format, Object arg) |
| 270 | + { |
| 271 | + FormattingTuple tp = MessageFormatter.format(format, arg); |
| 272 | + return !filter(level, tp); |
| 273 | + } |
| 274 | + |
| 275 | + |
| 276 | + /** |
| 277 | + * For formatted messages, first substitute arguments and then log. |
| 278 | + * |
| 279 | + * @param level |
| 280 | + * @param format |
| 281 | + * @param arg1 |
| 282 | + * @param arg2 |
| 283 | + */ |
| 284 | + private boolean formatAndLog(LogLevel level, String format, Object arg1, |
| 285 | + Object arg2) |
| 286 | + { |
| 287 | + FormattingTuple tp = MessageFormatter.format(format, arg1, arg2); |
| 288 | + return !filter(level, tp); |
| 289 | + } |
| 290 | + |
| 291 | + |
| 292 | + /** |
| 293 | + * For formatted messages, first substitute arguments and then log. |
| 294 | + * |
| 295 | + * @param level |
| 296 | + * @param format |
| 297 | + * @param arguments |
| 298 | + * a list of 3 ore more arguments |
| 299 | + */ |
| 300 | + private boolean formatAndLog(LogLevel level, String format, |
| 301 | + Object... arguments) |
| 302 | + { |
| 303 | + FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments); |
| 304 | + return !filter(level, tp); |
| 305 | + } |
| 306 | + /* ---------------------------------------------------------------- */ |
| 307 | + |
| 308 | +} |
0 commit comments