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