add log writing if errors occur