All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.qos.logback.ext.spring.EventCacheMode Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2014 The logback-extensions developers ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ch.qos.logback.ext.spring;

import ch.qos.logback.classic.spi.ILoggingEvent;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Bryan Turner
 * @since 0.1
 */
public enum EventCacheMode {

    OFF {
        @Override
        public ILoggingEventCache createCache() {
            return new ILoggingEventCache() {

                @Override
                public List get() {
                    return Collections.emptyList();
                }

                @Override
                public void put(ILoggingEvent event) {
                    //When caching is off, events are discarded as they are received
                }
            };
        }
    },
    ON {
        @Override
        public ILoggingEventCache createCache() {
            return new ILoggingEventCache() {

                private List events = new ArrayList();

                @Override
                public List get() {
                    List list = Collections.unmodifiableList(events);
                    events = null;
                    return list;
                }

                @Override
                public void put(ILoggingEvent event) {
                    events.add(event);
                }
            };
        }
    },
    SOFT {
        @Override
        public ILoggingEventCache createCache() {
            return new ILoggingEventCache() {

                private List> references = new ArrayList>();

                @Override
                public List get() {
                    List events = new ArrayList(references.size());
                    for (SoftReference reference : references) {
                        ILoggingEvent event = reference.get();
                        if (event != null) {
                            events.add(event);
                        }
                    }
                    references = null;
                    return Collections.unmodifiableList(events);
                }

                @Override
                public void put(ILoggingEvent event) {
                    references.add(new SoftReference(event));
                }
            };
        }
    };

    public abstract ILoggingEventCache createCache();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy