Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
Copyright (C) SYSTAP, LLC DBA Blazegraph 2006-2016. All rights reserved.
Contact:
SYSTAP, LLC DBA Blazegraph
2501 Calvert ST NW #106
Washington, DC 20008
[email protected]
This program 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; version 2 of the License.
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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.blazegraph.gremlin.util;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Stream;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
/**
* Wrap conditional logging using Java 8 lambdas.
*
* To use this functionality, simply request a LambdaLogger instead of a normal Logger:
*
* LambdaLogger log = LambdaLogger.getLogger(Foo.class)
*
* To log conditionally, simply use this syntax:
*
* {@code log.debug(() -> getMessage()); // getMessage() only called if log level >= DEBUG }
*
* In this example getMessage() will not be called unless the log level is
* set to DEBUG or higher. Conditional logging drastically improves
* performance and should ALWAYS be preferred in production (non-test) code
* over the unconditional logging syntax:
*
* log.debug(getMessage()); // getMessage() called regardless of log level
*
* @author mikepersonick
*/
public class LambdaLogger extends Logger {
// private static String FQCN = Logger.class.getName();
private static String FQCN = LambdaLogger.class.getName();
/**
* Run the supplied code, quieting the supplied log at Level.ERROR
*/
public static final void quiet(Class cls, Code code) throws Exception {
quiet(cls, Level.OFF, code);
}
/**
* Run the supplied code, quieting the supplied log with the specified Level
*/
public static final void quiet(Class cls, Level set, Code code) throws Exception {
final Logger log = Logger.getLogger(cls);
final Level curr = log.getLevel();
log.setLevel(set);
try {
code.run();
} finally {
log.setLevel(curr);
}
}
private static final Map loggers = new ConcurrentHashMap<>();
/**
* Override to return a LambdaLogger.
*/
public static LambdaLogger getLogger(final String name) {
if (loggers.containsKey(name)) {
return loggers.get(name);
} else {
final LambdaLogger log = new LambdaLogger(Logger.getLogger(name));
final LambdaLogger curr = loggers.putIfAbsent(name, log);
return curr != null ? curr : log;
}
}
/**
* Override to return a LambdaLogger.
*/
@SuppressWarnings("rawtypes")
public static LambdaLogger getLogger(final Class c) {
return LambdaLogger.getLogger(c.getName());
}
private final Logger log;
private LambdaLogger(final Logger log) {
super(log.getName());
this.log = log;
this.repository = log.getLoggerRepository();
this.parent = log.getParent();
}
/**
*
* Checked exception throwing version of {@code Supplier}.
*
* @author mikepersonick
*/
@FunctionalInterface
public interface Supplier {
public T get() throws Exception ;
}
/**
* Conditionally log at the specified level. Does not call {@code supplier.get() }
* unless {@code log.getLevel() >= level} (conditional message production).
*
* If the supplied object is a stream, conditionally log each object in
* the stream on a separate line.
*/
protected void log(final Level level, final Supplier