]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2719 add timestamp to maven logs
authorsimonbrandhof <simon.brandhof@gmail.com>
Sun, 9 Oct 2011 19:59:39 +0000 (21:59 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Sun, 9 Oct 2011 20:31:55 +0000 (22:31 +0200)
sonar-batch/src/main/resources/org/sonar/batch/logback-base.xml [new file with mode: 0644]
sonar-batch/src/main/resources/org/sonar/batch/logback.xml
sonar-core/pom.xml
sonar-core/src/main/java/org/sonar/core/config/Logback.java [new file with mode: 0644]
sonar-maven-plugin/src/main/java/org/sonar/maven/SonarMojo.java
sonar-maven-plugin/src/main/resources/org/sonar/maven/logback.xml [new file with mode: 0644]
sonar-maven3-plugin/src/main/java/org/sonar/maven3/SonarMojo.java
sonar-maven3-plugin/src/main/resources/org/sonar/maven3/logback.xml [new file with mode: 0644]
sonar-server/pom.xml
sonar-server/src/main/java/org/sonar/server/platform/Logback.java [deleted file]
sonar-server/src/main/java/org/sonar/server/platform/PlatformLifecycleListener.java

diff --git a/sonar-batch/src/main/resources/org/sonar/batch/logback-base.xml b/sonar-batch/src/main/resources/org/sonar/batch/logback-base.xml
new file mode 100644 (file)
index 0000000..618c62a
--- /dev/null
@@ -0,0 +1,33 @@
+<included>
+  <logger name="org.hibernate">
+    <level value="WARN"/>
+  </logger>
+
+  <!-- set DEBUG to activate SQL logs. NOT RECOMMENDED  -->
+  <logger name="org.hibernate.SQL">
+    <level value="ERROR"/>
+  </logger>
+
+  <!-- set INFO to activate SQL statistics. NOT RECOMMENDED  -->
+  <logger name="org.sonar.DBSTATISTICS">
+    <level value="ERROR"/>
+  </logger>
+
+  <logger name="net.sf.ehcache">
+    <level value="WARN"/>
+  </logger>
+
+  <logger name="org.hibernate.cache.ReadWriteCache">
+    <!-- removing "An item was expired by the cache while it was locked (increase your cache timeout)" msg -->
+    <level value="ERROR"/>
+  </logger>
+  <logger name="org.hibernate.cache.EhCacheProvider">
+    <!-- removing "org.hibernate.cache.EhCacheProvider - Could not find configuratio)" message -->
+    <level value="ERROR"/>
+  </logger>
+
+  <root>
+    <level value="${ROOT_LOGGER_LEVEL}"/>
+    <appender-ref ref="STDOUT"/>
+  </root>
+</included>
\ No newline at end of file
index 91b34037ace613245c84989ec3da4b9bbb0e1422..2e65d27a617721492bdc8338922791d32519603b 100644 (file)
@@ -1,11 +1,16 @@
 <?xml version="1.0" encoding="UTF-8" ?>
-
 <configuration debug="false">
 
+  <!--
+  This file is directly loaded by Sonar Ant Task 1.1 and Sonar Runner 1.1.
+  Inclusion of logback-base.xml is not supported by these 2 versions, so loggers must
+  be duplicated.
+  -->
+
   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
-    <layout class="ch.qos.logback.classic.PatternLayout">
-      <pattern>[%level] %X{module} %msg%n</pattern>
-    </layout>
+    <encoder>
+      <pattern>%d{HH:mm:ss.SSS} %-5level %msg%n</pattern>
+    </encoder>
   </appender>
 
   <logger name="org.hibernate">
     <level value="ERROR"/>
   </logger>
 
-  <!-- see org.sonar.mojo.InternalMojo#initLogging -->
   <root>
     <level value="${ROOT_LOGGER_LEVEL}"/>
     <appender-ref ref="STDOUT"/>
   </root>
-
+  
 </configuration>
\ No newline at end of file
index 270de36a396e32c47943676cec0043ee3acb7495..302e57dfe683ea34e0056094024a4cf749e091e2 100644 (file)
       <artifactId>commons-dbcp</artifactId>
     </dependency>
 
+    <!-- logging -->
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>ch.qos.logback</groupId>
+      <artifactId>logback-classic</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>ch.qos.logback</groupId>
+      <artifactId>logback-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jcl-over-slf4j</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>log4j-over-slf4j</artifactId>
+    </dependency>
+
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <artifactId>hamcrest-all</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>ch.qos.logback</groupId>
-      <artifactId>logback-classic</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.dbunit</groupId>
       <artifactId>dbunit</artifactId>
diff --git a/sonar-core/src/main/java/org/sonar/core/config/Logback.java b/sonar-core/src/main/java/org/sonar/core/config/Logback.java
new file mode 100644 (file)
index 0000000..ceb1cc0
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.config;
+
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
+import ch.qos.logback.core.util.StatusPrinter;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.IOUtils;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+/**
+ * Configure Logback
+ *
+ * @since 2.12
+ */
+public final class Logback {
+
+  public static void configure(String classloaderPath) {
+    InputStream input = Logback.class.getResourceAsStream(classloaderPath);
+    if (input == null) {
+      throw new IllegalArgumentException("Logback configuration not found in classloader: " + classloaderPath);
+    }
+    configure(input);
+  }
+
+  public static void configure(File logbackFile) {
+    try {
+      FileInputStream input = FileUtils.openInputStream(logbackFile);
+      configure(input);
+    } catch (IOException e) {
+      throw new IllegalArgumentException("Fail to load the Logback configuration: " + logbackFile, e);
+    }
+  }
+
+  /**
+   * Note that this method closes the input stream
+   */
+  private static void configure(InputStream input) {
+    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
+    try {
+      JoranConfigurator configurator = new JoranConfigurator();
+      configurator.setContext(lc);
+      lc.reset();
+      configurator.doConfigure(input);
+    } catch (JoranException e) {
+      // StatusPrinter will handle this
+    } finally {
+      IOUtils.closeQuietly(input);
+    }
+    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
+  }
+}
index 6b78f9b463dee9884782d2c54228e63ba99b2c66..89b5190ba97da3a56e9fac9b5b08da7ff030dc5b 100644 (file)
  */
 package org.sonar.maven;
 
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.joran.JoranConfigurator;
-import ch.qos.logback.core.joran.spi.JoranException;
-import org.apache.commons.configuration.*;
-import org.apache.commons.io.IOUtils;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -38,13 +33,12 @@ import org.apache.maven.plugin.PluginManager;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectBuilder;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
-import org.slf4j.LoggerFactory;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.batch.Batch;
 import org.sonar.batch.MavenProjectConverter;
 import org.sonar.batch.bootstrapper.EnvironmentInformation;
-import java.io.InputStream;
+import org.sonar.core.config.Logback;
 
 /**
  * @goal sonar
@@ -81,7 +75,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact factory to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -90,7 +84,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact repository to use.
-   * 
+   *
    * @parameter expression="${localRepository}"
    * @required
    * @readonly
@@ -99,7 +93,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact metadata source to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -108,7 +102,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact collector to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -117,7 +111,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The dependency tree builder to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -139,7 +133,7 @@ public final class SonarMojo extends AbstractMojo {
   private RuntimeInformation runtimeInformation;
 
   public void execute() throws MojoExecutionException, MojoFailureException {
-    initLogging();
+    configureLogback();
     executeBatch();
   }
 
@@ -159,29 +153,8 @@ public final class SonarMojo extends AbstractMojo {
     return new EnvironmentInformation("Maven", mavenVersion);
   }
 
-  private void initLogging() throws MojoExecutionException {
-    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
-    JoranConfigurator jc = new JoranConfigurator();
-    jc.setContext(context);
-    context.reset();
-    InputStream input = getClass().getResourceAsStream("/org/sonar/batch/logback.xml");
+  private void configureLogback() {
     System.setProperty("ROOT_LOGGER_LEVEL", getLog().isDebugEnabled() ? "DEBUG" : "INFO");
-    try {
-      jc.doConfigure(input);
-
-    } catch (JoranException e) {
-      throw new MojoExecutionException("can not initialize logging", e);
-
-    } finally {
-      IOUtils.closeQuietly(input);
-    }
-  }
-
-  private Configuration getInitialConfiguration() {
-    CompositeConfiguration configuration = new CompositeConfiguration();
-    configuration.addConfiguration(new SystemConfiguration());
-    configuration.addConfiguration(new EnvironmentConfiguration());
-    configuration.addConfiguration(new MapConfiguration(project.getModel().getProperties()));
-    return configuration;
+    Logback.configure("/org/sonar/maven/logback.xml");
   }
 }
diff --git a/sonar-maven-plugin/src/main/resources/org/sonar/maven/logback.xml b/sonar-maven-plugin/src/main/resources/org/sonar/maven/logback.xml
new file mode 100644 (file)
index 0000000..585dc31
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<configuration debug="false">
+
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+    <encoder>
+      <pattern>[%level] %d{HH:mm:ss.SSS} %msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <!-- Include definition of loggers. The appender STDOUT is not shared in sonar-batch because
+   format is slightly different between Maven and Ant logs -->
+  <include resource="/org/sonar/batch/logback-base.xml"/>
+
+</configuration>
\ No newline at end of file
index 001378c7bb7ed542996d91c14afcc080d5aa4471..eabc913e42aaba29330cff2561654add6a72326b 100644 (file)
  */
 package org.sonar.maven3;
 
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.joran.JoranConfigurator;
-import ch.qos.logback.core.joran.spi.JoranException;
-import org.apache.commons.io.IOUtils;
 import org.apache.maven.artifact.factory.ArtifactFactory;
 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
 import org.apache.maven.artifact.repository.ArtifactRepository;
@@ -36,14 +32,12 @@ import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.apache.maven.project.MavenProjectBuilder;
 import org.apache.maven.shared.dependency.tree.DependencyTreeBuilder;
-import org.slf4j.LoggerFactory;
 import org.sonar.api.batch.bootstrap.ProjectDefinition;
 import org.sonar.api.batch.bootstrap.ProjectReactor;
 import org.sonar.batch.Batch;
 import org.sonar.batch.MavenProjectConverter;
 import org.sonar.batch.bootstrapper.EnvironmentInformation;
-
-import java.io.InputStream;
+import org.sonar.core.config.Logback;
 
 /**
  * @goal sonar
@@ -74,7 +68,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact factory to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -83,7 +77,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact repository to use.
-   * 
+   *
    * @parameter expression="${localRepository}"
    * @required
    * @readonly
@@ -92,7 +86,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact metadata source to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -101,7 +95,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The artifact collector to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -110,7 +104,7 @@ public final class SonarMojo extends AbstractMojo {
 
   /**
    * The dependency tree builder to use.
-   * 
+   *
    * @component
    * @required
    * @readonly
@@ -132,7 +126,7 @@ public final class SonarMojo extends AbstractMojo {
   private RuntimeInformation runtimeInformation;
 
   public void execute() throws MojoExecutionException, MojoFailureException {
-    initLogging();
+    configureLogback();
     executeBatch();
   }
 
@@ -152,22 +146,8 @@ public final class SonarMojo extends AbstractMojo {
     return new EnvironmentInformation("Maven", mavenVersion);
   }
 
-  private void initLogging() throws MojoExecutionException {
-    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
-    JoranConfigurator jc = new JoranConfigurator();
-    jc.setContext(context);
-    context.reset();
-    InputStream input = getClass().getResourceAsStream("/org/sonar/batch/logback.xml");
+  private void configureLogback() {
     System.setProperty("ROOT_LOGGER_LEVEL", getLog().isDebugEnabled() ? "DEBUG" : "INFO");
-    try {
-      jc.doConfigure(input);
-
-    } catch (JoranException e) {
-      throw new MojoExecutionException("can not initialize logging", e);
-
-    } finally {
-      IOUtils.closeQuietly(input);
-    }
+    Logback.configure("/org/sonar/maven3/logback.xml");
   }
-
 }
diff --git a/sonar-maven3-plugin/src/main/resources/org/sonar/maven3/logback.xml b/sonar-maven3-plugin/src/main/resources/org/sonar/maven3/logback.xml
new file mode 100644 (file)
index 0000000..585dc31
--- /dev/null
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<configuration debug="false">
+
+  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+    <encoder>
+      <pattern>[%level] %d{HH:mm:ss.SSS} %msg%n</pattern>
+    </encoder>
+  </appender>
+
+  <!-- Include definition of loggers. The appender STDOUT is not shared in sonar-batch because
+   format is slightly different between Maven and Ant logs -->
+  <include resource="/org/sonar/batch/logback-base.xml"/>
+
+</configuration>
\ No newline at end of file
index 4cd62ce8577691aa4f3711e592b4c127097233c9..11343cbd4020f4cc84f63dd99d4476fd8b303f39 100644 (file)
       <groupId>jfree</groupId>
       <artifactId>jfreechart</artifactId>
     </dependency>
-
-    <!-- logging -->
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
     </dependency>
-    <dependency>
-      <groupId>ch.qos.logback</groupId>
-      <artifactId>logback-classic</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>ch.qos.logback</groupId>
-      <artifactId>logback-core</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>jcl-over-slf4j</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>log4j-over-slf4j</artifactId>
-    </dependency>
-
-
     <dependency>
       <groupId>commons-dbcp</groupId>
       <artifactId>commons-dbcp</artifactId>
diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Logback.java b/sonar-server/src/main/java/org/sonar/server/platform/Logback.java
deleted file mode 100644 (file)
index eec641e..0000000
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.server.platform;
-
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.joran.JoranConfigurator;
-import ch.qos.logback.core.joran.spi.JoranException;
-import ch.qos.logback.core.util.StatusPrinter;
-import org.slf4j.LoggerFactory;
-
-import java.io.File;
-
-/**
- * Configure Logback with $SONAR_HOME/conf/logback.xml
- *
- * @since 2.12
- */
-final class Logback {
-
-  static void configure() {
-    configure(new File(SonarHome.getHome(), "conf/logback.xml"));
-  }
-
-  static void configure(File logbackFile) {
-    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
-    try {
-      JoranConfigurator configurator = new JoranConfigurator();
-      configurator.setContext(lc);
-      lc.reset();
-      configurator.doConfigure(logbackFile);
-    } catch (JoranException e) {
-      // StatusPrinter will handle this
-    }
-    StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
-  }
-}
index 10dcb45375cb0c3ce2e473e3a3bd2ffcdedb7991..1837223ae1b0bc4b30c6ca61f574b1f7bcf2db23 100644 (file)
  */
 package org.sonar.server.platform;
 
+import org.sonar.core.config.Logback;
+
 import javax.servlet.ServletContextEvent;
 import javax.servlet.ServletContextListener;
+import java.io.File;
 
 public final class PlatformLifecycleListener implements ServletContextListener {
 
   public void contextInitialized(ServletContextEvent event) {
-    Logback.configure();
+    configureLogback();
     Platform.getInstance().init(event.getServletContext());
     Platform.getInstance().start();
   }
@@ -33,4 +36,11 @@ public final class PlatformLifecycleListener implements ServletContextListener {
   public void contextDestroyed(ServletContextEvent event) {
     Platform.getInstance().stop();
   }
+
+  /**
+   * Configure Logback with $SONAR_HOME/conf/logback.xml
+   */
+  private void configureLogback() {
+    Logback.configure(new File(SonarHome.getHome(), "conf/logback.xml"));
+  }
 }