View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.log;
33  
34  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35  import javax.annotation.Nullable;
36  import org.slf4j.Logger;
37  import org.slf4j.Marker;
38  
39  public final class SLf4jXLogAdapter implements XLog {
40  
41    private final Logger wrapped;
42  
43    @SuppressFBWarnings("LO_SUSPECT_LOG_PARAMETER")
44    public SLf4jXLogAdapter(final Logger wrapped) {
45      this.wrapped = wrapped;
46    }
47  
48    @Override
49    @SuppressFBWarnings({"SA_LOCAL_SELF_COMPARISON", "SF_SWITCH_FALLTHROUGH"})
50    public void logUpgrade(@Nullable final Marker marker, final Level level, final String format,
51            final Object... pargs) {
52      LogUtils.logUpgrade(wrapped, marker, level, format, pargs);
53    }
54  
55    @Override
56    public void log(@Nullable final Marker marker, final Level level, final String format, final Object... args) {
57      switch (level) {
58        case TRACE:
59          if (marker != null) {
60            wrapped.trace(marker, format, args);
61          } else {
62            wrapped.trace(format, args);
63          }
64          break;
65        case DEBUG:
66          if (marker != null) {
67            wrapped.debug(marker, format, args);
68          } else {
69            wrapped.debug(format, args);
70          }
71          break;
72        case INFO:
73          if (marker != null) {
74            wrapped.info(marker, format, args);
75          } else {
76            wrapped.info(format, args);
77          }
78          break;
79        case WARN:
80          if (marker != null) {
81            wrapped.warn(marker, format, args);
82          } else {
83            wrapped.warn(format, args);
84          }
85          break;
86        case ERROR:
87          if (marker != null) {
88            wrapped.error(marker, format, args);
89          } else {
90            wrapped.error(format, args);
91          }
92          break;
93        default:
94          throw new UnsupportedOperationException("Unsupported " + level);
95      }
96    }
97  
98    @Override
99    public boolean isEnabled(final Level level, @Nullable final Marker marker) {
100     switch (level) {
101       case TRACE:
102         if (marker == null) {
103           return wrapped.isTraceEnabled();
104         } else {
105           return wrapped.isTraceEnabled(marker);
106         }
107       case DEBUG:
108         if (marker == null) {
109           return wrapped.isDebugEnabled();
110         } else {
111           return wrapped.isDebugEnabled(marker);
112         }
113       case INFO:
114         if (marker == null) {
115           return wrapped.isInfoEnabled();
116         } else {
117           return wrapped.isInfoEnabled(marker);
118         }
119       case WARN:
120         if (marker == null) {
121           return wrapped.isWarnEnabled();
122         } else {
123           return wrapped.isWarnEnabled(marker);
124         }
125       case ERROR:
126         if (marker == null) {
127           return wrapped.isErrorEnabled();
128         } else {
129           return wrapped.isErrorEnabled(null);
130         }
131       default:
132         throw new UnsupportedOperationException("Unsupported " + level);
133     }
134   }
135 
136   @Override
137   public Logger getWrapped() {
138     return wrapped;
139   }
140 
141   @Override
142   public String toString() {
143     return "SLf4jXLogAdapter{" + "wrapped=" + wrapped + '}';
144   }
145 
146 }