Any tricks to remove logging commands from production code?

Hi,

in memory constrained projects, it may seem beneficial to remove all those Log.info() (and any log level for that matter), since the string literals we are printing are stored in memory.

Hence, I believe we can save big amounts of memory if we comment them out right before deployment to production. Ok, ok, we remoce the logs, run some tests, then deploy :grimacing:.

Are there any tricks to do this automagically?

Some options I see:

  • write a script to comment out Log.info() and the likes (can hit issues when we are not using {} around blocks, or indentation/writing styles issues)
  • use #define to wrap around every single Log.info() and deactivate them at compile time.

Do you know of any other options?
Please comment below.
Thanks!

Are you flash constrained or RAM-constrained?

Log messages are only stored in flash (in the application binary), not in RAM, and all nRF52 and STM32 devices use XIP to execute directly from flash, including static data and string contants.

If you are flash constrained, first try removing the SerialLogHandler. This should reduce some of the usage, even if you leave the Log.info calls in.

And of course the messages generated by the system will have no effect on your application binary size.

1 Like

Mostly RAM-constrained.
Your comment made me realize that the logs only use flash memory.
I'll try removing the SerialLogHandler and see if that brings benefits.
Thanks!