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.portal.test.log;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    
019    import java.util.logging.Handler;
020    import java.util.logging.Level;
021    import java.util.logging.LogRecord;
022    
023    /**
024     * @author William Newbury
025     */
026    public class LogAssertionHandler extends Handler {
027    
028            public static final LogAssertionHandler INSTANCE =
029                    new LogAssertionHandler();
030    
031            @Override
032            public void close() throws SecurityException {
033            }
034    
035            @Override
036            public void flush() {
037            }
038    
039            @Override
040            public void publish(LogRecord logRecord) {
041                    Level level = logRecord.getLevel();
042    
043                    if (level.equals(Level.SEVERE)) {
044                            StringBundler sb = new StringBundler(6);
045    
046                            sb.append("{level=");
047                            sb.append(logRecord.getLevel());
048                            sb.append(", loggerName=");
049                            sb.append(logRecord.getLoggerName());
050                            sb.append(", message=");
051                            sb.append(logRecord.getMessage());
052    
053                            LogAssertionTestRule.caughtFailure(
054                                    new AssertionError(sb.toString(), logRecord.getThrown()));
055                    }
056            }
057    
058    }