Allow to configure logback logger with env variables (#8998)

Allow to configure logback logger with env variables (#8998)
6.19.x
Sergii Kabashniuk 2018-03-07 13:31:57 +02:00 committed by GitHub
parent dbdac2a604
commit e8750d6084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 129 additions and 0 deletions

View File

@ -56,6 +56,16 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-j2ee</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-logback</artifactId>
<exclusions>
<exclusion>
<artifactId>che-core-api-core</artifactId>
<groupId>org.eclipse.che.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
@ -112,6 +122,7 @@
<!-- dependency is required just to overlay it's content -->
<dep>org.eclipse.che:che-ide-gwt-app</dep>
<dep>org.eclipse.che.core:che-core-commons-j2ee</dep>
<dep>org.eclipse.che.core:che-core-commons-logback</dep>
<dep>ch.qos.logback:logback-classic</dep>
</ignoredUnusedDeclaredDependencies>
</configuration>

View File

@ -22,6 +22,14 @@
<packaging>jar</packaging>
<name>Che Core :: Commons :: Commons Logback</name>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>

View File

@ -0,0 +1,99 @@
/*
* Copyright (c) 2012-2018 Red Hat, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.commons.logback;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.LifeCycle;
import java.util.Arrays;
/**
* Class searches environment variable CHE_LOGGER_CONFIG Value of this variable is expected in such
* format:
*
* <p>CHE_LOGGER_CONFIG=logger1_name=logger1_level,logger2_name=logger2_level
*
* <p>In case if some logger name or logger level are omitted this pair will be silently ignored.
*/
public class EnvironmentVariablesLogLevelPropagator extends ContextAwareBase
implements LoggerContextListener, LifeCycle {
private boolean isStarted;
@Override
public void start() {
String config = System.getenv("CHE_LOGGER_CONFIG");
if (config != null && !config.isEmpty()) {
Arrays.stream(config.split(",")).map(String::trim).forEach(this::setLoggerLevel);
}
isStarted = true;
}
private void setLoggerLevel(String loggerConfig) {
String[] parts = loggerConfig.split("=", 2);
if (parts.length < 2) {
return;
}
String loggerName = parts[0];
String levelStr = parts[1];
if (levelStr.isEmpty() || loggerName.isEmpty()) {
return;
}
loggerName = loggerName.trim();
levelStr = levelStr.trim();
LoggerContext lc = (LoggerContext) context;
Logger logger = lc.getLogger(loggerName);
if ("null".equalsIgnoreCase(levelStr)) {
logger.setLevel(null);
} else {
Level level = Level.toLevel(levelStr, null);
if (level != null) {
logger.setLevel(level);
}
}
}
@Override
public void stop() {
isStarted = false;
}
@Override
public boolean isStarted() {
return isStarted;
}
@Override
public boolean isResetResistant() {
return false;
}
@Override
public void onStart(LoggerContext context) {}
@Override
public void onReset(LoggerContext context) {}
@Override
public void onStop(LoggerContext context) {}
@Override
public void onLevelChange(Logger logger, Level level) {}
}

View File

@ -91,6 +91,16 @@
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-lang</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-logback</artifactId>
<exclusions>
<exclusion>
<artifactId>che-core-api-core</artifactId>
<groupId>org.eclipse.che.core</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.eclipse.che.core</groupId>
<artifactId>che-core-commons-schedule</artifactId>
@ -162,6 +172,7 @@
<ignoredDependency>org.slf4j:jul-to-slf4j</ignoredDependency>
<ignoredDependency>org.slf4j:jcl-over-slf4j</ignoredDependency>
<ignoredDependency>org.slf4j:log4j-over-slf4j</ignoredDependency>
<ignoredDependency>org.eclipse.che.core:che-core-commons-logback</ignoredDependency>
</ignoredDependencies>
</configuration>
</execution>