001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2024, 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 */
014
015package ch.qos.logback.classic.tyler;
016
017import ch.qos.logback.classic.Level;
018import ch.qos.logback.classic.Logger;
019import ch.qos.logback.classic.LoggerContext;
020import ch.qos.logback.classic.util.LevelUtil;
021import ch.qos.logback.core.Context;
022import ch.qos.logback.core.model.util.PropertyModelHandlerHelper;
023import ch.qos.logback.core.model.util.VariableSubstitutionsHelper;
024import ch.qos.logback.core.spi.ContextAwareBase;
025import ch.qos.logback.core.spi.ContextAwarePropertyContainer;
026import ch.qos.logback.core.status.OnConsoleStatusListener;
027import ch.qos.logback.core.util.OptionHelper;
028import ch.qos.logback.core.util.StatusListenerConfigHelper;
029import ch.qos.logback.core.util.StringUtil;
030
031import java.util.Map;
032
033public class TylerConfiguratorBase extends ContextAwareBase implements ContextAwarePropertyContainer {
034
035    public static final String SET_CONTEXT_METHOD_NAME = "setContext";
036    public static final String SET_CONTEXT_NAME_METHOD_NAME = "setContextName";
037    public static final String SETUP_LOGGER_METHOD_NAME = "setupLogger";
038    public static final String VARIABLE_SUBSTITUTIONS_HELPER_FIELD_NAME = "variableSubstitutionsHelper";
039    public static final String PROPERTY_MODEL_HANDLER_HELPER_FIELD_NAME = "propertyModelHandlerHelper";
040
041    // initialized via #setContext
042    protected VariableSubstitutionsHelper variableSubstitutionsHelper;
043    // context set in #setContext
044    protected PropertyModelHandlerHelper propertyModelHandlerHelper = new PropertyModelHandlerHelper(this);
045
046    protected Logger setupLogger(String loggerName, String levelString, Boolean additivity) {
047        LoggerContext loggerContext = (LoggerContext) context;
048        Logger logger = loggerContext.getLogger(loggerName);
049        if (!OptionHelper.isNullOrEmptyOrAllSpaces(levelString)) {
050            Level level = LevelUtil.levelStringToLevel(levelString);
051            logger.setLevel(level);
052        }
053        if (additivity != null) {
054            logger.setAdditive(additivity);
055        }
056        return logger;
057    }
058
059    @Override
060    public void setContext(Context context) {
061        super.setContext(context);
062        variableSubstitutionsHelper = new VariableSubstitutionsHelper(context);
063        propertyModelHandlerHelper.setContext(context);
064    }
065
066    protected void setContextName(String name) {
067        if(StringUtil.isNullOrEmpty(name)) {
068            addError("Cannot set context name to null or empty string");
069            return;
070        }
071        try {
072            String substName = subst(name);
073            addInfo("Setting context name to ["+substName+"]");
074            context.setName(substName);
075        } catch (IllegalStateException e) {
076            addError("Failed to rename context as [" + name + "]");
077        }
078    }
079
080    protected void addOnConsoleStatusListener() {
081        StatusListenerConfigHelper.addOnConsoleListenerInstance(context, new OnConsoleStatusListener());
082    }
083
084    /**
085     * Performs variable substitution.
086     *
087     * @param ref
088     * @return
089     */
090    @Override
091    public String subst(String ref) {
092        return variableSubstitutionsHelper.subst(ref);
093    }
094
095    @Override
096    public void addSubstitutionProperty(String key, String value) {
097        variableSubstitutionsHelper.addSubstitutionProperty(key, value);
098    }
099
100    /**
101     * If a key is found in propertiesMap then return it.
102     */
103    @Override
104    public String getProperty(String key) {
105        return variableSubstitutionsHelper.getProperty(key);
106    }
107
108    @Override
109    public Map<String, String> getCopyOfPropertyMap() {
110        return variableSubstitutionsHelper.getCopyOfPropertyMap();
111    }
112
113    public boolean isNull(String k) {
114        String val = OptionHelper.propertyLookup(k, this, context);
115        return (val == null);
116    }
117
118    /**
119     * Method used in conditional evaluation
120     *
121     * @param k  a property name
122     * @return true if the property is defined
123     * @since 1.5.4
124     */
125    public boolean isDefined(String k) {
126        String val = OptionHelper.propertyLookup(k, this, context);
127        return (val != null);
128    }
129
130    /**
131     * Shorthand for {@link #property(String)}.
132     *
133     * @param k a property name
134     * @return value of property k
135     * @since 1.5.4
136     */
137    public String p(String k) {
138        return property(k);
139    }
140
141    /**
142     * Return the value of the property named k. If the value is null, then the
143     * empty string is returned to avoid null checks.
144     *
145     * @param k property name
146     * @return the value of the property named k
147     * @since 1.5.4
148     */
149    public String property(String k) {
150        String val = OptionHelper.propertyLookup(k, this, context);
151        if (val != null)
152            return val;
153        else
154            return "";
155    }
156}