001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.sl4fj;
016    
017    import com.liferay.portal.kernel.log.Log;
018    
019    import java.io.Serializable;
020    
021    import org.slf4j.Marker;
022    import org.slf4j.helpers.FormattingTuple;
023    import org.slf4j.helpers.MarkerIgnoringBase;
024    import org.slf4j.helpers.MessageFormatter;
025    import org.slf4j.spi.LocationAwareLogger;
026    
027    /**
028     * @author Michael C. Han
029     */
030    public class LiferayLoggerAdapter
031            extends MarkerIgnoringBase implements LocationAwareLogger, Serializable {
032    
033            public LiferayLoggerAdapter(Log log) {
034                    _log = log;
035    
036                    _log.setLogWrapperClassName(LiferayLoggerAdapter.class.getName());
037            }
038    
039            public LiferayLoggerAdapter(Log log, String name) {
040                    this(log);
041    
042                    this.name = name;
043            }
044    
045            @Override
046            public void debug(String message) {
047                    _log.debug(message);
048            }
049    
050            @Override
051            public void debug(String format, Object argument) {
052                    if (isDebugEnabled()) {
053                            FormattingTuple formattingTuple = MessageFormatter.format(
054                                    format, argument);
055    
056                            _log.debug(
057                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
058                    }
059            }
060    
061            @Override
062            public void debug(String format, Object... arguments) {
063                    if (isDebugEnabled()) {
064                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
065                                    format, arguments);
066    
067                            _log.debug(
068                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
069                    }
070            }
071    
072            @Override
073            public void debug(String format, Object argument1, Object argument2) {
074                    if (isDebugEnabled()) {
075                            FormattingTuple formattingTuple = MessageFormatter.format(
076                                    format, argument1, argument2);
077    
078                            _log.debug(
079                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
080                    }
081            }
082    
083            @Override
084            public void debug(String message, Throwable t) {
085                    _log.debug(message, t);
086            }
087    
088            @Override
089            public void error(String message) {
090                    _log.error(message);
091            }
092    
093            @Override
094            public void error(String format, Object argument) {
095                    if (isErrorEnabled()) {
096                            FormattingTuple formattingTuple = MessageFormatter.format(
097                                    format, argument);
098    
099                            _log.error(
100                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
101                    }
102            }
103    
104            @Override
105            public void error(String format, Object... arguments) {
106                    if (isErrorEnabled()) {
107                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
108                                    format, arguments);
109    
110                            _log.error(
111                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
112                    }
113            }
114    
115            @Override
116            public void error(String format, Object argument1, Object argument2) {
117                    if (isErrorEnabled()) {
118                            FormattingTuple formattingTuple = MessageFormatter.format(
119                                    format, argument1, argument2);
120    
121                            _log.error(
122                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
123                    }
124            }
125    
126            @Override
127            public void error(String message, Throwable t) {
128                    _log.error(message, t);
129            }
130    
131            @Override
132            public void info(String message) {
133                    _log.info(message);
134            }
135    
136            @Override
137            public void info(String format, Object argument) {
138                    if (isInfoEnabled()) {
139                            FormattingTuple formattingTuple = MessageFormatter.format(
140                                    format, argument);
141    
142                            _log.info(
143                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
144                    }
145            }
146    
147            @Override
148            public void info(String format, Object... arguments) {
149                    if (isInfoEnabled()) {
150                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
151                                    format, arguments);
152    
153                            _log.info(
154                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
155                    }
156            }
157    
158            @Override
159            public void info(String format, Object argument1, Object argument2) {
160                    if (isInfoEnabled()) {
161                            FormattingTuple formattingTuple = MessageFormatter.format(
162                                    format, argument1, argument2);
163    
164                            _log.info(
165                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
166                    }
167            }
168    
169            @Override
170            public void info(String message, Throwable t) {
171                    _log.info(message, t);
172            }
173    
174            @Override
175            public boolean isDebugEnabled() {
176                    return _log.isDebugEnabled();
177            }
178    
179            @Override
180            public boolean isErrorEnabled() {
181                    return _log.isErrorEnabled();
182            }
183    
184            @Override
185            public boolean isInfoEnabled() {
186                    return _log.isInfoEnabled();
187            }
188    
189            @Override
190            public boolean isTraceEnabled() {
191                    return _log.isTraceEnabled();
192            }
193    
194            @Override
195            public boolean isWarnEnabled() {
196                    return _log.isWarnEnabled();
197            }
198    
199            @Override
200            public void log(
201                    Marker marker, String fqcn, int level, String message,
202                    Object[] arguments, Throwable t) {
203    
204                    FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
205                            message, arguments);
206    
207                    switch (level) {
208                            case LocationAwareLogger.DEBUG_INT:
209                                    _log.debug(formattingTuple.getMessage(), t);
210    
211                                    break;
212    
213                            case LocationAwareLogger.ERROR_INT:
214                                    _log.error(formattingTuple.getMessage(), t);
215    
216                                    break;
217    
218                            case LocationAwareLogger.INFO_INT:
219                                    _log.info(formattingTuple.getMessage(), t);
220    
221                                    break;
222    
223                            case LocationAwareLogger.TRACE_INT:
224                                    _log.trace(formattingTuple.getMessage(), t);
225    
226                                    break;
227    
228                            case LocationAwareLogger.WARN_INT:
229                                    _log.warn(formattingTuple.getMessage(), t);
230    
231                                    break;
232    
233                            default:
234                                    _log.info(formattingTuple.getMessage(), t);
235                    }
236            }
237    
238            @Override
239            public void trace(String message) {
240                    _log.trace(message);
241            }
242    
243            @Override
244            public void trace(String format, Object argument) {
245                    if (isTraceEnabled()) {
246                            FormattingTuple formattingTuple = MessageFormatter.format(
247                                    format, argument);
248    
249                            _log.trace(
250                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
251                    }
252            }
253    
254            @Override
255            public void trace(String format, Object... arguments) {
256                    if (isTraceEnabled()) {
257                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
258                                    format, arguments);
259    
260                            _log.trace(
261                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
262                    }
263            }
264    
265            @Override
266            public void trace(String format, Object argument1, Object argument2) {
267                    if (isTraceEnabled()) {
268                            FormattingTuple formattingTuple = MessageFormatter.format(
269                                    format, argument1, argument2);
270    
271                            _log.trace(
272                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
273                    }
274            }
275    
276            @Override
277            public void trace(String message, Throwable t) {
278                    _log.trace(message, t);
279            }
280    
281            @Override
282            public void warn(String message) {
283                    _log.warn(message);
284            }
285    
286            @Override
287            public void warn(String format, Object argument) {
288                    if (isWarnEnabled()) {
289                            FormattingTuple formattingTuple = MessageFormatter.format(
290                                    format, argument);
291    
292                            _log.warn(
293                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
294                    }
295            }
296    
297            @Override
298            public void warn(String format, Object... arguments) {
299                    if (isWarnEnabled()) {
300                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
301                                    format, arguments);
302    
303                            _log.warn(
304                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
305                    }
306            }
307    
308            @Override
309            public void warn(String format, Object argument1, Object argument2) {
310                    if (isWarnEnabled()) {
311                            FormattingTuple formattingTuple = MessageFormatter.format(
312                                    format, argument1, argument2);
313    
314                            _log.warn(
315                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
316                    }
317            }
318    
319            @Override
320            public void warn(String message, Throwable t) {
321                    _log.warn(message, t);
322            }
323    
324            private final transient Log _log;
325    
326    }