Search in sources :

Example 1 with LogLevelAction

use of org.apache.storm.generated.LogLevelAction in project storm by apache.

the class LogConfigManager method processLogConfigChange.

public void processLogConfigChange(LogConfig logConfig) {
    if (null != logConfig) {
        LOG.debug("Processing received log config: {}", logConfig);
        TreeMap<String, LogLevel> loggers = new TreeMap<>(logConfig.get_named_logger_level());
        LoggerContext logContext = (LoggerContext) LogManager.getContext(false);
        Map<String, LogLevel> newLogConfigs = new HashMap<>();
        for (Map.Entry<String, LogLevel> entry : loggers.entrySet()) {
            String msgLoggerName = entry.getKey();
            msgLoggerName = ("ROOT".equalsIgnoreCase(msgLoggerName)) ? LogManager.ROOT_LOGGER_NAME : msgLoggerName;
            LogLevel loggerLevel = entry.getValue();
            // the new-timeouts map now contains logger => timeout
            if (loggerLevel.is_set_reset_log_level_timeout_epoch()) {
                LogLevel copy = new LogLevel(loggerLevel);
                if (originalLogLevels.containsKey(msgLoggerName)) {
                    copy.set_reset_log_level(originalLogLevels.get(msgLoggerName).name());
                } else {
                    copy.set_reset_log_level(Level.INFO.name());
                }
                newLogConfigs.put(msgLoggerName, copy);
            }
        }
        // Look for deleted log timeouts
        TreeMap<String, LogLevel> latestConf = latestLogConfig.get();
        if (latestConf != null) {
            for (String loggerName : latestConf.descendingKeySet()) {
                if (!newLogConfigs.containsKey(loggerName)) {
                    // if we had a timeout, but the timeout is no longer active
                    setLoggerLevel(logContext, loggerName, latestConf.get(loggerName).get_reset_log_level());
                }
            }
        }
        // the merged configs are only for the reset logic
        for (String loggerName : new TreeSet<>(logConfig.get_named_logger_level().keySet())) {
            LogLevel logLevel = logConfig.get_named_logger_level().get(loggerName);
            loggerName = ("ROOT".equalsIgnoreCase(loggerName)) ? LogManager.ROOT_LOGGER_NAME : loggerName;
            LogLevelAction action = logLevel.get_action();
            if (action == LogLevelAction.UPDATE) {
                setLoggerLevel(logContext, loggerName, logLevel.get_target_log_level());
            }
        }
        logContext.updateLoggers();
        latestLogConfig.set(new TreeMap<>(newLogConfigs));
        LOG.debug("New merged log config is {}", latestLogConfig.get());
    }
}
Also used : LogLevelAction(org.apache.storm.generated.LogLevelAction) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) LoggerContext(org.apache.logging.log4j.core.LoggerContext) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) LogLevel(org.apache.storm.generated.LogLevel)

Example 2 with LogLevelAction

use of org.apache.storm.generated.LogLevelAction in project storm by apache.

the class Nimbus method setLogConfig.

@Override
public void setLogConfig(String topoId, LogConfig config) throws TException {
    try {
        setLogConfigCalls.mark();
        Map<String, Object> topoConf = tryReadTopoConf(topoId, topoCache);
        topoConf = Utils.merge(conf, topoConf);
        String topoName = (String) topoConf.get(Config.TOPOLOGY_NAME);
        checkAuthorization(topoName, topoConf, "setLogConfig");
        IStormClusterState state = stormClusterState;
        LogConfig mergedLogConfig = state.topologyLogConfig(topoId, null);
        if (mergedLogConfig == null) {
            mergedLogConfig = new LogConfig();
        }
        if (mergedLogConfig.is_set_named_logger_level()) {
            Map<String, LogLevel> namedLoggers = mergedLogConfig.get_named_logger_level();
            for (LogLevel level : namedLoggers.values()) {
                level.set_action(LogLevelAction.UNCHANGED);
            }
        }
        if (config.is_set_named_logger_level()) {
            for (Entry<String, LogLevel> entry : config.get_named_logger_level().entrySet()) {
                LogLevel logConfig = entry.getValue();
                String loggerName = entry.getKey();
                LogLevelAction action = logConfig.get_action();
                if (loggerName.isEmpty()) {
                    throw new RuntimeException("Named loggers need a valid name. Use ROOT for the root logger");
                }
                switch(action) {
                    case UPDATE:
                        setLoggerTimeouts(logConfig);
                        mergedLogConfig.put_to_named_logger_level(loggerName, logConfig);
                        break;
                    case REMOVE:
                        Map<String, LogLevel> nl = mergedLogConfig.get_named_logger_level();
                        if (nl != null) {
                            nl.remove(loggerName);
                        }
                        break;
                    default:
                        // NOOP
                        break;
                }
            }
        }
        LOG.info("Setting log config for {}:{}", topoName, mergedLogConfig);
        state.setTopologyLogConfig(topoId, mergedLogConfig, topoConf);
    } catch (Exception e) {
        LOG.warn("set log config topology exception. (topology id='{}')", topoId, e);
        if (e instanceof TException) {
            throw (TException) e;
        }
        throw new RuntimeException(e);
    }
}
Also used : TException(org.apache.storm.thrift.TException) LogLevelAction(org.apache.storm.generated.LogLevelAction) IStormClusterState(org.apache.storm.cluster.IStormClusterState) LogLevel(org.apache.storm.generated.LogLevel) WrappedAuthorizationException(org.apache.storm.utils.WrappedAuthorizationException) IOException(java.io.IOException) IllegalStateException(org.apache.storm.generated.IllegalStateException) AlreadyAliveException(org.apache.storm.generated.AlreadyAliveException) WrappedNotAliveException(org.apache.storm.utils.WrappedNotAliveException) WrappedInvalidTopologyException(org.apache.storm.utils.WrappedInvalidTopologyException) AuthorizationException(org.apache.storm.generated.AuthorizationException) NotAliveException(org.apache.storm.generated.NotAliveException) WrappedAlreadyAliveException(org.apache.storm.utils.WrappedAlreadyAliveException) InterruptedIOException(java.io.InterruptedIOException) KeyAlreadyExistsException(org.apache.storm.generated.KeyAlreadyExistsException) TException(org.apache.storm.thrift.TException) WrappedIllegalStateException(org.apache.storm.utils.WrappedIllegalStateException) KeyNotFoundException(org.apache.storm.generated.KeyNotFoundException) InvalidTopologyException(org.apache.storm.generated.InvalidTopologyException) BindException(java.net.BindException) LogConfig(org.apache.storm.generated.LogConfig)

Aggregations

LogLevel (org.apache.storm.generated.LogLevel)2 LogLevelAction (org.apache.storm.generated.LogLevelAction)2 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 BindException (java.net.BindException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 TreeSet (java.util.TreeSet)1 LoggerContext (org.apache.logging.log4j.core.LoggerContext)1 IStormClusterState (org.apache.storm.cluster.IStormClusterState)1 AlreadyAliveException (org.apache.storm.generated.AlreadyAliveException)1 AuthorizationException (org.apache.storm.generated.AuthorizationException)1 IllegalStateException (org.apache.storm.generated.IllegalStateException)1 InvalidTopologyException (org.apache.storm.generated.InvalidTopologyException)1 KeyAlreadyExistsException (org.apache.storm.generated.KeyAlreadyExistsException)1 KeyNotFoundException (org.apache.storm.generated.KeyNotFoundException)1 LogConfig (org.apache.storm.generated.LogConfig)1 NotAliveException (org.apache.storm.generated.NotAliveException)1 TException (org.apache.storm.thrift.TException)1