1

My main program PGM_A calls API2 and another module Subpgm_1. Subpgm_1 invokes SubAPI_1.

PGM_A

  -- Subpgm_1

     -- SubAPI_1

  -- API2

I would need to write a log file in the following format: {"@timestamp":"09/10/2022 11:42:33 AM","log.level":"INFO","message":"Main : Refresh started!!! "}

I have the following code in my main program PGM_A:

   filename = datetime.now().strftime('Logfile_%H%M%d%m%Y.log')
    logging.basicConfig(filename=filename,
    format='{"@timestamp":"%(asctime)s","log.level":"%(levelname)-1s","message":"%(message)s"}',
    datefmt='%m/%d/%Y %H:%M:%S %p')
    logger = logging.getLogger('Delta_Refresh_Pipeline')
    logger.setLevel(logging.INFO)
    handler = logging.StreamHandler()
    handler.setLevel(logging.ERROR)
    logger.addHandler(handler)

I am able to get the logs from main program. But I would need the log messages from APIs and submodule to be written to same log file Logfile.

In short, my log data should look like:

{"@timestamp":"09/10/2022 11:42:33 AM","log.level":"INFO","message":"Log from PGM_A "}
{"@timestamp":"09/10/2022 11:42:34 AM","log.level":"INFO","message":"Log from Subpgm_1"}
{"@timestamp":"09/10/2022 11:42:35 AM","log.level":"INFO","message":"Log from SubAPI_1"}
{"@timestamp":"09/10/2022 11:42:36 AM","log.level":"INFO","message":"Log from API2"}
{"@timestamp":"09/10/2022 11:42:37 AM","log.level":"INFO","message":"Log from PGM_A "}

Any pointers what modification I have to do in the API code and Subpgm_1? Thanks

petezurich
  • 9,280
  • 9
  • 43
  • 57
usr_lal123
  • 650
  • 12
  • 28

1 Answers1

1

Just do logger = logging.getLogger() to log everything with a 'root' logger.

Otherwise, you can use multiple loggers and duplicate the code. So to get Subpgm_1 and SubAPI_1 you should be able to do logger = logging.getLogger('Subpgm_1') if they follow the naming convention.

The logs for SubAPI_1 should be under Subpgm_1.SubAPI_1. So by logging Subpgm_1 you get it, along with everything "under" it, it follows a nice hierarchy

You can also list all available logger names by looking at this question, in case they don't follow the naming convention. See this question for handling multiple loggers

Mandera
  • 2,647
  • 3
  • 21
  • 26