com.tangfan.test.log.Log4jMockAppender.java Source code

Java tutorial

Introduction

Here is the source code for com.tangfan.test.log.Log4jMockAppender.java

Source

/**
 * Copyright (c) 2005-2012 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.tangfan.test.log;

import java.util.List;

import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.spi.LoggingEvent;

import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

/**
 * List?Appender, Log4j.
 * 
 * ?, ??addToLogger()appender??logger.
 * 
 * @author calvin
 */
public class Log4jMockAppender extends AppenderSkeleton {

    private List<LoggingEvent> logs = Lists.newArrayList();

    /**
     * ?appendlog.
     */
    public LoggingEvent getFirstLog() {
        if (logs.isEmpty()) {
            return null;
        }
        return logs.get(0);
    }

    /**
     * ?appendlog?.
     */
    public String getFirstMessage() {
        if (logs.isEmpty()) {
            return null;
        }
        return getFirstLog().getMessage().toString();
    }

    /**
     * ?append?log.
     */
    public LoggingEvent getLastLog() {
        if (logs.isEmpty()) {
            return null;
        }
        return Iterables.getLast(logs);
    }

    /**
     * ?append?log?.
     */
    public String getLastMessage() {
        if (logs.isEmpty()) {
            return null;
        }
        return getLastLog().getMessage().toString();
    }

    /**
     * ?appendlog.
     */
    public List<LoggingEvent> getAllLogs() {
        return logs;
    }

    /**
     * Log?
     */
    public int getLogsCount() {
        return logs.size();
    }

    /**
     * ?log.
     */
    public boolean isEmpty() {
        return logs.isEmpty();
    }

    /**
     * ?appendlog.
     */
    public void clearLogs() {
        logs.clear();
    }

    /**
     * appenderlogger.
     */
    public void addToLogger(String loggerName) {
        Logger logger = Logger.getLogger(loggerName);
        logger.addAppender(this);
    }

    /**
     * appenderlogger.
     */
    public void addToLogger(Class<?> loggerClass) {
        Logger logger = Logger.getLogger(loggerClass);
        logger.addAppender(this);
    }

    /**
     * appenderlogger.
     */
    public void removeFromLogger(String loggerName) {
        Logger logger = Logger.getLogger(loggerName);
        logger.removeAppender(this);
    }

    /**
     * appenderlogger.
     */
    public void removeFromLogger(Class<?> loggerClass) {
        Logger logger = Logger.getLogger(loggerClass);
        logger.removeAppender(this);
    }

    /**
     * ?.
     */
    public void setLayout(String pattern) {
        setLayout(new PatternLayout(pattern));
    }

    /**
     * AppenderSkeletonappend, logList.
     */
    @Override
    protected void append(LoggingEvent event) {
        logs.add(event);
    }

    /**
     * @see AppenderSkeleton#close()
     */
    @Override
    public void close() {
        logs.clear();
    }

    /**
     * @see AppenderSkeleton#requiresLayout()
     */
    @Override
    public boolean requiresLayout() {
        return false;
    }

}