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