View Javadoc

1   /*
2    * Copyright (c) 2014. Peter Crossley
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package com.xley.lfosc.util;
23  
24  import com.xley.lfosc.OSCProxy;
25  import org.apache.log4j.Level;
26  import org.apache.log4j.Logger;
27  
28  import java.util.Map;
29  import java.util.concurrent.ConcurrentHashMap;
30  
31  public class LogUtil {
32  
33      /**
34       * The constant LogUtil.
35       */
36  
37      private static Map<Class, Logger> loggerMap = new ConcurrentHashMap<>();
38      private static Level level = null;
39  
40      public static void trace(Object message) {
41          getLogger(OSCProxy.class).trace(message);
42      }
43  
44      public static void debug(Object message) {
45          getLogger(OSCProxy.class).debug(message);
46      }
47  
48      public static void info(Object message) {
49          getLogger(OSCProxy.class).info(message);
50      }
51  
52      public static void warn(Object message) {
53          getLogger(OSCProxy.class).warn(message);
54      }
55  
56      public static void error(Object message) {
57          getLogger(OSCProxy.class).error(message);
58      }
59  
60      public static void error(Object message, Throwable throwable) {
61          getLogger(OSCProxy.class).error(message, throwable);
62      }
63  
64      public static void fatal(Object message) {
65          getLogger(OSCProxy.class).fatal(message);
66      }
67  
68      public static void fatal(Object message, Throwable throwable) {
69          getLogger(OSCProxy.class).fatal(message, throwable);
70      }
71  
72      public static void trace(Class clazz, Object message) {
73          getLogger(clazz).trace(message);
74      }
75  
76      public static void debug(Class clazz, Object message) {
77          getLogger(clazz).debug(message);
78      }
79  
80      public static void info(Class clazz, Object message) {
81          getLogger(clazz).info(message);
82      }
83  
84      public static void warn(Class clazz, Object message) {
85          getLogger(clazz).warn(message);
86      }
87  
88      public static void error(Class clazz, Object message) {
89          getLogger(clazz).error(message);
90      }
91  
92      public static void error(Class clazz, Object message, Throwable throwable) {
93          getLogger(clazz).error(message, throwable);
94      }
95  
96      public static void fatal(Class clazz, Object message) {
97          getLogger(clazz).fatal(message);
98      }
99  
100     public static void fatal(Class clazz, Object message, Throwable throwable) {
101         getLogger(clazz).fatal(message, throwable);
102     }
103 
104     private static Logger getLogger(Class clazz) {
105         Logger log = Logger.getLogger(clazz);
106         if (level != null && !loggerMap.containsKey(clazz)) {
107             log.setLevel(level);
108         }
109         loggerMap.put(clazz, log);
110         return log;
111     }
112 
113     public static void setLevel(Level logLevel) {
114         level = logLevel;
115         for (Logger log : loggerMap.values()) {
116             log.setLevel(level);
117         }
118     }
119 }
120