|
Logging
Description of how logging works.
Featured Logging?For reasons now relegated to history, Spy has its own logging implementation. However, it is compatible with other types of logging, if configured as such. Logging Howtospymemcached is built on top of an internal logging API that has some nice logging abstractions built in. It's analogous to what you might find in Apache's commons-logging, except Dustin had his for quite a while before finding that one. Both log4j and Java's built-in logging are supported. The logger is selected via the system property net.spy.memcached.compat.log.LoggerImpl. Using log4jSet the logger impl to net.spy.log.Log4JLogger. For example: -Dnet.spy.log.LoggerImpl=net.spy.memcached.compat.log.Log4JLogger Using Java's Built-in LoggingSet the logger impl to net.spy.memcached.compat.log.SunLogger. For example: -Dnet.spy.log.LoggerImpl=net.spy.memcached.compat.log.SunLogger This can also be done programmatically, as shown below. If you're writing a simple application, say a test, and you simply want to get the default console handler to be more verbose, you can set that up like so:
// Tell spy to use the SunLogger
Properties systemProperties = System.getProperties();
systemProperties.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.SunLogger");
System.setProperties(systemProperties);
Logger.getLogger("net.spy.memcached").setLevel(Level.FINEST);
//get the top Logger
Logger topLogger = java.util.logging.Logger.getLogger("");
// Handler for console (reuse it if it already exists)
Handler consoleHandler = null;
//see if there is already a console handler
for (Handler handler : topLogger.getHandlers()) {
if (handler instanceof ConsoleHandler) {
//found the console handler
consoleHandler = handler;
break;
}
}
if (consoleHandler == null) {
//there was no console handler found, create a new one
consoleHandler = new ConsoleHandler();
topLogger.addHandler(consoleHandler);
}
//set the console handler to fine:
consoleHandler.setLevel(java.util.logging.Level.FINEST);
Making Logging More Verbose with JDK LoggerSometimes, you want to log what's happening with the internals of spymemcached, but not for every class. An easy way to do that is to define some properties and pass in more specific logging properties. For instance, if you start the JVM with -Djava.util.logging.config.file=logging.properties defined and then put the text below in a file named logging.properties in your classpath, you can log for just the net.spymemcached.vbucket classes. net.spy.memcached.vbucket.level = FINEST --- Attribution: The java.util.logging method of getting the consoleHandler was borrowed from this stackoverflow thread |
slf4j is good enough :-)
Yes, why have you rolled your own logging and your own dependent spy.jar base class in here?
I couldn't find the source for the latter anywhere -- your own links are broken, and it is a required dependency with an open source license. When I decompiled the spy.jar, all I found was that your base "SpyObject?" and "SpyThread?" add nothing, just logging methods.
This kind of forcing proprietary dependencies for no reason... it's anti-pattern, and discourages the use of your library. Most of us with Java projects have enough time tracking and managing dependencies.
I have a fork of your code which is converted to use standard Java Object and Thread, uses SLFJ for logging, and is a Maven 2 project with a standardized buiqdl. If you're interested, I could contribute a patch.
Ryan (see jmemcached, a Java implementation of the memcache daemon, at http://thimbleware.com/projects/jmemcached)
I second this idea of removing the spy dependency in order to support something more generic that we can extend/override !
:)
I've copied memcached-2.2.jar and spy-2.4.jar on my dev environment. When I tried to call client.get(key), it calls get(String, Transcoder<T>) and then asyncGet(String, Transcoder<T>) at line 636. Then it ClaaNotfoundException? when calling loadClassInternal(String). Any suggestion?
(I haven't noticed the comments coming into here until now, so I figure I should respond to them)
@ryan.daum I rolled my own logging because nothing remotely similar (i.e. slf4j) existed until almost three years after I wrote mine. I don't think it's unreasonable to use what I know.
@Raidel7 If there's something you need to extend or override, but can't, then file a bug. I've never found anything I couldn't get my logger to do, so if you have something, I haven't thought of it.
@joseph There are tons of potential classpath related errors. You'll get a CNFE if the class that's cached isn't accessible to the decoder.
I just switched from memcached-2.3.jar recently. I saw the following exception. Any idea? I never seen that issue with memcached-2.1.jar (along with spy-2.4.jar) before. Thanks.
--sitao
ava.lang.RuntimeException: Problem getting logger at net.spy.memcached.compat.log.LoggerFactory.internalGetLogger(LoggerFactory.java:80) at net.spy.memcached.compat.log.LoggerFactory.getLogger(LoggerFactory.java:67) at net.spy.memcached.compat.log.LoggerFactory.getLogger(LoggerFactory.java:53) at net.spy.memcached.compat.SpyObject.getLogger(SpyObject.java:30) at net.spy.memcached.MemcachedConnection.<init>(MemcachedConnection.java:96) at net.spy.memcached.DefaultConnectionFactory.createConnection(DefaultConnectionFactory.java:96) at net.spy.memcached.MemcachedClient.<init>(MemcachedClient.java:147) at net.spy.memcached.MemcachedClient.<init>(MemcachedClient.java:118) at com.rialto.common.cache.SpyMemCacheClient.<init>(SpyMemCacheClient.java:69) at com.rialto.common.cache.SpyMemCacheClient.<init>(SpyMemCacheClient.java:51) Caused by: java.lang.ClassCastException: net.spy.log.Log4JLogger cannot be cast to net.spy.memcached.compat.log.Logger at net.spy.memcached.compat.log.LoggerFactory.getNewInstance(LoggerFactory.java:100) at net.spy.memcached.compat.log.LoggerFactory.internalGetLogger(LoggerFactory.java:78) ... 13 moreany idea regarding above comment...i am also getting same exception.
now, spy2.4.jar is no more needed...and change logging conf statement to -Dnet.spy.log.LoggerImpl?=net.spy.memcached.compat.log.Log4JLogger
Jigsmart, thanks for that. It seems really obvious now that you mention it. I haven't changed a config on these in a while.
Is there a way to adjust the log level for the built-in logger?
How does one change the logging level to less verbose ?
For instance I'd like to suppress messages like this
2009-04-08 13:31:52.120 INFO net.spy.memcached.transcoders.SerializingTranscoder? : Compressed [B from 21693 to 20755
Does one enable the sunlogger and control the logging levels by tags ? Can I control the built logger directly ?
-Dnet.spy.log.LoggerImpl?=net.spy.memcached.compat.log.SunLogger?
Where is the current source for spy-2.4.jar ? I was debugging the SerializingTranscoder? and determined it accessed the logger from the SpyObject? class. I like to be able to verify how the log levels are controlled by debugging the code.
The built-in logger is quite primitive and just dumps info (I believe) and up to stderr. If you care, you're expected to use a different logger. :)
spy.jar should no longer be necessary, but it's all in my github account: http://github.com/dustin
how to use slf4j
add vmargument
-Dnet.spy.log.LoggerImpl?=yourpackage.Slf4JLogger
create a new class file below codes..
public class Slf4JLogger extends AbstractLogger?{
}
See this issue for some continued discussion: http://code.google.com/p/spymemcached/issues/detail?id=51
I'm running tomcat and wanted less logging in catalina.out, adding this before setting up the client seemed to do the trick
you'll need
import java.util.logging.Level; import java.util.logging.Logger;
in your imports