My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Logging  
Description of how logging works.
Featured
Updated Aug 5, 2011 by ingen...@gmail.com

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 Howto

spymemcached 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 log4j

Set 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 Logging

Set 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 Logger

Sometimes, 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

Comment by suc...@gmail.com, Sep 18, 2008

slf4j is good enough :-)

Comment by ryan.daum, Dec 1, 2008

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)

Comment by Raid...@gmail.com, Dec 9, 2008

I second this idea of removing the spy dependency in order to support something more generic that we can extend/override !

:)

Comment by josephyk...@yahoo.com, Jan 9, 2009

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?

Comment by project member dsalli...@gmail.com, Jan 30, 2009

(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.

Comment by sitao.y...@gmail.com, Mar 23, 2009

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 more
Comment by Jigsm...@gmail.com, Mar 25, 2009

any idea regarding above comment...i am also getting same exception.

Comment by Jigsm...@gmail.com, Mar 26, 2009

now, spy2.4.jar is no more needed...and change logging conf statement to -Dnet.spy.log.LoggerImpl?=net.spy.memcached.compat.log.Log4JLogger

Comment by project member dsalli...@gmail.com, Mar 26, 2009

Jigsmart, thanks for that. It seems really obvious now that you mention it. I haven't changed a config on these in a while.

Comment by obengt...@gmail.com, Apr 5, 2009

Is there a way to adjust the log level for the built-in logger?

Comment by jro...@yahoo.com, Apr 8, 2009

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.

Comment by project member dsalli...@gmail.com, Apr 8, 2009

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

Comment by erin...@gmail.com, Apr 16, 2009

how to use slf4j

add vmargument

-Dnet.spy.log.LoggerImpl?=yourpackage.Slf4JLogger

create a new class file below codes..

public class Slf4JLogger extends AbstractLogger?{

private final Logger log;

public Slf4JLogger(String name) {
super(name); log = LoggerFactory?.getLogger(name);
}
public boolean isDebugEnabled() {
return(log.isDebugEnabled());
} public boolean isInfoEnabled() {
return(log.isInfoEnabled());
} public void log(Level level, Object message, Throwable e) {
String msg = message.toString(); switch(level == null ? Level.FATAL : level){ case DEBUG:
log.debug(msg, e); break;
case INFO:
log.info(msg, e); break;
case WARN:
log.warn(msg, e);
case ERROR: case FATAL:
log.error(msg, e); break;
}
}

}

Comment by ddlat...@gmail.com, Feb 24, 2010

See this issue for some continued discussion: http://code.google.com/p/spymemcached/issues/detail?id=51

Comment by edwardca...@yahoo.com, Mar 4, 2011

I'm running tomcat and wanted less logging in catalina.out, adding this before setting up the client seemed to do the trick

System.setProperty("net.spy.log.LoggerImpl?", "net.spy.memcached.compat.log.SunLogger?"); Logger.getLogger("net.spy.memcached").setLevel(Level.WARNING);

you'll need

import java.util.logging.Level; import java.util.logging.Logger;

in your imports


Sign in to add a comment
Powered by Google Project Hosting