You can do this using property replacers working on the msg property, assuming this is where the string localhost is found.
Put in your rsyslog.conf or similar a line defining a template called, say, newmsg:
$template newmsg,"%timestamp% %programname% %msg:R,ERE,1:(.*) localhost --end% HOST01 %msg:R,ERE,1: localhost (.*)--end%\n"
To make this more readable here it is split over several lines, but you must use the above version:
$template newmsg, "%timestamp% %programname% %msg:R,ERE,1:(.*) localhost --end% HOST01 %msg:R,ERE,1: localhost (.*)--end% \n"
This contains 2 uses of a replacer like this: %msg:R,ERE,1: ...(...)... --end% where %msg% is the property used as input for a regexp R, extended regexp ERE, keep only capture group 1, followed by the regexp pattern which has a capture group (), with the replacer ended by --end.
Since this template always adds the word HOST01 to the message you should only use it if the message actually contains localhost, so edit the action where you log the message to test for this, eg:
:msg, contains, " localhost " -/var/log/test.log; newmsg
Note the use of the template at the end: ; newmsg.
You can use templates when sending to a remote too, eg:
action(type="omfwd" Target="server.example.net" Port="10514" Protocol="tcp" Template="newmsg" )