001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2022, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.model.processor;
015
016import ch.qos.logback.access.model.ConfigurationModel;
017import ch.qos.logback.core.Context;
018import ch.qos.logback.core.model.Model;
019import ch.qos.logback.core.model.processor.ModelHandlerBase;
020import ch.qos.logback.core.model.processor.ModelHandlerException;
021import ch.qos.logback.core.model.processor.ModelInterpretationContext;
022import ch.qos.logback.core.status.OnConsoleStatusListener;
023import ch.qos.logback.core.util.OptionHelper;
024import ch.qos.logback.core.util.StatusListenerConfigHelper;
025
026public class ConfigurationModelHandler extends ModelHandlerBase {
027    static final String DEBUG_SYSTEM_PROPERTY_KEY = "logback-access.debug";
028
029    public ConfigurationModelHandler(Context context) {
030        super(context);
031    }
032
033    static public ModelHandlerBase makeInstance(Context context, ModelInterpretationContext ic) {
034        return new ConfigurationModelHandler(context);
035    }
036
037    protected Class<ConfigurationModel> getSupportedModelClass() {
038        return ConfigurationModel.class;
039    }
040
041    @Override
042    public void handle(ModelInterpretationContext mic, Model model) throws ModelHandlerException {
043        ConfigurationModel configurationModel = (ConfigurationModel) model;
044        // See LBCLASSIC-225 (the system property is looked up first). Thus, it overrides
045        // the equivalent property in the config file. This reversal of scope priority
046        // is justified
047        // by the use case: the admin trying to chase rogue config file
048        String debug = System.getProperty(DEBUG_SYSTEM_PROPERTY_KEY);
049        if (debug == null) {
050            debug = configurationModel.getDebug();
051        }
052        if (OptionHelper.isNullOrEmpty(debug) || debug.equals("false") || debug.equals("null")) {
053            addInfo(ConfigurationModel.INTERNAL_DEBUG_ATTR + " attribute not set");
054        } else {
055            StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
056        }
057
058    }
059}