// Path: components/logging/org.wso2.carbon.logging.service/src/main/java/org/wso2/carbon/logging/service/data/LoggerData.java
// public class LoggerData {
// 
//     private String name;
//     private String level;
//     private String componentName;
// 
//     public LoggerData() {
//     }
// 
//     public LoggerData(String name, String level, String componentName) {
//         this.name = name;
//         this.level = level;
//         this.componentName = componentName;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getLevel() {
//         return level;
//     }
// 
//     public void setLevel(String level) {
//         this.level = level;
//     }
// 
//     public String getComponentName() {
//         return componentName;
//     }
// 
//     public void setComponentName(String componentName) {
//         this.componentName = componentName;
//     }
// }
// 
// Path: components/logging/org.wso2.carbon.logging.service/src/main/java/org/wso2/carbon/logging/service/util/Utils.java
// public class Utils {
// 
//     /**
//      * Util method to return the specified  property from a properties file.
//      *
//      * @param srcFile - The source file which needs to be looked up.
//      * @param key     - Key of the property.
//      * @return - Value of the property.
//      */
//     public static String getProperty(File srcFile, String key) throws IOException {
// 
//         String value;
//         try (FileInputStream fis = new FileInputStream(srcFile)) {
//             Properties properties = new Properties();
//             properties.load(fis);
//             value = properties.getProperty(key);
//         } catch (IOException e) {
//             throw new IOException("Error occurred while reading the input stream");
//         }
//         return value;
//     }
// }


import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.PropertiesConfigurationLayout;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.logging.service.data.LoggerData;
import org.wso2.carbon.logging.service.util.Utils;
import org.wso2.carbon.utils.ServerConstants;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
 * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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 org.wso2.carbon.logging.service;




/**
 * This is the Admin service used for obtaining Log4J2 information about the system and also used for
 * managing the system Log4J2 configuration.
 */
public class LoggingAdmin {
    private static final Log log = LogFactory.getLog(LoggingAdmin.class);

    private String filePath = System.getProperty(ServerConstants.CARBON_CONFIG_DIR_PATH) + File.separator
            + "log4j2.properties";
    private File logPropFile = new File(filePath);

    private PropertiesConfiguration config;
    private PropertiesConfigurationLayout layout;

    private static final String LOGGER_PREFIX = "logger.";
    private static final String LOGGER_LEVEL_SUFFIX = ".level";
    private static final String LOGGER_NAME_SUFFIX = ".name";
    private static final String LOGGERS_PROPERTY = "loggers";
    private static final String ROOT_LOGGER = "rootLogger";

    public String getLoggers() throws IOException {
// Path: components/logging/org.wso2.carbon.logging.service/src/main/java/org/wso2/carbon/logging/service/data/LoggerData.java
// public class LoggerData {
// 
//     private String name;
//     private String level;
//     private String componentName;
// 
//     public LoggerData() {
//     }
// 
//     public LoggerData(String name, String level, String componentName) {
//         this.name = name;
//         this.level = level;
//         this.componentName = componentName;
//     }
// 
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getLevel() {
//         return level;
//     }
// 
//     public void setLevel(String level) {
//         this.level = level;
//     }
// 
//     public String getComponentName() {
//         return componentName;
//     }
// 
//     public void setComponentName(String componentName) {
//         this.componentName = componentName;
//     }
// }
// 
// Path: components/logging/org.wso2.carbon.logging.service/src/main/java/org/wso2/carbon/logging/service/util/Utils.java
// public class Utils {
// 
//     /**
//      * Util method to return the specified  property from a properties file.
//      *
//      * @param srcFile - The source file which needs to be looked up.
//      * @param key     - Key of the property.
//      * @return - Value of the property.
//      */
//     public static String getProperty(File srcFile, String key) throws IOException {
// 
//         String value;
//         try (FileInputStream fis = new FileInputStream(srcFile)) {
//             Properties properties = new Properties();
//             properties.load(fis);
//             value = properties.getProperty(key);
//         } catch (IOException e) {
//             throw new IOException("Error occurred while reading the input stream");
//         }
//         return value;
//     }
// }


// Path: components/logging/org.wso2.carbon.logging.service/src/main/java/org/wso2/carbon/logging/service/LoggingAdmin.java
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.PropertiesConfigurationLayout;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.logging.service.data.LoggerData;
import org.wso2.carbon.logging.service.util.Utils;
import org.wso2.carbon.utils.ServerConstants;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
 * Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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 org.wso2.carbon.logging.service;




/**
 * This is the Admin service used for obtaining Log4J2 information about the system and also used for
 * managing the system Log4J2 configuration.
 */
public class LoggingAdmin {
    private static final Log log = LogFactory.getLog(LoggingAdmin.class);

    private String filePath = System.getProperty(ServerConstants.CARBON_CONFIG_DIR_PATH) + File.separator
            + "log4j2.properties";
    private File logPropFile = new File(filePath);

    private PropertiesConfiguration config;
    private PropertiesConfigurationLayout layout;

    private static final String LOGGER_PREFIX = "logger.";
    private static final String LOGGER_LEVEL_SUFFIX = ".level";
    private static final String LOGGER_NAME_SUFFIX = ".name";
    private static final String LOGGERS_PROPERTY = "loggers";
    private static final String ROOT_LOGGER = "rootLogger";

    public String getLoggers() throws IOException {
        return Utils.getProperty(logPropFile, LOGGERS_PROPERTY);