Skip to content

Commit 7a974f9

Browse files
committed
patch from Kevin Lafferty to optimize logging
1 parent d5465ba commit 7a974f9

File tree

1 file changed

+36
-18
lines changed

1 file changed

+36
-18
lines changed

src/com/meetup/memcached/MemcachedClient.java

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,17 @@ public boolean delete( String key, Integer hashCode, Date expiry ) {
462462
// if we get appropriate response back, then we return true
463463
String line = sock.readLine();
464464
if ( DELETED.equals( line ) ) {
465-
log.info( "++++ deletion of key: " + key + " from cache was a success" );
465+
if ( log.isInfoEnabled() )
466+
log.info( "++++ deletion of key: " + key + " from cache was a success" );
466467

467468
// return sock to pool and bail here
468469
sock.close();
469470
sock = null;
470471
return true;
471472
}
472473
else if ( NOTFOUND.equals( line ) ) {
473-
log.info( "++++ deletion of key: " + key + " from cache failed as the key was not found" );
474+
if ( log.isInfoEnabled() )
475+
log.info( "++++ deletion of key: " + key + " from cache failed as the key was not found" );
474476
}
475477
else {
476478
log.error( "++++ error deleting key: " + key );
@@ -718,7 +720,8 @@ private boolean set( String cmdname, String key, Object value, Date expiry, Inte
718720
// useful for sharing data between java and non-java
719721
// and also for storing ints for the increment method
720722
try {
721-
log.info( "++++ storing data as a string for key: " + key + " for class: " + value.getClass().getName() );
723+
if ( log.isInfoEnabled() )
724+
log.info( "++++ storing data as a string for key: " + key + " for class: " + value.getClass().getName() );
722725
val = value.toString().getBytes( defaultEncoding );
723726
}
724727
catch ( UnsupportedEncodingException ue ) {
@@ -735,7 +738,8 @@ private boolean set( String cmdname, String key, Object value, Date expiry, Inte
735738
}
736739
else {
737740
try {
738-
log.info( "Storing with native handler..." );
741+
if ( log.isInfoEnabled() )
742+
log.info( "Storing with native handler..." );
739743
flags |= NativeHandler.getMarkerFlag( value );
740744
val = NativeHandler.encode( value );
741745
}
@@ -756,7 +760,8 @@ private boolean set( String cmdname, String key, Object value, Date expiry, Inte
756760
else {
757761
// always serialize for non-primitive types
758762
try {
759-
log.info( "++++ serializing for key: " + key + " for class: " + value.getClass().getName() );
763+
if ( log.isInfoEnabled() )
764+
log.info( "++++ serializing for key: " + key + " for class: " + value.getClass().getName() );
760765
ByteArrayOutputStream bos = new ByteArrayOutputStream();
761766
(new ObjectOutputStream( bos )).writeObject( value );
762767
val = bos.toByteArray();
@@ -785,8 +790,10 @@ private boolean set( String cmdname, String key, Object value, Date expiry, Inte
785790
if ( compressEnable && val.length > compressThreshold ) {
786791

787792
try {
788-
log.info( "++++ trying to compress data" );
789-
log.info( "++++ size prior to compression: " + val.length );
793+
if ( log.isInfoEnabled() ) {
794+
log.info( "++++ trying to compress data" );
795+
log.info( "++++ size prior to compression: " + val.length );
796+
}
790797
ByteArrayOutputStream bos = new ByteArrayOutputStream( val.length );
791798
GZIPOutputStream gos = new GZIPOutputStream( bos );
792799
gos.write( val, 0, val.length );
@@ -796,7 +803,8 @@ private boolean set( String cmdname, String key, Object value, Date expiry, Inte
796803
val = bos.toByteArray();
797804
flags |= F_COMPRESSED;
798805

799-
log.info( "++++ compression succeeded, size after: " + val.length );
806+
if ( log.isInfoEnabled() )
807+
log.info( "++++ compression succeeded, size after: " + val.length );
800808
}
801809
catch ( IOException e ) {
802810

@@ -819,16 +827,19 @@ private boolean set( String cmdname, String key, Object value, Date expiry, Inte
819827

820828
// get result code
821829
String line = sock.readLine();
822-
log.info( "++++ memcache cmd (result code): " + cmd + " (" + line + ")" );
830+
if ( log.isInfoEnabled() )
831+
log.info( "++++ memcache cmd (result code): " + cmd + " (" + line + ")" );
823832

824833
if ( STORED.equals( line ) ) {
825-
log.info("++++ data successfully stored for key: " + key );
834+
if ( log.isInfoEnabled() )
835+
log.info("++++ data successfully stored for key: " + key );
826836
sock.close();
827837
sock = null;
828838
return true;
829839
}
830840
else if ( NOTSTORED.equals( line ) ) {
831-
log.info( "++++ data not stored in cache for key: " + key );
841+
if ( log.isInfoEnabled() )
842+
log.info( "++++ data not stored in cache for key: " + key );
832843
}
833844
else {
834845
log.error( "++++ error storing data in cache for key: " + key + " -- length: " + val.length );
@@ -932,7 +943,8 @@ public long getCounter( String key, Integer hashCode ) {
932943
errorHandler.handleErrorOnGet( this, ex, key );
933944

934945
// not found or error getting out
935-
log.info( String.format( "Failed to parse Long value for key: %s", key ) );
946+
if ( log.isInfoEnabled() )
947+
log.info( String.format( "Failed to parse Long value for key: %s", key ) );
936948
}
937949

938950
return counter;
@@ -1156,7 +1168,8 @@ private long incrdecr( String cmdname, String key, long inc, Integer hashCode )
11561168
}
11571169
}
11581170
else if ( NOTFOUND.equals( line ) ) {
1159-
log.info( "++++ key not found to incr/decr for key: " + key );
1171+
if ( log.isInfoEnabled() )
1172+
log.info( "++++ key not found to incr/decr for key: " + key );
11601173
}
11611174
else {
11621175
log.error( "++++ error incr/decr key: " + key );
@@ -1335,7 +1348,8 @@ public Object get( String key, Integer hashCode, boolean asString ) {
13351348
if ( ( flag & F_SERIALIZED ) != F_SERIALIZED ) {
13361349
if ( primitiveAsString || asString ) {
13371350
// pulling out string value
1338-
log.info( "++++ retrieving object and stuffing into a string." );
1351+
if ( log.isInfoEnabled() )
1352+
log.info( "++++ retrieving object and stuffing into a string." );
13391353
o = new String( buf, defaultEncoding );
13401354
}
13411355
else {
@@ -1360,7 +1374,8 @@ public Object get( String key, Integer hashCode, boolean asString ) {
13601374
new ContextObjectInputStream( new ByteArrayInputStream( buf ), classLoader );
13611375
try {
13621376
o = ois.readObject();
1363-
log.info( "++++ deserializing " + o.getClass() );
1377+
if ( log.isInfoEnabled() )
1378+
log.info( "++++ deserializing " + o.getClass() );
13641379
}
13651380
catch ( ClassNotFoundException e ) {
13661381

@@ -1561,7 +1576,8 @@ public Map<String,Object> getMulti( String[] keys, Integer[] hashCodes, boolean
15611576
sock.close();
15621577
}
15631578

1564-
log.info( "multi get socket count : " + cmdMap.size() );
1579+
if ( log.isInfoEnabled() )
1580+
log.info( "multi get socket count : " + cmdMap.size() );
15651581

15661582
// now query memcache
15671583
Map<String,Object> ret =
@@ -1675,7 +1691,8 @@ private void loadMulti( LineInputStream input, Map<String,Object> hm, boolean as
16751691
if ( ( flag & F_SERIALIZED ) != F_SERIALIZED ) {
16761692
if ( primitiveAsString || asString ) {
16771693
// pulling out string value
1678-
log.info( "++++ retrieving object and stuffing into a string." );
1694+
if ( log.isInfoEnabled() )
1695+
log.info( "++++ retrieving object and stuffing into a string." );
16791696
o = new String( buf, defaultEncoding );
16801697
}
16811698
else {
@@ -1700,7 +1717,8 @@ private void loadMulti( LineInputStream input, Map<String,Object> hm, boolean as
17001717
new ContextObjectInputStream( new ByteArrayInputStream( buf ), classLoader );
17011718
try {
17021719
o = ois.readObject();
1703-
log.info( "++++ deserializing " + o.getClass() );
1720+
if ( log.isInfoEnabled() )
1721+
log.info( "++++ deserializing " + o.getClass() );
17041722
}
17051723
catch ( ClassNotFoundException e ) {
17061724

0 commit comments

Comments
 (0)