Source Code for Apache Log4j Core Implementation

Apache Log4j Core Implementation provides the functional components of the logging system. Users are free to create their own plugins and include them in the logging configuration. Apache Log4j Core is a required module to use Apache Log4j.

Bytecode (Java 8) for Apache Log4j Core Implementation is provided in a separate JAR file like log4j-core-2.14.1.jar.

Source Code files for Apache Log4j API are provided in both binary packge like apache-log4j-2.14.1-bin.zip and source package like apache-log4j-2.14.1-src.zip. You can download them at Apache Log4j Website.

You can also browse Source Code files for Apache Log4j Core Implementation 2.14.1 below.

✍: FYIcenter.com

org/apache/logging/log4j/core/util/CyclicBuffer.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.logging.log4j.core.util;

import java.lang.reflect.Array;

/**
 * A bounded buffer containing elements of type T. When the number of elements to be added will exceed the
 * size of the buffer the oldest element will be overwritten. Access to the buffer is thread safe.
 * @param <T> The type of object stored in the buffer.
 */
public final class CyclicBuffer<T> {
    private final T[] ring;
    private int first = 0;
    private int last = 0;
    private int numElems = 0;
    private final Class<T> clazz;

    /**
     * Instantiates a new CyclicBuffer of at most <code>maxSize</code> events.
     * @param clazz The Class associate with the type of object in the buffer.
     * @param size The number of items in the buffer.
     * @throws IllegalArgumentException if the size is negative.
     */
    public CyclicBuffer(final Class<T> clazz, final int size) throws IllegalArgumentException {
        if (size < 0) {
            throw new IllegalArgumentException("The maxSize argument (" + size + ") cannot be negative.");
        }
        this.ring = makeArray(clazz, size);
        this.clazz = clazz;
    }

    @SuppressWarnings("unchecked")
    private T[] makeArray(final Class<T> cls, final int size) {
        return (T[]) Array.newInstance(cls, size);
    }

    /**
     * Adds an item as the last event in the buffer.
     * @param item The item to add to the buffer.
     */
    public synchronized void add(final T item) {
        if (ring.length > 0) {
            ring[last] = item;
            if (++last == ring.length) {
                last = 0;
            }

            if (numElems < ring.length) {
                numElems++;
            } else if (++first == ring.length) {
                first = 0;
            }
        }
    }

    /**
     * Removes all the elements from the buffer and returns them.
     * @return An array of the elements in the buffer.
     */
    public synchronized T[] removeAll() {
        final T[] array = makeArray(clazz, numElems);
        int index = 0;
        while (numElems > 0) {
            numElems--;
            array[index++] = ring[first];
            ring[first] = null;
            if (++first == ring.length) {
                first = 0;
            }
        }
        return array;
    }

    /**
     * Determines if the buffer contains elements.
     * @return true if the buffer is empty, false otherwise.
     */
    public boolean isEmpty() {
        return 0 == numElems;
    }
}

org/apache/logging/log4j/core/util/CyclicBuffer.java

 

Or download all of them as a single archive file:

File name: log4j-core-2.14.1-sources.jar
File size: 1281358 bytes
Release date: 2021-03-06
Download 

 

Source Code for Apache Log4j JDK Logging Adapter

Source Code for Apache Log4j API

Downloading and Reviewing Apache Log4j Packages

⇑⇑ FAQ for Apache Log4j

2015-11-03, 92875👍, 0💬