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

org.neo4j.logging.DuplicatingLogProvider Maven / Gradle / Ivy

There is a newer version: 5.26.1
Show newest version
/*
 * Copyright (c) 2002-2016 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package org.neo4j.logging;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Function;

/**
 * A {@link LogProvider} implementation that duplicates all messages to other LogProvider instances
 */
public class DuplicatingLogProvider extends AbstractLogProvider
{
    private final CopyOnWriteArraySet logProviders;
    private final Map> duplicatingLogCache = Collections.synchronizedMap( new WeakHashMap>() );

    /**
     * @param logProviders A list of {@link LogProvider} instances that messages should be duplicated to
     */
    public DuplicatingLogProvider( LogProvider... logProviders )
    {
        this.logProviders = new CopyOnWriteArraySet<>( Arrays.asList( logProviders ) );
    }

    /**
     * Remove a {@link LogProvider} from the duplicating set. Note that the LogProvider must return
     * cached Log instances from its {@link LogProvider#getLog(String)} for this to behave as expected.
     *
     * @param logProvider the LogProvider to be removed
     * @return true if the log was found and removed
     */
    public boolean remove( LogProvider logProvider )
    {
        if ( !this.logProviders.remove( logProvider ) )
        {
            return false;
        }
        for ( DuplicatingLog duplicatingLog : cachedLogs() )
        {
            duplicatingLog.remove( duplicatingLogCache.get( duplicatingLog ).remove( logProvider ) );
        }
        return true;
    }

    @Override
    protected DuplicatingLog buildLog( final Class loggingClass )
    {
        return buildLog( logProvider -> {
            return logProvider.getLog( loggingClass );
        } );
    }

    @Override
    protected DuplicatingLog buildLog( final String name )
    {
        return buildLog( logProvider -> {
            return logProvider.getLog( name );
        } );
    }

    private DuplicatingLog buildLog( Function logConstructor )
    {
        ArrayList logs = new ArrayList<>( logProviders.size() );
        HashMap providedLogs = new HashMap<>();
        for ( LogProvider logProvider : logProviders )
        {
            Log log = logConstructor.apply( logProvider );
            providedLogs.put( logProvider, log );
            logs.add( log );
        }
        DuplicatingLog duplicatingLog = new DuplicatingLog( logs );
        duplicatingLogCache.put( duplicatingLog, providedLogs );
        return duplicatingLog;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy