View Javadoc

1   package org.csc.phynixx.common.logger;
2   
3   /*
4    * #%L
5    * phynixx-common
6    * %%
7    * Copyright (C) 2014 csc
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  import java.io.PrintStream;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  public class PrintLogger implements IPhynixxLogger {
29  
30      public static final Integer DEBUG =Integer.valueOf(5);
31      public static final Integer INFO =Integer.valueOf(4);
32      public static final Integer WARN =Integer.valueOf(3);
33      public static final Integer ERROR =Integer.valueOf(2);
34      public static final Integer FATAL =Integer.valueOf(1);
35  
36      private static final Map<Integer,String> VALID_LOG_LEVELS = new HashMap<Integer,String>();
37  
38      static {
39          VALID_LOG_LEVELS.put(DEBUG, "DEBUG");
40          VALID_LOG_LEVELS.put(INFO, "INFO");
41          VALID_LOG_LEVELS.put(WARN, "WARN");
42          VALID_LOG_LEVELS.put(ERROR, "ERROR");
43          VALID_LOG_LEVELS.put(FATAL, "FATAL");
44      }
45  
46  
47      private PrintStream logStream = System.out;
48      private Integer logLevel = ERROR;
49  
50  
51      public Integer getLogLevel() {
52          return logLevel;
53      }
54  
55      public void setLogLevel(Integer logLevel) {
56          if (!VALID_LOG_LEVELS.containsKey(logLevel)) {
57              throw new IllegalArgumentException("Invalid log level " + logLevel);
58          }
59          this.logLevel = logLevel;
60      }
61  
62      public PrintStream getLogStream() {
63          return logStream;
64      }
65  
66      public void setLogStream(PrintStream logStream) {
67          this.logStream = logStream;
68      }
69  
70      public void debug(Object o) {
71          if (this.isDebugEnabled()) {
72              this.getLogStream().println(o);
73          }
74      }
75  
76      public void debug(Object o, Throwable t) {
77  
78          if (this.isDebugEnabled()) {
79              this.getLogStream().println(o + "Exception :: " + t.getMessage());
80              t.printStackTrace(this.getLogStream());
81          }
82      }
83  
84      public void error(Object o) {
85          this.getLogStream().println(o);
86      }
87  
88      public void error(Object o, Throwable t) {
89  
90          this.getLogStream().println(o + "Exception :: " + t.getMessage());
91          t.printStackTrace(this.getLogStream());
92      }
93  
94      public void info(Object o) {
95          if (this.isInfoEnabled()) {
96              this.getLogStream().println(o);
97          }
98      }
99  
100     public void info(Object o, Throwable t) {
101 
102         if (this.isInfoEnabled()) {
103             this.getLogStream().println(o + "Exception :: " + t.getMessage());
104             t.printStackTrace(this.getLogStream());
105         }
106     }
107 
108     public void fatal(Object o) {
109         this.error(o);
110     }
111 
112     public void fatal(Object o, Throwable t) {
113         this.error(o, t);
114     }
115 
116 
117     public boolean isDebugEnabled() {
118         return this.logLevel.compareTo(DEBUG) < 0;
119     }
120 
121     public boolean isInfoEnabled() {
122         return this.logLevel.compareTo(INFO) < 0;
123     }
124 
125     public void warn(Object o) {
126         this.error(o);
127 
128     }
129 
130     public void warn(Object o, Throwable t) {
131         this.error(o, t);
132     }
133 
134 }