Search in sources :

Example 26 with PropertiesUtil

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));
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Level(org.apache.logging.log4j.Level) Logger(org.apache.logging.log4j.Logger) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger) Properties(java.util.Properties) SimpleLogger(org.apache.logging.log4j.simple.SimpleLogger)

Example 27 with PropertiesUtil

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;
}
Also used : ProtocolException(java.net.ProtocolException) HttpURLConnection(java.net.HttpURLConnection) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection) JarURLConnection(java.net.JarURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 28 with PropertiesUtil

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());
}
Also used : PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 29 with PropertiesUtil

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;
}
Also used : ArrayList(java.util.ArrayList) PropertiesUtil(org.apache.logging.log4j.util.PropertiesUtil) Properties(java.util.Properties)

Aggregations

PropertiesUtil (org.apache.logging.log4j.util.PropertiesUtil)29 Properties (java.util.Properties)7 ArrayList (java.util.ArrayList)6 Level (org.apache.logging.log4j.Level)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Logger (org.apache.logging.log4j.Logger)3 CloseShieldOutputStream (org.apache.logging.log4j.core.util.CloseShieldOutputStream)3 SimpleLogger (org.apache.logging.log4j.simple.SimpleLogger)3 FileNotFoundException (java.io.FileNotFoundException)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 PrintStream (java.io.PrintStream)2 HttpURLConnection (java.net.HttpURLConnection)2 ProtocolException (java.net.ProtocolException)2 URISyntaxException (java.net.URISyntaxException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 DefaultThreadContextStack (org.apache.logging.log4j.spi.DefaultThreadContextStack)2 NoOpThreadContextMap (org.apache.logging.log4j.spi.NoOpThreadContextMap)2