
org.glowroot.agent.init.LoggingInit Maven / Gradle / Ivy
/*
* Copyright 2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.glowroot.agent.init;
import java.io.File;
import org.glowroot.agent.shaded.qos.logback.classic.LoggerContext;
import org.glowroot.agent.shaded.qos.logback.classic.joran.JoranConfigurator;
import org.glowroot.agent.shaded.qos.logback.core.joran.spi.JoranException;
import org.glowroot.agent.shaded.qos.logback.core.util.StatusPrinter;
import org.glowroot.agent.shaded.google.common.io.Resources;
import org.glowroot.agent.shaded.slf4j.Logger;
import org.glowroot.agent.shaded.slf4j.LoggerFactory;
import org.glowroot.common.util.OnlyUsedByTests;
public class LoggingInit {
private static final Logger logger = LoggerFactory.getLogger(LoggingInit.class);
private LoggingInit() {}
public static void initStaticLoggerState(File baseDir) {
if (shouldOverrideLogging()) {
overrideLogging(baseDir);
}
}
public static boolean shouldOverrideLogging() {
// don't override glowroot.logback-test.xml
return isShaded() && ClassLoader.getSystemResource("glowroot.logback-test.xml") == null;
}
@OnlyUsedByTests
public static void close() {
if (shouldOverrideLogging()) {
((LoggerContext) LoggerFactory.getILoggerFactory()).reset();
}
}
private static void overrideLogging(File baseDir) {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
context.putProperty("glowroot.base.dir", baseDir.getPath());
File logbackXmlFile = new File(baseDir, "glowroot.logback.xml");
if (logbackXmlFile.exists()) {
configurator.doConfigure(logbackXmlFile);
} else {
configurator.doConfigure(Resources.getResource("glowroot.logback.xml"));
}
} catch (JoranException je) {
// any errors are printed below by StatusPrinter
}
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
private static boolean isShaded() {
try {
Class.forName("org.glowroot.agent.shaded.slf4j.Logger");
return true;
} catch (ClassNotFoundException e) {
// log exception at trace level
logger.trace(e.getMessage(), e);
return false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy