View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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    public ContextAwareBase(Object declaredOrigin) {
38      this.declaredOrigin = declaredOrigin;
39    }
40  
41    public void setContext(Context context) {
42      if (this.context == null) {
43        this.context = context;
44      } else if (this.context != context) {
45        throw new IllegalStateException("Context has been already set");
46      }
47    }
48  
49    public Context getContext() {
50      return this.context;
51    }
52  
53    public StatusManager getStatusManager() {
54      if (context == null) {
55        return null;
56      }
57      return context.getStatusManager();
58    }
59  
60    /**
61     * The declared origin of status messages. By default 'this'. Derived classes may override this
62     * method to declare other origin.
63     * 
64     * @return the declared origin, by default 'this'
65     */ 
66    protected Object getDeclaredOrigin() {
67      return declaredOrigin;
68    }
69  
70    public void addStatus(Status status) {
71      if (context == null) {
72        if (noContextWarning++ == 0) {
73          System.out.println("LOGBACK: No context given for " + this);
74        }
75        return;
76      }
77      StatusManager sm = context.getStatusManager();
78      if (sm != null) {
79        sm.add(status);
80      }
81    }
82  
83    public void addInfo(String msg) {
84      addStatus(new InfoStatus(msg, getDeclaredOrigin()));
85    }
86  
87    public void addInfo(String msg, Throwable ex) {
88      addStatus(new InfoStatus(msg, getDeclaredOrigin(), ex));
89    }
90  
91    public void addWarn(String msg) {
92      addStatus(new WarnStatus(msg, getDeclaredOrigin()));
93    }
94  
95    public void addWarn(String msg, Throwable ex) {
96      addStatus(new WarnStatus(msg, getDeclaredOrigin(), ex));
97    }
98  
99    public void addError(String msg) {
100     addStatus(new ErrorStatus(msg, getDeclaredOrigin()));
101   }
102 
103   public void addError(String msg, Throwable ex) {
104     addStatus(new ErrorStatus(msg, getDeclaredOrigin(), ex));
105   }
106 }