Search in sources :

Example 71 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project hive by apache.

the class HiveMetaStore method main.

/**
 * @param args
 */
public static void main(String[] args) throws Throwable {
    final Configuration conf = MetastoreConf.newMetastoreConf();
    shutdownHookMgr = ShutdownHookManager.get();
    HiveMetastoreCli cli = new HiveMetastoreCli(conf);
    cli.parse(args);
    final boolean isCliVerbose = cli.isVerbose();
    // NOTE: It is critical to do this prior to initializing log4j, otherwise
    // any log specific settings via hiveconf will be ignored
    Properties hiveconf = cli.addHiveconfToSystemProperties();
    // before any of the other core hive classes are loaded
    try {
        // use Hive's default log4j configuration
        if (System.getProperty("log4j.configurationFile") == null) {
            LogUtils.initHiveLog4j(conf);
        } else {
            // reconfigure log4j after settings via hiveconf are write into System Properties
            LoggerContext context = (LoggerContext) LogManager.getContext(false);
            context.reconfigure();
        }
    } catch (LogUtils.LogInitializationException e) {
        HMSHandler.LOG.warn(e.getMessage());
    }
    startupShutdownMessage(HiveMetaStore.class, args, LOG);
    try {
        String msg = "Starting hive metastore on port " + cli.port;
        HMSHandler.LOG.info(msg);
        if (cli.isVerbose()) {
            System.err.println(msg);
        }
        // set all properties specified on the command line
        for (Map.Entry<Object, Object> item : hiveconf.entrySet()) {
            conf.set((String) item.getKey(), (String) item.getValue());
        }
        // Add shutdown hook.
        shutdownHookMgr.addShutdownHook(() -> {
            String shutdownMsg = "Shutting down hive metastore.";
            HMSHandler.LOG.info(shutdownMsg);
            if (isCliVerbose) {
                System.err.println(shutdownMsg);
            }
            if (MetastoreConf.getBoolVar(conf, ConfVars.METRICS_ENABLED)) {
                try {
                    Metrics.shutdown();
                } catch (Exception e) {
                    LOG.error("error in Metrics deinit: " + e.getClass().getName() + " " + e.getMessage(), e);
                }
            }
            ThreadPool.shutdown();
        }, 10);
        // Start Metrics for Standalone (Remote) Mode
        if (MetastoreConf.getBoolVar(conf, ConfVars.METRICS_ENABLED)) {
            try {
                Metrics.initialize(conf);
            } catch (Exception e) {
                // log exception, but ignore inability to start
                LOG.error("error in Metrics init: " + e.getClass().getName() + " " + e.getMessage(), e);
            }
        }
        Lock startLock = new ReentrantLock();
        Condition startCondition = startLock.newCondition();
        AtomicBoolean startedServing = new AtomicBoolean();
        startMetaStoreThreads(conf, startLock, startCondition, startedServing);
        startMetaStore(cli.getPort(), HadoopThriftAuthBridge.getBridge(), conf, startLock, startCondition, startedServing);
    } catch (Throwable t) {
        // Catch the exception, log it and rethrow it.
        HMSHandler.LOG.error("Metastore Thrift Server threw an exception...", t);
        throw t;
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) Configuration(org.apache.hadoop.conf.Configuration) Properties(java.util.Properties) LoggerContext(org.apache.logging.log4j.core.LoggerContext) JDOException(javax.jdo.JDOException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) ExecutionException(java.util.concurrent.ExecutionException) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogUtils(org.apache.hadoop.hive.metastore.utils.LogUtils) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 72 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project hive by apache.

the class TestHive method testMetaStoreApiTiming.

/**
 * Test logging of timing for metastore api calls
 *
 * @throws Throwable
 */
public void testMetaStoreApiTiming() throws Throwable {
    // Get the RootLogger which, if you don't have log4j2-test.properties defined, will only log ERRORs
    Logger logger = LogManager.getLogger("hive.ql.metadata.Hive");
    Level oldLevel = logger.getLevel();
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    loggerConfig.setLevel(Level.DEBUG);
    ctx.updateLoggers();
    // Create a String Appender to capture log output
    StringAppender appender = StringAppender.createStringAppender("%m");
    appender.addToLogger(logger.getName(), Level.DEBUG);
    appender.start();
    try {
        hm.clearMetaCallTiming();
        hm.getAllDatabases();
        hm.dumpAndClearMetaCallTiming("test");
        String logStr = appender.getOutput();
        String expectedString = "getAllDatabases_()=";
        Assert.assertTrue(logStr + " should contain <" + expectedString, logStr.contains(expectedString));
        // reset the log buffer, verify new dump without any api call does not contain func
        appender.reset();
        hm.dumpAndClearMetaCallTiming("test");
        logStr = appender.getOutput();
        Assert.assertFalse(logStr + " should not contain <" + expectedString, logStr.contains(expectedString));
    } finally {
        loggerConfig.setLevel(oldLevel);
        ctx.updateLoggers();
        appender.removeFromLogger(logger.getName());
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Level(org.apache.logging.log4j.Level) Logger(org.apache.logging.log4j.Logger) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 73 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project hive by apache.

the class OperationManager method initOperationLogCapture.

private void initOperationLogCapture(String loggingMode) {
    // Register another Appender (with the same layout) that talks to us.
    Appender ap = LogDivertAppender.createInstance(this, OperationLog.getLoggingLevel(loggingMode));
    LoggerContext context = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = context.getConfiguration();
    LoggerConfig loggerConfig = configuration.getLoggerConfig(LoggerFactory.getLogger(getClass()).getName());
    loggerConfig.addAppender(ap, null, null);
    context.updateLoggers();
    ap.start();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 74 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext in project storm by apache.

the class LogConfigManager method getLoggerLevels.

public Map<String, Level> getLoggerLevels() {
    Configuration loggerConfig = ((LoggerContext) LogManager.getContext(false)).getConfiguration();
    Map<String, Level> logLevelMap = new HashMap<>();
    for (Map.Entry<String, LoggerConfig> entry : loggerConfig.getLoggers().entrySet()) {
        logLevelMap.put(entry.getKey(), entry.getValue().getLevel());
    }
    return logLevelMap;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) HashMap(java.util.HashMap) LogLevel(org.apache.storm.generated.LogLevel) Level(org.apache.logging.log4j.Level) LoggerContext(org.apache.logging.log4j.core.LoggerContext) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 75 with LoggerContext

use of org.apache.logging.log4j.core.LoggerContext 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)

Aggregations

LoggerContext (org.apache.logging.log4j.core.LoggerContext)163 Configuration (org.apache.logging.log4j.core.config.Configuration)57 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)36 Test (org.junit.Test)33 Appender (org.apache.logging.log4j.core.Appender)18 File (java.io.File)12 IOException (java.io.IOException)12 Logger (org.apache.logging.log4j.Logger)11 BeforeClass (org.junit.BeforeClass)11 Map (java.util.Map)10 Level (org.apache.logging.log4j.Level)8 LogEvent (org.apache.logging.log4j.core.LogEvent)8 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)7 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)7 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)7 Logger (org.apache.logging.log4j.core.Logger)6 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)5 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 RoutingAppender (org.apache.logging.log4j.core.appender.routing.RoutingAppender)4 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)4