Permalink
Browse files

SERVER-5013 don't log lines over 100k

  • Loading branch information...
1 parent 0a83368 commit c6539aaea1fab92a7653a86cdc196193feb994bb @erh erh committed Feb 21, 2012
Showing with 54 additions and 2 deletions.
  1. +32 −0 jstests/loglong.js
  2. +22 −2 src/mongo/util/log.cpp
View
@@ -0,0 +1,32 @@
+// test for SERVER-5013
+// make sure very long long lines get truncated
+
+t = db.loglong;
+t.drop();
+
+t.insert( { x : 1 } );
+
+n = 0;
+query = { x : [] }
+while ( Object.bsonsize( query ) < 30000 ) {
+ query.x.push( n++ );
+}
+
+before = db.adminCommand( { setParameter : 1 , logLevel : 1 } )
+
+t.findOne( query )
+
+x = db.adminCommand( { setParameter : 1 , logLevel : before.was } )
+assert.eq( 1 , x.was , tojson( x ) )
+
+log = db.adminCommand( { getLog : "global" } ).log
+
+found = false
+for ( i=log.length - 1; i>= 0; i-- ) {
+ if ( log[i].indexOf( "warning: log line attempted (16k)" ) >= 0 ) {
+ found = true;
+ break;
+ }
+}
+
+assert( found )
View
@@ -267,13 +267,20 @@ namespace mongo {
}
void Logstream::flush(Tee *t) {
+ const size_t MAX_LOG_LINE = 1024 * 10;
+
// this ensures things are sane
if ( doneSetup == 1717 ) {
string msg = ss.str();
+
string threadName = getThreadName();
const char * type = logLevelToString(logLevel);
- int spaceNeeded = (int)(msg.size() + 64 + threadName.size());
+ size_t msgLen = msg.size();
+ if ( msgLen > MAX_LOG_LINE )
+ msgLen = MAX_LOG_LINE;
+
+ const int spaceNeeded = (int)( msgLen + 64 /* for extra info */ + threadName.size());
int bufSize = 128;
while ( bufSize < spaceNeeded )
bufSize += 128;
@@ -295,9 +302,22 @@ namespace mongo {
b.appendStr( ": " , false );
}
- b.appendStr( msg );
+ if ( msg.size() > MAX_LOG_LINE ) {
+ stringstream sss;
+ sss << "warning: log line attempted (" << msg.size() / 1024 << "k) over max size(" << MAX_LOG_LINE / 1024 << "k)";
+ sss << ", printing beginning and end ... ";
+ b.appendStr( sss.str() );
+ const char * xx = msg.c_str();
+ b.appendBuf( xx , MAX_LOG_LINE / 3 );
+ b.appendStr( " .......... " );
+ b.appendStr( xx + msg.size() - ( MAX_LOG_LINE / 3 ) );
+ }
+ else {
+ b.appendStr( msg );
+ }
string out( b.buf() , b.len() - 1);
+ assert( b.len() < spaceNeeded );
scoped_lock lk(mutex);

0 comments on commit c6539aa

Please sign in to comment.