PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/apache-log4j-1.2.17/src/main/java/org/apache/log4j/pattern/PropertiesPatternConverter.java

#
Java | 104 lines | 48 code | 12 blank | 44 comment | 14 complexity | 3037e89e8bf8f5925a83f575b4901ddd MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.log4j.pattern;
  18. import org.apache.log4j.spi.LoggingEvent;
  19. import java.util.Iterator;
  20. import java.util.Set;
  21. import org.apache.log4j.helpers.*;
  22. /**
  23. * Able to handle the contents of the LoggingEvent's Property bundle and either
  24. * output the entire contents of the properties in a similar format to the
  25. * java.util.Hashtable.toString(), or to output the value of a specific key
  26. * within the property bundle
  27. * when this pattern converter has the option set.
  28. *
  29. * @author Paul Smith
  30. * @author Ceki Gülcü
  31. */
  32. public final class PropertiesPatternConverter
  33. extends LoggingEventPatternConverter {
  34. /**
  35. * Name of property to output.
  36. */
  37. private final String option;
  38. /**
  39. * Private constructor.
  40. * @param options options, may be null.
  41. */
  42. private PropertiesPatternConverter(
  43. final String[] options) {
  44. super(
  45. ((options != null) && (options.length > 0))
  46. ? ("Property{" + options[0] + "}") : "Properties", "property");
  47. if ((options != null) && (options.length > 0)) {
  48. option = options[0];
  49. } else {
  50. option = null;
  51. }
  52. }
  53. /**
  54. * Obtains an instance of PropertiesPatternConverter.
  55. * @param options options, may be null or first element contains name of property to format.
  56. * @return instance of PropertiesPatternConverter.
  57. */
  58. public static PropertiesPatternConverter newInstance(
  59. final String[] options) {
  60. return new PropertiesPatternConverter(options);
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
  66. // if there is no additional options, we output every single
  67. // Key/Value pair for the MDC in a similar format to Hashtable.toString()
  68. if (option == null) {
  69. toAppendTo.append("{");
  70. try {
  71. Set keySet = MDCKeySetExtractor.INSTANCE.getPropertyKeySet(event);
  72. if (keySet != null) {
  73. for (Iterator i = keySet.iterator(); i.hasNext();) {
  74. Object item = i.next();
  75. Object val = event.getMDC(item.toString());
  76. toAppendTo.append("{").append(item).append(",").append(val).append(
  77. "}");
  78. }
  79. }
  80. } catch(Exception ex) {
  81. LogLog.error("Unexpected exception while extracting MDC keys", ex);
  82. }
  83. toAppendTo.append("}");
  84. } else {
  85. // otherwise they just want a single key output
  86. Object val = event.getMDC(option);
  87. if (val != null) {
  88. toAppendTo.append(val);
  89. }
  90. }
  91. }
  92. }