ZooTrace.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.yahoo.zookeeper.server;
  17. import org.apache.log4j.Logger;
  18. import com.yahoo.zookeeper.server.quorum.QuorumPacket;
  19. /**
  20. * This class encapsulates and centralizes tracing for the ZooKeeper server.
  21. * Trace messages go to the log with TRACE level.
  22. * <p>
  23. * Log4j must be correctly configured to capture the TRACE messages.
  24. */
  25. public class ZooTrace {
  26. final static public long CLIENT_REQUEST_TRACE_MASK = 1 << 1;
  27. final static public long CLIENT_DATA_PACKET_TRACE_MASK = 1 << 2;
  28. final static public long CLIENT_PING_TRACE_MASK = 1 << 3;
  29. final static public long SERVER_PACKET_TRACE_MASK = 1 << 4;
  30. final static public long SESSION_TRACE_MASK = 1 << 5;
  31. final static public long EVENT_DELIVERY_TRACE_MASK = 1 << 6;
  32. final static public long SERVER_PING_TRACE_MASK = 1 << 7;
  33. final static public long WARNING_TRACE_MASK = 1 << 8;
  34. final static public long JMX_TRACE_MASK = 1 << 9;
  35. private static long traceMask = CLIENT_REQUEST_TRACE_MASK
  36. | SERVER_PACKET_TRACE_MASK | SESSION_TRACE_MASK
  37. | WARNING_TRACE_MASK;
  38. public static long getTextTraceLevel() {
  39. return traceMask;
  40. }
  41. public static void setTextTraceLevel(long mask) {
  42. traceMask = mask;
  43. Logger LOG = Logger.getLogger(ZooTrace.class);
  44. LOG.info("Set text trace mask to " + Long.toHexString(mask));
  45. }
  46. public static boolean isTraceEnabled(Logger log, long mask) {
  47. return log.isTraceEnabled() && (mask & traceMask) != 0;
  48. }
  49. public static void logTraceMessage(Logger log, long mask, String msg) {
  50. if (isTraceEnabled(log, mask)) {
  51. log.trace(msg);
  52. }
  53. }
  54. static public void logQuorumPacket(Logger log, long mask,
  55. char direction, QuorumPacket qp)
  56. {
  57. return;
  58. // if (isTraceEnabled(log, mask)) {
  59. // logTraceMessage(LOG, mask, direction + " "
  60. // + FollowerHandler.packetToString(qp));
  61. // }
  62. }
  63. static public void logRequest(Logger log, long mask,
  64. char rp, Request request, String header)
  65. {
  66. if (isTraceEnabled(log, mask)) {
  67. log.trace(header + ":" + rp + request.toString());
  68. }
  69. }
  70. }