Search code examples
pythondebuggingloggingmultiline

Multi-line variable logging Python


I have a Python (3.9.7) logging module that prints some of my variables over multiple lines. An example:

[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,371 RMS re-projection error: 0.49781799970689544
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,371 Camera matrix: [[2.99470903e+04 0.00000000e+00 4.20193475e+02]
 [0.00000000e+00 3.02086634e+04 5.72796134e+02]
 [0.00000000e+00 0.00000000e+00 1.00000000e+00]]
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,373 Distortion coefficients: [[-6.06561628e+00  6.57303938e+02  1.90820141e-02  6.50947473e-02
   1.01373218e+00]]
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,373 Rotation vectors: (array([[ 0.01367595],
       [-3.13001593],
       [-0.10945951]]), array([[ 0.00776643],
       [-3.1302109 ],
       [-0.1329642 ]]), array([[ 0.01940937],
       [-3.13779723],
       [-0.12255833]]), array([[ 0.00404013],
       [-3.13138946],
       [-0.14486612]]))
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,376 Translation vectors: (array([[ 251.01501206],
       [-340.13064488],
       [8311.74652117]]), array([[ 247.35806988],
       [-119.53774136],
       [8265.26765344]]), array([[ 576.15864844],
       [-336.33047539],
       [8258.25314664]]), array([[ 571.09850597],
       [-118.67818482],
       [8269.13533797]]))

While I prefer:

[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,371 RMS re-projection error: 0.49781799970689544
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,371 Camera matrix: [[2.99470903e+04 0.00000000e+00 4.20193475e+02][0.00000000e+00 3.02086634e+04 5.72796134e+02][0.00000000e+00 0.00000000e+00 1.00000000e+00]]
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,373 Distortion coefficients: [[-6.06561628e+00  6.57303938e+02  1.90820141e-02  6.50947473e-02       1.01373218e+00]]
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,373 Rotation vectors: (array([[ 0.01367595],           [-3.13001593],           [-0.10945951]]), array([[ 0.00776643],           [-3.1302109 ],           [-0.1329642 ]]), array([[ 0.01940937],           [-3.13779723],           [-0.12255833]]), array([[ 0.00404013],           [-3.13138946],           [-0.14486612]]))
[   DEBUG] [SERVER_NAME] 2022-01-03 11:19:11,376 Translation vectors: (array([[ 251.01501206],       [-340.13064488],       [8311.74652117]]), array([[ 247.35806988],       [-119.53774136],       [8265.26765344]]), array([[ 576.15864844],       [-336.33047539],       [8258.25314664]]), array([[ 571.09850597],       [-118.67818482],       [8269.13533797]]))

I use the following logging formatter:

formatter = logging.Formatter('[%(levelname)8s] [SERVER_NAME] %(asctime)s %(message)s')

the call for the variables in the logger is done by: self.

logger.debug("RMS re-projection error: %s", ret)
self.logger.debug("Camera matrix: %s", mtx)
self.logger.debug("Distortion coefficients: %s", dist)
self.logger.debug("Rotation vectors: %s", rvecs)
self.logger.debug("Translation vectors: %s", tvecs)

I tried to find a solution to print longer variables on a single line but I could not find anything yet. If you miss any information I am more than happy to share it! Any help is greatly appreciated!


Solution

  • As Olvin Roght suggested, I transformed the variables to strings where I removed the new lines with empty strings by str(ret).replace("\n", "")