1

I want to use step number in my Python script. This step number should be auto incrementing and I want to put it as part of logger. Is there any default option available for that?

2
  • A code snippet would assist illustrating in your scenario & requirement. Mar 21, 2016 at 12:14
  • I have something like this below: logger.info("Step %s : print hi" %step) step+=1 logger.info("Step %s: print hello" %step) step+=1 I want the step to be embedded in the logger, so that I dont have to increment it each time and can use it globally Mar 21, 2016 at 12:29

2 Answers 2

0

The simplest way is to just wrap the logging call.

def step_log(message, *args, **kwargs):
  logging.info("Step %d:" % step_log.counter + message, *args, **kwargs)
  step_log.counter += 1
step_log.counter = 1
1
  • Thank you. That could work for me. Mar 21, 2016 at 16:08
0

This could work in your case

`

step = 0

def sync_step():
    global step
    step = step+1

logging.info("message", sync_step())
logging.info("message", sync_step())
logging.info("message", sync_step())

print step # answer 3

`

2
  • Thank you. I could use this as well. Mar 21, 2016 at 16:08
  • you could accept the answer that you find helpful. Mar 21, 2016 at 19:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.