Refactor logging code to general function, make sure the message log doesn't overflow after 30 lines.