Skip to content

Commit

Permalink
SONAR-5700 ability to change level of loggers of 3rd-party libraries
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Feb 19, 2015
1 parent 97fad28 commit 6adb553
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 118 deletions.

This file was deleted.

Expand Up @@ -204,8 +204,7 @@ public FileAppender createAppender(String appenderName) {
FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy(); FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
rollingPolicy.setContext(context); rollingPolicy.setContext(context);
rollingPolicy.setFileNamePattern(StringUtils.replace(filePath, filenamePrefix + ".log", filenamePrefix + ".%i.log")); rollingPolicy.setFileNamePattern(StringUtils.replace(filePath, filenamePrefix + ".log", filenamePrefix + ".%i.log"));
rollingPolicy.setMaxIndex(1); rollingPolicy.setMinIndex(1);
rollingPolicy.setMaxIndex(maxFiles);
rollingPolicy.setMaxIndex(maxFiles); rollingPolicy.setMaxIndex(maxFiles);
rollingPolicy.setParent(appender); rollingPolicy.setParent(appender);
rollingPolicy.start(); rollingPolicy.start();
Expand Down
9 changes: 4 additions & 5 deletions sonar-application/src/main/assembly/conf/sonar.properties
Expand Up @@ -245,10 +245,7 @@
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# LOGGING # LOGGING


# Level of information displayed in the logs: NONE (default), BASIC (functional information) # Enable debug logs in file sonar.log.
# and FULL (functional and technical details)
#sonar.log.profilingLevel=NONE

#sonar.log.debug=false #sonar.log.debug=false


# Path to log files. Can be absolute or relative to installation directory. # Path to log files. Can be absolute or relative to installation directory.
Expand All @@ -262,7 +259,9 @@
# - disabled if value is "none". That needs logs to be managed by an external system like logrotate. # - disabled if value is "none". That needs logs to be managed by an external system like logrotate.
#sonar.log.rollingPolicy=time:yyyy-MM-dd #sonar.log.rollingPolicy=time:yyyy-MM-dd


# Maximum number of files to keep if a rolling policy is enabled # Maximum number of files to keep if a rolling policy is enabled.
# - maximum value is 20 on size rolling policy
# - unlimited on time rolling policy. Set to zero to disable old file purging.
#sonar.log.maxFiles=7 #sonar.log.maxFiles=7


# Access log is the list of all the HTTP requests received by server. If enabled, it is stored # Access log is the list of all the HTTP requests received by server. If enabled, it is stored
Expand Down
5 changes: 5 additions & 0 deletions sonar-plugin-api/pom.xml
Expand Up @@ -155,6 +155,11 @@
<artifactId>logback-classic</artifactId> <artifactId>logback-classic</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<optional>true</optional>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
Expand Down
Expand Up @@ -35,7 +35,7 @@ class ConsoleLogger extends BaseLogger {


private final PrintStream stream; private final PrintStream stream;


ConsoleLogger(String unusedName) { ConsoleLogger() {
this.stream = System.out; this.stream = System.out;
} }


Expand Down Expand Up @@ -142,6 +142,11 @@ public void doError(String msg, Throwable thrown) {
thrown.printStackTrace(); thrown.printStackTrace();
} }


@Override
public boolean setLevel(LoggerLevel level) {
return false;
}

private void log(String level, String msg) { private void log(String level, String msg) {
this.stream.println(String.format("%s %s", level, msg)); this.stream.println(String.format("%s %s", level, msg));
} }
Expand Down
Expand Up @@ -25,7 +25,7 @@ class ConsoleLoggers extends Loggers {


@Override @Override
protected Logger newInstance(String name) { protected Logger newInstance(String name) {
return new ConsoleLogger(name); return new ConsoleLogger();
} }


@Override @Override
Expand Down
Expand Up @@ -29,7 +29,7 @@
* This JUnit rule allows to configure and access logs in tests. By default * This JUnit rule allows to configure and access logs in tests. By default
* debug logs are enabled. * debug logs are enabled.
* <p/> * <p/>
* Warning - not compatible with parallel execution of tests. * Warning - not compatible with parallel execution of tests in the same JVM fork.
* <p/> * <p/>
* Example: * Example:
* <pre> * <pre>
Expand Down
Expand Up @@ -19,106 +19,132 @@
*/ */
package org.sonar.api.utils.log; package org.sonar.api.utils.log;


import ch.qos.logback.classic.*;
import ch.qos.logback.classic.Logger;

import javax.annotation.Nullable; import javax.annotation.Nullable;


/** /**
* Note that logback is accessed through SLF4J * Logback is used in production.
*/ */
class LogbackLogger extends BaseLogger { class LogbackLogger extends BaseLogger {


private final transient org.slf4j.Logger slf4j; private final transient ch.qos.logback.classic.Logger logback;


LogbackLogger(org.slf4j.Logger slf4j) { LogbackLogger(ch.qos.logback.classic.Logger logback) {
this.slf4j = slf4j; this.logback = logback;
} }


@Override @Override
public boolean isDebugEnabled() { public boolean isDebugEnabled() {
return slf4j.isDebugEnabled(); return logback.isDebugEnabled();
} }


@Override @Override
protected void doDebug(String msg) { protected void doDebug(String msg) {
slf4j.debug(msg); logback.debug(msg);
} }


@Override @Override
protected void doDebug(String msg, @Nullable Object arg) { protected void doDebug(String msg, @Nullable Object arg) {
slf4j.debug(msg, arg); logback.debug(msg, arg);
} }


@Override @Override
protected void doDebug(String msg, @Nullable Object arg1, @Nullable Object arg2) { protected void doDebug(String msg, @Nullable Object arg1, @Nullable Object arg2) {
slf4j.debug(msg, arg1, arg2); logback.debug(msg, arg1, arg2);
} }


@Override @Override
protected void doDebug(String msg, Object... args) { protected void doDebug(String msg, Object... args) {
slf4j.debug(msg, args); logback.debug(msg, args);
} }


@Override @Override
protected void doInfo(String msg) { protected void doInfo(String msg) {
slf4j.info(msg); logback.info(msg);
} }


@Override @Override
protected void doInfo(String msg, @Nullable Object arg) { protected void doInfo(String msg, @Nullable Object arg) {
slf4j.info(msg, arg); logback.info(msg, arg);
} }


@Override @Override
protected void doInfo(String msg, @Nullable Object arg1, @Nullable Object arg2) { protected void doInfo(String msg, @Nullable Object arg1, @Nullable Object arg2) {
slf4j.info(msg, arg1, arg2); logback.info(msg, arg1, arg2);
} }


@Override @Override
protected void doInfo(String msg, Object... args) { protected void doInfo(String msg, Object... args) {
slf4j.info(msg, args); logback.info(msg, args);
} }


@Override @Override
protected void doWarn(String msg) { protected void doWarn(String msg) {
slf4j.warn(msg); logback.warn(msg);
} }


@Override @Override
protected void doWarn(String msg, @Nullable Object arg) { protected void doWarn(String msg, @Nullable Object arg) {
slf4j.warn(msg, arg); logback.warn(msg, arg);
} }


@Override @Override
protected void doWarn(String msg, @Nullable Object arg1, @Nullable Object arg2) { protected void doWarn(String msg, @Nullable Object arg1, @Nullable Object arg2) {
slf4j.warn(msg, arg1, arg2); logback.warn(msg, arg1, arg2);
} }


@Override @Override
protected void doWarn(String msg, Object... args) { protected void doWarn(String msg, Object... args) {
slf4j.warn(msg, args); logback.warn(msg, args);
} }


@Override @Override
protected void doError(String msg) { protected void doError(String msg) {
slf4j.error(msg); logback.error(msg);
} }


@Override @Override
protected void doError(String msg, @Nullable Object arg) { protected void doError(String msg, @Nullable Object arg) {
slf4j.error(msg, arg); logback.error(msg, arg);
} }


@Override @Override
protected void doError(String msg, @Nullable Object arg1, @Nullable Object arg2) { protected void doError(String msg, @Nullable Object arg1, @Nullable Object arg2) {
slf4j.error(msg, arg1, arg2); logback.error(msg, arg1, arg2);
} }


@Override @Override
protected void doError(String msg, Object... args) { protected void doError(String msg, Object... args) {
slf4j.error(msg, args); logback.error(msg, args);
} }


@Override @Override
protected void doError(String msg, Throwable thrown) { protected void doError(String msg, Throwable thrown) {
slf4j.error(msg, thrown); logback.error(msg, thrown);
}

@Override
public boolean setLevel(LoggerLevel level) {
switch (level) {
case DEBUG:
logback.setLevel(Level.DEBUG);
break;
case INFO:
logback.setLevel(Level.INFO);
break;
case WARN:
logback.setLevel(Level.WARN);
break;
case ERROR:
logback.setLevel(Level.ERROR);
break;
}
return true;
}

Logger logbackLogger() {
return logback;
} }
} }
Expand Up @@ -30,7 +30,8 @@ class LogbackLoggers extends Loggers {


@Override @Override
protected Logger newInstance(String name) { protected Logger newInstance(String name) {
return new LogbackLogger(LoggerFactory.getLogger(name)); // logback is accessed through SLF4J
return new LogbackLogger((ch.qos.logback.classic.Logger)LoggerFactory.getLogger(name));
} }


@Override @Override
Expand Down

0 comments on commit 6adb553

Please sign in to comment.