How can two private pilots flying together log time and split costs legally?
Short answer: if your neighbor acts as PIC, then he can pay all the costs and you can both log PIC time for however long you were sole manipulator of the controls. But you have to avoid becoming his personal pilot, because then you're getting into commercial territory.



As background, remember that acting as PIC and logging PIC time are different things. And I'm assuming that you're going to fly VFR only, i.e. there are no IFR and/or safety pilot issues (we have a few questions on that already), and that you're both fully qualified to act as PIC (medical, currency etc.).

First, logging PIC time. 14 CFR 61.51 has the rules, and the key one for your scenario is:


  (e) Logging pilot-in-command flight time. (1) A sport, recreational,
  private, commercial, or airline transport pilot may log pilot in
  command flight time for flights-
  
  (i) When the pilot is the sole manipulator of the controls of an
  aircraft for which the pilot is rated, or has sport pilot privileges
  for that category and class of aircraft, if the aircraft class rating
  is appropriate;


That means, when you're actually controlling the aircraft then you can log PIC time, regardless of who's acting as PIC. For example, if your neighbor has the controls for 0.5 and then you take them for 0.5, you can each log 0.5 PIC time. But in a single-pilot operation, only one person can log PIC at a time.

Second, sharing costs. Because a C172 is a single-pilot aircraft, if your neighbor is acting as PIC then you're a passenger. Note the wording in 61.113 (emphasis mine):


  (a) Except as provided in paragraphs (b) through (h) of this section,
  no person who holds a private pilot certificate may act as pilot in
  command of an aircraft that is carrying passengers or property for
  compensation or hire  
  
  [...]  
  
  (c) A private pilot may not pay less than the pro rata share of
  the operating expenses of a flight with passengers, provided the
  expenses involve only fuel, oil, airport expenditures, or rental fees.


As you can see, if your neighbor is acting as PIC he must pay at least 50% of those specific costs so he can always pay 100% if he wants; that just comes down to what you agree with him. If you act as PIC then the reverse applies: you must pay at least 50%.

Third, whatever arrangement you have with your neighbor, make sure you don't become a de facto personal pilot. If he starts asking you to fly him places he needs to get to, or even to just accompany him on flights because he isn't comfortable alone, then you should be very cautious. In theory, if he always acts as PIC then it's OK because you're just a passenger. But if there's an accident and the FAA has a reason to believe that he wasn't genuinely acting as PIC then you'll both be in trouble. To take an 'extreme' example, let's say he breaks his leg and asks you to fly him somewhere, with him paying all the costs. He couldn't be acting PIC because of his injury (61.53) therefore you would be acting PIC without paying any costs. That's receiving compensation for carrying a passenger and it requires a commercial certificate.

Finally, a couple of practical points to think about:


Two pilots in a single-pilot aircraft can lead to confusion and even accidents. Make sure you have a good pre-flight briefing that includes who is acting as PIC, and in flight always do a clear exchange of controls. And decide up front what happens in case of an emergency, like engine failure: does the person flying keep flying, or does your neighbor always take the controls?
You should have appropriate insurance. Even if he puts you on his policy as a named pilot you might still have to pay thousands of dollars as a deductible in case of an accident. If you don't already have it, it might be worth getting your own non-owned insurance to cover that, but it all depends on the details of the policies. If you're an AOPA member they can probably help you out with that.