@Override public void run() { context = LogManager.getContext(false); } }
/** * Detects if a Logger with the specified name exists. This is a convenience method for porting from version 1. * * @param name The Logger name to search for. * @return true if the Logger exists, false otherwise. * @see LoggerContext#hasLogger(String) */ public static boolean exists(final String name) { return getContext().hasLogger(name); }
/** * Initialize this servlet. */ @Override public void init() throws ServletException { context = (LoggerContext) LogManager.getContext(false); conf = context.getConfiguration(); }
@VisibleForTesting protected Collection<LoggerConfig> getLoggerConfigs() { final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); final Configuration configuration = loggerContext.getConfiguration(); return configuration.getLoggers().values(); }
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; }
/** * Given an appender name, as configured, get the parent directory of the appender's log file. * Note that if anything goes wrong, this will throw an Error and exit. */ private String logRootDir(String appenderName) { Appender appender = ((LoggerContext) LogManager.getContext()).getConfiguration().getAppender(appenderName); if (appenderName != null && appender != null && RollingFileAppender.class.isInstance(appender)) { return new File(((RollingFileAppender) appender).getFileName()).getParent(); } else { throw new RuntimeException("Log viewer could not find configured appender, or the appender is not a FileAppender. " + "Please check that the appender name configured in storm and log4j agree."); } }
/** * Always treat de-serialization as a full-blown constructor, by validating the final state of * the de-serialized object. */ private void readObject(final ObjectInputStream aInputStream) throws ClassNotFoundException, IOException { // always perform the default de-serialization first aInputStream.defaultReadObject(); logger = LogManager.getContext().getLogger(name); converter = createConverter(); }
/** * Returns a Logger with the specified name. * * @param name The logger name. If null the name of the calling class will be used. * @return The Logger. * @throws UnsupportedOperationException if {@code name} is {@code null} and the calling class cannot be determined. */ public static Logger getLogger(final String name) { return name != null ? getContext(false).getLogger(name) : getLogger(StackLocatorUtil.getCallerClass(2)); }
private Appender getAppender(final String appenderName) { final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); final Configuration configuration = loggerContext.getConfiguration(); return configuration.getAppender(appenderName); }
/** * Returns a Logger with the specified name. * * @param name The logger name. If null the name of the calling class will be used. * @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the * logger but will log a warning if mismatched. * @return The Logger. * @throws UnsupportedOperationException if {@code name} is {@code null} and the calling class cannot be determined. */ public static Logger getLogger(final String name, final MessageFactory messageFactory) { return name != null ? getContext(false).getLogger(name, messageFactory) : getLogger( StackLocatorUtil.getCallerClass(2), messageFactory); }
private static void workAroundLog4j2_5Bug() { // use reflection so we can use the same test with older versions of log4j2 try { final Method setUseThreadLocals = AsyncLoggerContext.class.getDeclaredMethod("setUseThreadLocals", new Class[]{boolean.class}); final LoggerContext context = LogManager.getContext(false); setUseThreadLocals.invoke(context, new Object[] {Boolean.TRUE}); } catch (final Throwable ignored) { } } }
@VisibleForTesting protected Level getLoggerLevel(final String loggerName) { final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); final Configuration configuration = loggerContext.getConfiguration(); final LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName); return loggerConfig.getLevel(); }
/** * Returns a Logger using the fully qualified name of the Class as the Logger name. * * @param clazz The Class whose name should be used as the Logger name. If null it will default to the calling * class. * @return The Logger. * @throws UnsupportedOperationException if {@code clazz} is {@code null} and the calling class cannot be * determined. */ public static Logger getLogger(final Class<?> clazz) { final Class<?> cls = callerClass(clazz); return getContext(cls.getClassLoader(), false).getLogger(toLoggerName(cls)); }
@Test public void testShutdown() { final LoggerContext loggerContext = LogManager.getContext(false); } }
private void initializeLogging(final Level logLevel) { final LoggerContext context = (LoggerContext) LogManager.getContext(false); final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration(); config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).setLevel(logLevel); config.getLoggerConfig(Main.class.getPackage().getName()).setLevel(logLevel); context.updateLoggers(config); }
@VisibleForTesting protected void setLoggerLevel(final String loggerName, final Level level) { final LoggerContext context = (LoggerContext) LogManager.getContext(false); final Configuration config = context.getConfiguration(); final LoggerConfig loggerConfig = config.getLoggerConfig(loggerName); if(loggerName.equals(loggerConfig.getName())) { loggerConfig.setLevel(level); } else { final LoggerConfig newLoggerConfig = new LoggerConfig(loggerName, level, loggerConfig.isAdditive()); newLoggerConfig.setLevel(level); config.addLogger(loggerName, newLoggerConfig); } context.updateLoggers(); }
private void addInstrumentedAppender(final MetricRegistry metrics, final Level level) { final InstrumentedAppender appender = new InstrumentedAppender(metrics, null, null, false); appender.start(); final LoggerContext context = (LoggerContext) LogManager.getContext(false); final org.apache.logging.log4j.core.config.Configuration config = context.getConfiguration(); config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME).addAppender(appender, level, null); context.updateLoggers(config); }
@Test(timeout = 5000) public void shutdownTest() throws Exception { final LoggerContext ctx = (LoggerContext)LogManager.getContext(false); final Logger logger = ctx.getLogger("Logger"); logger.info("This is a test"); ctx.stop(); }
@Before public void setup() { // programmatically set root logger level to INFO. By default if log4j2-test.properties is not // available root logger will use ERROR log level LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration config = ctx.getConfiguration(); LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); loggerConfig.setLevel(Level.INFO); ctx.updateLoggers(); }
public TestTxnHandler() throws Exception { TxnDbUtil.setConfValues(conf); LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration conf = ctx.getConfiguration(); conf.getLoggerConfig(CLASS_NAME).setLevel(Level.DEBUG); ctx.updateLoggers(conf); tearDown(); }