c76ca24ace5b7ce5218c5fa62e660eddaa76c149
[zxing.git] / zxingorg / src / com / google / zxing / web / ServletContextLogHandler.java
1 /*
2  * Copyright 2008 Google Inc.
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
17 package com.google.zxing.web;
18
19 import javax.servlet.ServletContext;
20 import java.util.logging.Formatter;
21 import java.util.logging.Handler;
22 import java.util.logging.LogRecord;
23
24 /**
25  * @author Sean Owen
26  */
27 final class ServletContextLogHandler extends Handler {
28
29   private final ServletContext context;
30
31   ServletContextLogHandler(ServletContext context) {
32     this.context = context;
33   }
34
35   @Override
36   public void publish(LogRecord record) {
37     Formatter formatter = getFormatter();
38     String message;
39     if (formatter == null) {
40       message = record.getMessage();
41     } else {
42       message = formatter.format(record);
43     }
44     Throwable throwable = record.getThrown();
45     if (throwable == null) {
46       context.log(message);
47     } else {
48       context.log(message, throwable);
49     }
50   }
51
52   @Override
53   public void flush() {
54     // do nothing
55   }
56
57   @Override
58   public void close() {
59     // do nothing
60   }
61
62 }