Home > database >  How to use variable inside python logger
How to use variable inside python logger

Time:05-25

Is there any way to use variable in python logger level instead of levels(error, info..)? I get event level from Delta Live Tables events

level_log = event.level // this is from Delta Live Tables Events
log_event.{level_log}(level, extra=extra) // is this possible? and how?

log_event.info(level, extra=extra) // I can use like this, but I need to use a lot if statements

CodePudding user response:

Use Logger.log():

log_event.log(level, ..., extra=extra)

CodePudding user response:

log_event.{level_log}(level, extra=extra) // is this possible? and how?

Yes, you might get attributes dynamically via exploiting getattr, consider following example

import math
what = input("What constant value to show? ")
val = getattr(math, what)
print(val)

which will output 3.141592653589793 if you give pi, 6.283185307179586 if you give tau and 2.718281828459045 if you give e. Here I use built-in math module, but same function might be applied to instances of classes and classes themselves. Attributes which are callable might be then called consider following example

import math
trifunc = input("Value for which trigonometric function at 0 you want to know? ")
val = getattr(math, trifunc)(0)
print(val)

will output 0.0 if you give sin, 1.0 if you give cos, 0.0 if you give tan and so on

  • Related