Mix of logging code from AGN, NA3, CC1, UM1. Tried to keep only the good parts.