/**
 * Copyright (c) 2015-2017, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl-3.0.txt
 * <p>
 * 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 io.jboot.web.handler;

import com.jfinal.handler.Handler;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import io.jboot.exception.JbootExceptionHolder;
import io.jboot.web.RequestManager;
import io.jboot.web.session.JbootServletRequestWrapper;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class JbootHandler extends Handler {

    @Override
    public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {

        if (target.indexOf('.') != -1) {
            return;
        }

        RequestManager.me().handle(request, response);
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        JbootExceptionHolder.init();
        try {
            doHandle(target, request, response, isHandled);
        } finally {
            try {
                context.shutdown();
            } catch (Throwable ex) {
            }

            try {
                RequestManager.me().release();
            } catch (Throwable ex) {
            }

            try {
                JbootExceptionHolder.release();
            } catch (Throwable ex) {
            }
        }

    }

    private void doHandle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
        request.setAttribute("REQUEST", request);
        request.setAttribute("CPATH", request.getContextPath());
        next.handle(target, new JbootServletRequestWrapper(request), response, isHandled);
    }


}

@@ -720,24 +720,51 @@
 er.Handler;%0A
+import com.jfinal.log.Log;%0A
 import com.n
@@ -822,16 +822,16 @@
 ontext;%0A
-
 import i
@@ -1101,16 +1101,69 @@
 andler %7B
+%0A    static Log log = Log.getLog(JbootHandler.class);
 %0A%0A    @O
@@ -1714,32 +1714,78 @@
 Throwable ex) %7B%0A
+                log.error(ex.toString(), ex);%0A
             %7D%0A%0A 
@@ -1877,32 +1877,78 @@
 Throwable ex) %7B%0A
+                log.error(ex.toString(), ex);%0A
             %7D%0A%0A 
@@ -2004,32 +2004,32 @@
 lder.release();%0A
-
             %7D ca
@@ -2041,32 +2041,78 @@
 Throwable ex) %7B%0A
+                log.error(ex.toString(), ex);%0A
             %7D%0A  

add print error info to log in JbootHandler