View Javadoc
1   package com.github.triceo.splitlog.util;
2   
3   import org.slf4j.Logger;
4   
5   import com.github.triceo.splitlog.api.Message;
6   import com.github.triceo.splitlog.api.MessageDeliveryStatus;
7   import com.github.triceo.splitlog.api.MessageListener;
8   import com.github.triceo.splitlog.api.MessageProducer;
9   
10  public class LogUtil {
11  
12      public static enum Level {
13          DEBUG, ERROR, INFO, TRACE, WARNING;
14      }
15  
16      private static final Object[] NOARGS = new Object[0];
17      private static final String STATUS_TEMPLATE_NOPRODUCER = " {}\n\tMessage: {}\n\tWhere: {}";
18      private static final String STATUS_TEMPLATE_PRODUCER = LogUtil.STATUS_TEMPLATE_NOPRODUCER + "\n\tFrom: {}";
19  
20      public static void newMessage(final Logger logger, final Level level, final String note, final Message msg,
21          final MessageDeliveryStatus status, final MessageProducer<?> producer, final MessageListener<?> consumer) {
22          LogUtil.newMessage(logger, level, note, msg, status, producer, consumer, LogUtil.NOARGS);
23      }
24  
25      public static void newMessage(final Logger logger, final Level level, final String note, final Message msg,
26          final MessageDeliveryStatus status, final MessageProducer<?> producer, final MessageListener<?> consumer,
27          final Object... objects) {
28          final String result = note + LogUtil.STATUS_TEMPLATE_PRODUCER;
29          LogUtil.notify(logger, level, result, status, msg, consumer, producer, objects);
30      }
31  
32      public static void notify(final Logger logger, final Level level, final String note, final Object... objects) {
33          switch (level) {
34              case DEBUG:
35                  logger.debug(note, objects);
36                  break;
37              case ERROR:
38                  logger.error(note, objects);
39                  break;
40              case INFO:
41                  logger.info(note, objects);
42                  break;
43              case TRACE:
44                  logger.trace(note, objects);
45                  break;
46              case WARNING:
47                  logger.warn(note, objects);
48                  break;
49          }
50      }
51  }