001    /**
002     * Copyright (c) 2000-2013 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    
037            @Override
038            public void debug(String message) {
039                    _log.debug(message);
040            }
041    
042            @Override
043            public void debug(String format, Object argument) {
044                    if (isDebugEnabled()) {
045                            FormattingTuple formattingTuple = MessageFormatter.format(
046                                    format, argument);
047    
048                            _log.debug(
049                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
050                    }
051            }
052    
053            @Override
054            public void debug(String format, Object... arguments) {
055                    if (isDebugEnabled()) {
056                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
057                                    format, arguments);
058    
059                            _log.debug(
060                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
061                    }
062            }
063    
064            @Override
065            public void debug(String format, Object argument1, Object argument2) {
066                    if (isDebugEnabled()) {
067                            FormattingTuple formattingTuple = MessageFormatter.format(
068                                    format, argument1, argument2);
069    
070                            _log.debug(
071                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
072                    }
073            }
074    
075            @Override
076            public void debug(String message, Throwable t) {
077                    _log.debug(message, t);
078            }
079    
080            @Override
081            public void error(String message) {
082                    _log.error(message);
083            }
084    
085            @Override
086            public void error(String format, Object argument) {
087                    if (isErrorEnabled()) {
088                            FormattingTuple formattingTuple = MessageFormatter.format(
089                                    format, argument);
090    
091                            _log.error(
092                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
093                    }
094            }
095    
096            @Override
097            public void error(String format, Object... arguments) {
098                    if (isErrorEnabled()) {
099                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
100                                    format, arguments);
101    
102                            _log.error(
103                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
104                    }
105            }
106    
107            @Override
108            public void error(String format, Object argument1, Object argument2) {
109                    if (isErrorEnabled()) {
110                            FormattingTuple formattingTuple = MessageFormatter.format(
111                                    format, argument1, argument2);
112    
113                            _log.error(
114                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
115                    }
116            }
117    
118            @Override
119            public void error(String message, Throwable t) {
120                    _log.error(message, t);
121            }
122    
123            @Override
124            public void info(String message) {
125                    _log.info(message);
126            }
127    
128            @Override
129            public void info(String format, Object argument) {
130                    if (isInfoEnabled()) {
131                            FormattingTuple formattingTuple = MessageFormatter.format(
132                                    format, argument);
133    
134                            _log.info(
135                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
136                    }
137            }
138    
139            @Override
140            public void info(String format, Object... arguments) {
141                    if (isInfoEnabled()) {
142                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
143                                    format, arguments);
144    
145                            _log.info(
146                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
147                    }
148            }
149    
150            @Override
151            public void info(String format, Object argument1, Object argument2) {
152                    if (isInfoEnabled()) {
153                            FormattingTuple formattingTuple = MessageFormatter.format(
154                                    format, argument1, argument2);
155    
156                            _log.info(
157                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
158                    }
159            }
160    
161            @Override
162            public void info(String message, Throwable t) {
163                    _log.info(message, t);
164            }
165    
166            @Override
167            public boolean isDebugEnabled() {
168                    return _log.isDebugEnabled();
169            }
170    
171            @Override
172            public boolean isErrorEnabled() {
173                    return _log.isErrorEnabled();
174            }
175    
176            @Override
177            public boolean isInfoEnabled() {
178                    return _log.isInfoEnabled();
179            }
180    
181            @Override
182            public boolean isTraceEnabled() {
183                    return _log.isTraceEnabled();
184            }
185    
186            @Override
187            public boolean isWarnEnabled() {
188                    return _log.isWarnEnabled();
189            }
190    
191            @Override
192            public void log(
193                    Marker marker, String fqcn, int level, String message,
194                    Object[] arguments, Throwable t) {
195    
196                    FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
197                            message, arguments);
198    
199                    switch (level) {
200                            case LocationAwareLogger.DEBUG_INT:
201                                    _log.debug(formattingTuple.getMessage(), t);
202    
203                                    break;
204    
205                            case LocationAwareLogger.ERROR_INT:
206                                    _log.error(formattingTuple.getMessage(), t);
207    
208                                    break;
209    
210                            case LocationAwareLogger.INFO_INT:
211                                    _log.info(formattingTuple.getMessage(), t);
212    
213                                    break;
214    
215                            case LocationAwareLogger.TRACE_INT:
216                                    _log.trace(formattingTuple.getMessage(), t);
217    
218                                    break;
219    
220                            case LocationAwareLogger.WARN_INT:
221                                    _log.warn(formattingTuple.getMessage(), t);
222    
223                                    break;
224    
225                            default:
226                                    _log.info(formattingTuple.getMessage(), t);
227                    }
228            }
229    
230            @Override
231            public void trace(String message) {
232                    _log.trace(message);
233            }
234    
235            @Override
236            public void trace(String format, Object argument) {
237                    if (isTraceEnabled()) {
238                            FormattingTuple formattingTuple = MessageFormatter.format(
239                                    format, argument);
240    
241                            _log.trace(
242                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
243                    }
244            }
245    
246            @Override
247            public void trace(String format, Object... arguments) {
248                    if (isTraceEnabled()) {
249                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
250                                    format, arguments);
251    
252                            _log.trace(
253                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
254                    }
255            }
256    
257            @Override
258            public void trace(String format, Object argument1, Object argument2) {
259                    if (isTraceEnabled()) {
260                            FormattingTuple formattingTuple = MessageFormatter.format(
261                                    format, argument1, argument2);
262    
263                            _log.trace(
264                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
265                    }
266            }
267    
268            @Override
269            public void trace(String message, Throwable t) {
270                    _log.trace(message, t);
271            }
272    
273            @Override
274            public void warn(String message) {
275                    _log.warn(message);
276            }
277    
278            @Override
279            public void warn(String format, Object argument) {
280                    if (isWarnEnabled()) {
281                            FormattingTuple formattingTuple = MessageFormatter.format(
282                                    format, argument);
283    
284                            _log.warn(
285                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
286                    }
287            }
288    
289            @Override
290            public void warn(String format, Object... arguments) {
291                    if (isWarnEnabled()) {
292                            FormattingTuple formattingTuple = MessageFormatter.arrayFormat(
293                                    format, arguments);
294    
295                            _log.warn(
296                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
297                    }
298            }
299    
300            @Override
301            public void warn(String format, Object argument1, Object argument2) {
302                    if (isWarnEnabled()) {
303                            FormattingTuple formattingTuple = MessageFormatter.format(
304                                    format, argument1, argument2);
305    
306                            _log.warn(
307                                    formattingTuple.getMessage(), formattingTuple.getThrowable());
308                    }
309            }
310    
311            @Override
312            public void warn(String message, Throwable t) {
313                    _log.warn(message, t);
314            }
315    
316            private transient Log _log;
317    
318    }