All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.networknt.logging.handler.LoggerPostHandler Maven / Gradle / Ivy

Go to download

This module will provide the information about logging and switch logging level.

There is a newer version: 2.1.38
Show newest version
/*
 * Copyright (c) 2016 Network New Technologies Inc.
 *
 * Licensed 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 com.networknt.logging.handler;

import ch.qos.logback.classic.Level;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.body.BodyHandler;
import com.networknt.config.Config;
import com.networknt.config.JsonMapper;
import com.networknt.handler.LightHttpHandler;
import com.networknt.httpstring.ContentType;
import com.networknt.logging.model.LoggerConfig;
import com.networknt.logging.model.LoggerInfo;
import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import org.slf4j.LoggerFactory;

import java.util.Deque;
import java.util.List;
import java.util.Map;

/**
 * This handler will change the logging level for the given Logger. Ex. ERROR to DEBUG
 *
 */
public class LoggerPostHandler implements LightHttpHandler {
    public static final String CONFIG_NAME = "logging";
    static final String STATUS_LOGGER_INFO_DISABLED = "ERR12108";
    static final String STATUS_REQUEST_BODY_MISSING = "ERR10059";

    public LoggerPostHandler() {
    }

    @Override
    public void handleRequest(final HttpServerExchange exchange) throws Exception {
        LoggerConfig config = (LoggerConfig) Config.getInstance().getJsonObjectConfig(CONFIG_NAME, LoggerConfig.class);
        if (config.isEnabled()) {
            List loggers = (List) exchange.getAttachment(BodyHandler.REQUEST_BODY);
            if(loggers == null) {
                logger.error("loggers is null from the attachment. Most likely, the body handler is not in the handler chain.");
                setExchangeStatus(exchange, STATUS_REQUEST_BODY_MISSING);
            } else {
                for (Object object : loggers) {
                    Map map = (Map) object;
                    String name = map.get("name");
                    Level level = Level.valueOf(map.get("level"));
                    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(name);
                    if(level != logger.getLevel()) logger.setLevel(level);
                }

                exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, ContentType.APPLICATION_JSON.value());
                exchange.getResponseSender().send(JsonMapper.toJson(loggers));
            }
        } else {
            logger.error("Logging is disabled in logging.yml");
            setExchangeStatus(exchange, STATUS_LOGGER_INFO_DISABLED);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy