to_sql call in a log statement is messing things up