Japan logged a trade deficit of 634.5 billion yen ($4 billion) in July, government data showed Thursday.