Backport add-log branch and fix NullPointerException