Limit log size and handle exceptions