0

Is there any way to use variables in the 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 it like this, but I need to use a lot of statements
TBA
  • 1,921
  • 4
  • 13
  • 26
Jelena Ajdukovic
  • 311
  • 3
  • 12

2 Answers2

1

Use Logger.log():

log_event.log(level, ..., extra=extra)
AKX
  • 152,115
  • 15
  • 115
  • 172
-1
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

Daweo
  • 31,313
  • 3
  • 12
  • 25