-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to configure logback logger with env variables #8998
Changes from all commits
7859a5a
9699e6f
73d1cdf
58f03be
04a6bef
92a571b
10e7091
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate on how it is used and why it should not be thread safe? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is implemented in similar to ch.qos.logback.classic.jul.LevelChangePropagator way |
||
|
||
@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]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it makes sense to trim values here |
||
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) {} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it excluded?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to make sure ide war doesn't contain che-core-api-core and all it unnecessary dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a bit strange. It means that you're using classes from this dependency which don't use
api-core
classes.But somebody can easily use it, and then Runtime exception will occur.
It's just thought. I'm OK to leave it as it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need logback in IDE war at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sleshchenko about exclusion.
We already have che-core-commons-logback module. To avoid exclusion we need another module with only one single class. When I selecting this approach I thought it would be less confusing then to have two modules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@garagatyi
Some filters are using slf4j-> logback logging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it is more convenient right now, but we will have to remember that this exclusion is needed. And other projects that would extend Che also would need to know about this exclusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would not be a problem soon. Since that is not a case for Theia.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ok with this approach, just thinking out loud