001/*
002 *  Copyright 2017 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.plugins.core.ui.log;
018
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.log4j.Level;
024import org.apache.log4j.LogManager;
025import org.apache.log4j.Logger;
026import org.apache.log4j.spi.LoggerRepository;
027
028import org.ametys.core.ui.Callable;
029import org.ametys.core.ui.StaticClientSideElement;
030
031/**
032 * A static client side element for the ServerLogMessageTargetFactory, giving some callables 
033 */
034public class ServerLogMessageTargetFactoryElement extends StaticClientSideElement
035{
036    /**
037     * Get the log levels associated with the given log categories
038     * @param categories The log categories to check
039     * @return A map with 2 keys : levels and effectiveLevels. Each one is associating each log category given to its level ('DEBUG'... 'INHERIT'...)
040     */
041    @Callable
042    public Map<String, Map<String, String>> getLevels(List<String> categories)
043    {
044        LoggerRepository loggerRepository = LogManager.getLoggerRepository();
045        
046        Map<String, String> levels = new HashMap<>();
047        Map<String, String> effectiveLevels = new HashMap<>();
048        
049        for (String category : categories)
050        {
051            Logger logger = "root".equals(category) ? loggerRepository.getRootLogger() : loggerRepository.getLogger(category);
052            if (logger != null)
053            {
054                Level level = logger.getLevel();
055                levels.put(category, level != null ? level.toString() : "inherit");
056                effectiveLevels.put(category, logger.getEffectiveLevel().toString());
057            }
058        }
059
060        Map<String, Map<String, String>> results = new HashMap<>();
061        results.put("levels", levels);
062        results.put("effectiveLevels", effectiveLevels);
063        return results;
064    }
065}