You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoggingConfiguration.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.batch.bootstrapper;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import javax.annotation.Nullable;
  24. import org.apache.commons.lang.StringUtils;
  25. /**
  26. * @since 2.14
  27. */
  28. public final class LoggingConfiguration {
  29. public static final String PROPERTY_ROOT_LOGGER_LEVEL = "ROOT_LOGGER_LEVEL";
  30. public static final String PROPERTY_FORMAT = "FORMAT";
  31. public static final String LEVEL_ROOT_VERBOSE = "DEBUG";
  32. public static final String LEVEL_ROOT_DEFAULT = "INFO";
  33. static final String FORMAT_DEFAULT = "%d{HH:mm:ss.SSS} %-5level - %msg%n";
  34. static final String FORMAT_MAVEN = "[%level] [%d{HH:mm:ss.SSS}] %msg%n";
  35. private Map<String, String> substitutionVariables = new HashMap<>();
  36. private LogOutput logOutput = null;
  37. private boolean verbose;
  38. public LoggingConfiguration() {
  39. this(null);
  40. }
  41. public LoggingConfiguration(@Nullable EnvironmentInformation environment) {
  42. setVerbose(false);
  43. if (environment != null && "maven".equalsIgnoreCase(environment.getKey())) {
  44. setFormat(FORMAT_MAVEN);
  45. } else {
  46. setFormat(FORMAT_DEFAULT);
  47. }
  48. }
  49. public LoggingConfiguration setProperties(Map<String, String> properties) {
  50. setVerbose(properties);
  51. return this;
  52. }
  53. public LoggingConfiguration setLogOutput(@Nullable LogOutput listener) {
  54. this.logOutput = listener;
  55. return this;
  56. }
  57. public LoggingConfiguration setVerbose(boolean verbose) {
  58. return setRootLevel(verbose ? LEVEL_ROOT_VERBOSE : LEVEL_ROOT_DEFAULT);
  59. }
  60. public boolean isVerbose() {
  61. return verbose;
  62. }
  63. public LoggingConfiguration setVerbose(Map<String, String> props) {
  64. String logLevel = props.get("sonar.log.level");
  65. String deprecatedProfilingLevel = props.get("sonar.log.profilingLevel");
  66. verbose = "true".equals(props.get("sonar.verbose")) ||
  67. "DEBUG".equals(logLevel) || "TRACE".equals(logLevel) ||
  68. "BASIC".equals(deprecatedProfilingLevel) || "FULL".equals(deprecatedProfilingLevel);
  69. return setVerbose(verbose);
  70. }
  71. public LoggingConfiguration setRootLevel(String level) {
  72. return addSubstitutionVariable(PROPERTY_ROOT_LOGGER_LEVEL, level);
  73. }
  74. LoggingConfiguration setFormat(String format) {
  75. return addSubstitutionVariable(PROPERTY_FORMAT, StringUtils.defaultIfBlank(format, FORMAT_DEFAULT));
  76. }
  77. private LoggingConfiguration addSubstitutionVariable(String key, String value) {
  78. substitutionVariables.put(key, value);
  79. return this;
  80. }
  81. String getSubstitutionVariable(String key) {
  82. return substitutionVariables.get(key);
  83. }
  84. Map<String, String> getSubstitutionVariables() {
  85. return substitutionVariables;
  86. }
  87. LogOutput getLogOutput() {
  88. return logOutput;
  89. }
  90. }