View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.spi;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.status.ErrorStatus;
18  import ch.qos.logback.core.status.InfoStatus;
19  import ch.qos.logback.core.status.Status;
20  import ch.qos.logback.core.status.StatusManager;
21  import ch.qos.logback.core.status.WarnStatus;
22  
23  /**
24   * A helper class that implements ContextAware methods. A class can implement
25   * the ContextAware interface by deriving from this class.
26   * 
27   * @author Ceki Gülcü
28   */
29  public class ContextAwareBase implements ContextAware {
30      private int noContextWarning = 0;
31      protected Context context;
32      final Object declaredOrigin;
33  
34      public ContextAwareBase() {
35          declaredOrigin = this;
36      }
37  
38      public ContextAwareBase(ContextAware declaredOrigin) {
39          this.declaredOrigin = declaredOrigin;
40      }
41  
42      public void setContext(Context context) {
43          if (this.context == null) {
44              this.context = context;
45          } else if (this.context != context) {
46              throw new IllegalStateException("Context has been already set");
47          }
48      }
49  
50      public Context getContext() {
51          return this.context;
52      }
53  
54      public StatusManager getStatusManager() {
55          if (context == null) {
56              return null;
57          }
58          return context.getStatusManager();
59      }
60  
61      /**
62       * The declared origin of status messages. By default 'this'. Derived classes
63       * may override this method to declare other origin.
64       * 
65       * @return the declared origin, by default 'this'
66       */
67      protected Object getDeclaredOrigin() {
68          return declaredOrigin;
69      }
70  
71      public void addStatus(Status status) {
72          if (context == null) {
73              if (noContextWarning++ == 0) {
74                  System.out.println("LOGBACK: No context given for " + this);
75              }
76              return;
77          }
78          StatusManager sm = context.getStatusManager();
79          if (sm != null) {
80              sm.add(status);
81          }
82      }
83  
84      public void addInfo(String msg) {
85          addStatus(new InfoStatus(msg, getDeclaredOrigin()));
86      }
87  
88      public void addInfo(String msg, Throwable ex) {
89          addStatus(new InfoStatus(msg, getDeclaredOrigin(), ex));
90      }
91  
92      public void addWarn(String msg) {
93          addStatus(new WarnStatus(msg, getDeclaredOrigin()));
94      }
95  
96      public void addWarn(String msg, Throwable ex) {
97          addStatus(new WarnStatus(msg, getDeclaredOrigin(), ex));
98      }
99  
100     public void addError(String msg) {
101         addStatus(new ErrorStatus(msg, getDeclaredOrigin()));
102     }
103 
104     public void addError(String msg, Throwable ex) {
105         addStatus(new ErrorStatus(msg, getDeclaredOrigin(), ex));
106     }
107 }