diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2015-06-25 15:08:48 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2015-06-25 15:13:58 +0200 |
commit | 30aaeccec8ecf4668658a7151ad0418f86d2a6b3 (patch) | |
tree | 0e05513e5cbf355699931216df4b95c4a84fc610 /sonar-home/src/main | |
parent | 9c9d68f82fee5d020e4f9e990c8995bb1808e8ab (diff) | |
download | sonarqube-30aaeccec8ecf4668658a7151ad0418f86d2a6b3.tar.gz sonarqube-30aaeccec8ecf4668658a7151ad0418f86d2a6b3.zip |
SONAR-6648 Rework redirection of logs
Diffstat (limited to 'sonar-home/src/main')
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/FileCache.java | 31 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java | 19 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/Logger.java (renamed from sonar-home/src/main/java/org/sonar/home/log/Log.java) | 15 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java | 48 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java | 22 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/log/LogListener.java | 28 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/log/Slf4jLog.java | 62 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/log/StandardLog.java | 38 | ||||
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/log/package-info.java | 25 |
9 files changed, 62 insertions, 226 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java index 5998b97a2c5..3e83cd9d673 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java @@ -19,13 +19,10 @@ */ package org.sonar.home.cache; -import org.sonar.home.log.Log; - -import javax.annotation.CheckForNull; - import java.io.File; import java.io.IOException; import java.nio.file.Files; +import javax.annotation.CheckForNull; /** * This class is responsible for managing Sonar batch file cache. You can put file into cache and @@ -39,18 +36,18 @@ public class FileCache { private final File dir, tmpDir; private final FileHashes hashes; - private final Log log; + private final Logger logger; - FileCache(File dir, Log log, FileHashes fileHashes) { + FileCache(File dir, FileHashes fileHashes, Logger logger) { this.hashes = fileHashes; - this.log = log; - this.dir = createDir(dir, log, "user cache"); - log.info(String.format("User cache: %s", dir.getAbsolutePath())); - this.tmpDir = createDir(new File(dir, "_tmp"), log, "temp dir"); + this.logger = logger; + this.dir = createDir(dir, "user cache"); + logger.info(String.format("User cache: %s", dir.getAbsolutePath())); + this.tmpDir = createDir(new File(dir, "_tmp"), "temp dir"); } - public static FileCache create(File dir, Log log) { - return new FileCache(dir, log, new FileHashes()); + public static FileCache create(File dir, Logger logger) { + return new FileCache(dir, new FileHashes(), logger); } public File getDir() { @@ -67,7 +64,7 @@ public class FileCache { if (cachedFile.exists()) { return cachedFile; } - log.debug(String.format("No file found in the cache with name %s and hash %s", filename, hash)); + logger.debug(String.format("No file found in the cache with name %s and hash %s", filename, hash)); return null; } @@ -105,8 +102,8 @@ public class FileCache { boolean rename = sourceFile.renameTo(targetFile); // Check if the file was cached by another process during download if (!rename && !targetFile.exists()) { - log.warn(String.format("Unable to rename %s to %s", sourceFile.getAbsolutePath(), targetFile.getAbsolutePath())); - log.warn("A copy/delete will be tempted but with no guarantee of atomicity"); + logger.warn(String.format("Unable to rename %s to %s", sourceFile.getAbsolutePath(), targetFile.getAbsolutePath())); + logger.warn("A copy/delete will be tempted but with no guarantee of atomicity"); try { Files.move(sourceFile.toPath(), targetFile.toPath()); } catch (IOException e) { @@ -147,9 +144,9 @@ public class FileCache { throw new IllegalStateException("Failed to create directory in " + tmpDir); } - private File createDir(File dir, Log log, String debugTitle) { + private File createDir(File dir, String debugTitle) { if (!dir.isDirectory() || !dir.exists()) { - log.debug("Create : " + dir.getAbsolutePath()); + logger.debug("Create : " + dir.getAbsolutePath()); try { Files.createDirectories(dir.toPath()); } catch (IOException e) { diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java index 6ebb828ca5a..040ea887b8e 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCacheBuilder.java @@ -19,25 +19,20 @@ */ package org.sonar.home.cache; -import org.sonar.home.log.Log; -import org.sonar.home.log.StandardLog; - -import javax.annotation.Nullable; - import java.io.File; +import javax.annotation.Nullable; public class FileCacheBuilder { private File userHome; - private Log log = new StandardLog(); + private Logger logger; - public FileCacheBuilder setUserHome(File d) { - this.userHome = d; - return this; + public FileCacheBuilder(Logger logger) { + this.logger = logger; } - public FileCacheBuilder setLog(Log log) { - this.log = log; + public FileCacheBuilder setUserHome(File d) { + this.userHome = d; return this; } @@ -56,6 +51,6 @@ public class FileCacheBuilder { userHome = new File(path); } File cacheDir = new File(userHome, "cache"); - return FileCache.create(cacheDir, log); + return FileCache.create(cacheDir, logger); } } diff --git a/sonar-home/src/main/java/org/sonar/home/log/Log.java b/sonar-home/src/main/java/org/sonar/home/cache/Logger.java index bb67428ae98..fb4e8ebc2e0 100644 --- a/sonar-home/src/main/java/org/sonar/home/log/Log.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/Logger.java @@ -17,15 +17,18 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -package org.sonar.home.log; +package org.sonar.home.cache; -public interface Log { - void debug(String s); +public interface Logger { - void info(String s); + void debug(String msg); - void warn(String s); + void info(String msg); - void error(String s, Throwable throwable); + void warn(String msg); + + void error(String msg); + + void error(String msg, Throwable t); } diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java index 6af55ef6271..95b93e6ed27 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java @@ -19,12 +19,6 @@ */ package org.sonar.home.cache; -import org.sonar.home.log.Log; - -import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - import java.io.IOException; import java.io.RandomAccessFile; import java.io.UnsupportedEncodingException; @@ -39,10 +33,16 @@ import java.nio.file.attribute.BasicFileAttributes; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.concurrent.Callable; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; -import static java.nio.file.StandardOpenOption.*; +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; public class PersistentCache { + private static final Charset ENCODING = StandardCharsets.UTF_8; private static final String DIGEST_ALGO = "MD5"; private static final String LOCK_FNAME = ".lock"; @@ -51,23 +51,23 @@ public class PersistentCache { // eviction strategy is to expire entries after modification once a time duration has elapsed private final long defaultDurationToExpireMs; - private final Log log; private boolean forceUpdate; + private final Logger logger; - public PersistentCache(Path baseDir, long defaultDurationToExpireMs, Log log, boolean forceUpdate) { + public PersistentCache(Path baseDir, long defaultDurationToExpireMs, boolean forceUpdate, Logger logger) { this.baseDir = baseDir; this.defaultDurationToExpireMs = defaultDurationToExpireMs; - this.log = log; + this.logger = logger; reconfigure(forceUpdate); - log.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs); + logger.debug("cache: " + baseDir + ", default expiration time (ms): " + defaultDurationToExpireMs); } public void reconfigure(boolean forceUpdate) { this.forceUpdate = forceUpdate; if (forceUpdate) { - log.debug("cache: forcing update"); + logger.debug("cache: forcing update"); } try { @@ -115,13 +115,13 @@ public class PersistentCache { byte[] cached = getCache(key); if (cached != null) { - log.debug("cache hit for " + obj + " -> " + key); + logger.debug("cache hit for " + obj + " -> " + key); return cached; } - log.debug("cache miss for " + obj + " -> " + key); + logger.debug("cache miss for " + obj + " -> " + key); } else { - log.debug("cache force update for " + obj + " -> " + key); + logger.debug("cache force update for " + obj + " -> " + key); } if (valueLoader != null) { @@ -142,12 +142,12 @@ public class PersistentCache { * Deletes all cache entries */ public synchronized void clear() { - log.info("cache: clearing"); + logger.info("cache: clearing"); try { lock(); deleteCacheEntries(createClearFilter()); } catch (IOException e) { - log.error("Error clearing cache", e); + logger.error("Error clearing cache", e); } finally { unlock(); } @@ -157,12 +157,12 @@ public class PersistentCache { * Deletes cache entries that are no longer valid according to the default expiration time period. */ public synchronized void clean() { - log.info("cache: cleaning"); + logger.info("cache: cleaning"); try { lock(); deleteCacheEntries(createCleanFilter()); } catch (IOException e) { - log.error("Error cleaning cache", e); + logger.error("Error cleaning cache", e); } finally { unlock(); } @@ -183,21 +183,21 @@ public class PersistentCache { try { lock.release(); } catch (IOException e) { - log.error("Error releasing lock", e); + logger.error("Error releasing lock", e); } } if (lock_fc != null) { try { lock_fc.close(); } catch (IOException e) { - log.error("Error closing file channel", e); + logger.error("Error closing file channel", e); } } if (lock_raf != null) { try { lock_raf.close(); } catch (IOException e) { - log.error("Error closing file", e); + logger.error("Error closing file", e); } } @@ -222,7 +222,7 @@ public class PersistentCache { try { Files.delete(p); } catch (Exception e) { - log.error("Error deleting " + p, e); + logger.error("Error deleting " + p, e); } } } @@ -271,7 +271,7 @@ public class PersistentCache { } if (isCacheEntryExpired(cacheEntryPath, durationToExpireMs)) { - log.debug("cache: expiring entry"); + logger.debug("cache: expiring entry"); Files.delete(cacheEntryPath); return false; } diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java index c58dc53bbd6..e115f1cab64 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCacheBuilder.java @@ -19,35 +19,29 @@ */ package org.sonar.home.cache; -import org.sonar.home.log.StandardLog; - -import org.sonar.home.log.Log; - -import javax.annotation.Nullable; - import java.nio.file.Path; import java.nio.file.Paths; import java.util.concurrent.TimeUnit; +import javax.annotation.Nullable; public class PersistentCacheBuilder { private static final long DEFAULT_EXPIRE_DURATION = TimeUnit.MILLISECONDS.convert(1L, TimeUnit.DAYS); private static final String DIR_NAME = "ws_cache"; private boolean forceUpdate = false; - private Path cachePath = null; - private Log log = new StandardLog(); + private Path cachePath; + private final Logger logger; + + public PersistentCacheBuilder(Logger logger) { + this.logger = logger; + } public PersistentCache build() { if (cachePath == null) { setSonarHome(findHome()); } - return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, log, forceUpdate); - } - - public PersistentCacheBuilder setLog(Log log) { - this.log = log; - return this; + return new PersistentCache(cachePath, DEFAULT_EXPIRE_DURATION, forceUpdate, logger); } public PersistentCacheBuilder setSonarHome(@Nullable Path p) { diff --git a/sonar-home/src/main/java/org/sonar/home/log/LogListener.java b/sonar-home/src/main/java/org/sonar/home/log/LogListener.java deleted file mode 100644 index fa11374fdc4..00000000000 --- a/sonar-home/src/main/java/org/sonar/home/log/LogListener.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.home.log; - -public interface LogListener { - void log(String msg, Level level); - - enum Level { - ERROR, WARN, INFO, DEBUG, TRACE; - } -} diff --git a/sonar-home/src/main/java/org/sonar/home/log/Slf4jLog.java b/sonar-home/src/main/java/org/sonar/home/log/Slf4jLog.java deleted file mode 100644 index 03ff7029ff8..00000000000 --- a/sonar-home/src/main/java/org/sonar/home/log/Slf4jLog.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.home.log; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Slf4jLog implements Log { - - private final Logger logger; - - public Slf4jLog(Logger logger) { - this.logger = logger; - } - - public Slf4jLog(Class loggerClass) { - this.logger = LoggerFactory.getLogger(loggerClass); - } - - public boolean isDebugEnabled() { - return logger.isDebugEnabled(); - } - - @Override - public void debug(String s) { - logger.debug(s); - } - - @Override - public void info(String s) { - logger.info(s); - } - - @Override - public void warn(String s) { - logger.warn(s); - } - - @Override - public void error(String s, Throwable throwable) { - logger.error(s, throwable); - } - - -} diff --git a/sonar-home/src/main/java/org/sonar/home/log/StandardLog.java b/sonar-home/src/main/java/org/sonar/home/log/StandardLog.java deleted file mode 100644 index fa4402d15ca..00000000000 --- a/sonar-home/src/main/java/org/sonar/home/log/StandardLog.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.home.log; - -public class StandardLog implements Log { - @Override - public void debug(String s) { - } - - @Override - public void info(String s) { - } - - @Override - public void warn(String s) { - } - - @Override - public void error(String s, Throwable throwable) { - } -} diff --git a/sonar-home/src/main/java/org/sonar/home/log/package-info.java b/sonar-home/src/main/java/org/sonar/home/log/package-info.java deleted file mode 100644 index bbf1fe5f448..00000000000 --- a/sonar-home/src/main/java/org/sonar/home/log/package-info.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -@ParametersAreNonnullByDefault -package org.sonar.home.log; - -import javax.annotation.ParametersAreNonnullByDefault; - |