use of org.apache.logging.log4j.util.PropertiesUtil in project Javacord by Javacord.
the class LoggerUtil method getLogger.
/**
* Get or create a logger with the given name.
*
* @param name The name of the logger.
* @return The logger with the given name.
*/
public static Logger getLogger(String name) {
AtomicBoolean logWarning = new AtomicBoolean(false);
initialized.updateAndGet(initialized -> {
if (!initialized && !ProviderUtil.hasProviders()) {
noLogger.set(true);
logWarning.set(true);
}
return true;
});
if (noLogger.get()) {
return loggers.computeIfAbsent(name, key -> {
Level level = FallbackLoggerConfiguration.isTraceEnabled() ? Level.TRACE : (FallbackLoggerConfiguration.isDebugEnabled() ? Level.DEBUG : Level.INFO);
Logger logger = new SimpleLogger(name, level, true, false, true, true, "yyyy-MM-dd HH:mm:ss.SSSZ", null, new PropertiesUtil(new Properties()), System.out);
if (logWarning.get()) {
logger.info("No Log4j2 compatible logger was found. Using default Javacord implementation!");
}
return new PrivacyProtectionLogger(logger);
});
} else {
return new PrivacyProtectionLogger(LogManager.getLogger(name));
}
}
use of org.apache.logging.log4j.util.PropertiesUtil in project logging-log4j2 by apache.
the class UrlConnectionFactory method createConnection.
@SuppressWarnings("unchecked")
public static <T extends URLConnection> T createConnection(final URL url, final long lastModifiedMillis, final SslConfiguration sslConfiguration, final AuthorizationProvider authorizationProvider) throws IOException {
final PropertiesUtil props = PropertiesUtil.getProperties();
final List<String> allowed = Arrays.asList(Strings.splitList(props.getStringProperty(ALLOWED_PROTOCOLS, DEFAULT_ALLOWED_PROTOCOLS).toLowerCase(Locale.ROOT)));
if (allowed.size() == 1 && NO_PROTOCOLS.equals(allowed.get(0))) {
throw new ProtocolException("No external protocols have been enabled");
}
final String protocol = url.getProtocol();
if (protocol == null) {
throw new ProtocolException("No protocol was specified on " + url.toString());
}
if (!allowed.contains(protocol)) {
throw new ProtocolException("Protocol " + protocol + " has not been enabled as an allowed protocol");
}
URLConnection urlConnection;
if (url.getProtocol().equals(HTTP) || url.getProtocol().equals(HTTPS)) {
final HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
if (authorizationProvider != null) {
authorizationProvider.addAuthorization(httpURLConnection);
}
httpURLConnection.setAllowUserInteraction(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
if (connectTimeoutMillis > 0) {
httpURLConnection.setConnectTimeout(connectTimeoutMillis);
}
if (readTimeoutMillis > 0) {
httpURLConnection.setReadTimeout(readTimeoutMillis);
}
final String[] fileParts = url.getFile().split("\\.");
final String type = fileParts[fileParts.length - 1].trim();
final String contentType = isXml(type) ? XML : isJson(type) ? JSON : isProperties(type) ? PROPERTIES : TEXT;
httpURLConnection.setRequestProperty("Content-Type", contentType);
if (lastModifiedMillis > 0) {
httpURLConnection.setIfModifiedSince(lastModifiedMillis);
}
if (url.getProtocol().equals(HTTPS) && sslConfiguration != null) {
((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(sslConfiguration.getSslSocketFactory());
if (!sslConfiguration.isVerifyHostName()) {
((HttpsURLConnection) httpURLConnection).setHostnameVerifier(LaxHostnameVerifier.INSTANCE);
}
}
urlConnection = httpURLConnection;
} else if (url.getProtocol().equals(JAR)) {
urlConnection = url.openConnection();
urlConnection.setUseCaches(false);
} else {
urlConnection = url.openConnection();
}
return (T) urlConnection;
}
use of org.apache.logging.log4j.util.PropertiesUtil in project logging-log4j2 by apache.
the class SslConfigurationFactoryTest method testStaticConfiguration.
@Test
public void testStaticConfiguration() {
final Properties props = new Properties();
final PropertiesUtil util = new PropertiesUtil(props);
// No keystore and truststore -> no SslConfiguration
SslConfiguration sslConfiguration = SslConfigurationFactory.createSslConfiguration(util);
assertNull(sslConfiguration);
// Only keystore
props.clear();
addKeystoreConfiguration(props);
util.reload();
sslConfiguration = SslConfigurationFactory.createSslConfiguration(util);
assertNotNull(sslConfiguration);
assertNotNull(sslConfiguration.getKeyStoreConfig());
assertNull(sslConfiguration.getTrustStoreConfig());
// Only truststore
props.clear();
addTruststoreConfiguration(props);
util.reload();
sslConfiguration = SslConfigurationFactory.createSslConfiguration(util);
assertNotNull(sslConfiguration);
assertNull(sslConfiguration.getKeyStoreConfig());
assertNotNull(sslConfiguration.getTrustStoreConfig());
// Both
props.clear();
addKeystoreConfiguration(props);
addTruststoreConfiguration(props);
util.reload();
sslConfiguration = SslConfigurationFactory.createSslConfiguration(util);
assertNotNull(sslConfiguration);
assertNotNull(sslConfiguration.getKeyStoreConfig());
assertNotNull(sslConfiguration.getTrustStoreConfig());
}
use of org.apache.logging.log4j.util.PropertiesUtil in project logging-log4j2 by apache.
the class Log4j2SpringBootLoggingSystem method getStandardConfigLocations.
@Override
protected String[] getStandardConfigLocations() {
String[] locations = super.getStandardConfigLocations();
PropertiesUtil props = new PropertiesUtil(new Properties());
String location = props.getStringProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
if (location != null) {
List<String> list = new ArrayList<>(Arrays.asList(super.getStandardConfigLocations()));
list.add(location);
locations = list.toArray(new String[0]);
}
return locations;
}
Aggregations