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