Debugging based on a debug level without performance impact? In a web application I am writing, I have used the following:

def dbgmsg(lvl, fmt, ...):
    if dbglvl &gt;= lvl:
        # ... format and write the message
        pass # just here for correctness


Which is then used inside the code as:

dbgmsg(3, "Something happened")


Since there is no thing such as a the #ifdef in Python to my knowledge, I need to include the code at runtime. Will the above code cause a performance impact due to the call being made even when the globally set dbglvl is 0? Which alternatives exist?
It's going to take some time for Python to run the test dbglvl &gt;= lvl. You can estimate the amount of time using the timeit module. For example, on my computer,

&gt;&gt;&gt; from timeit import timeit
&gt;&gt;&gt; def f1():
...     pass
... 
&gt;&gt;&gt; def f2():
...     if x &gt; 0:
...         pass
... 
&gt;&gt;&gt; x = 0
&gt;&gt;&gt; timeit(f1)
0.14283514022827148
&gt;&gt;&gt; timeit(f2)
0.18653011322021484


So the if x &gt; 0: test costs about 40 ns. If this is going to be a problem for you, then Python is probably not a suitable language for your application.
You can avoid the test altogether by having a separate name for the logging function at each debug level and then assigning a no-op function to the levels that don't apply.

def debug_message_write(format, *args):
    debug_output.write(format.format(*args))

def debug_message_ignore(format, *args):
    pass

debug_message_1 = debug_message_write if debug_level &gt;= 1 else debug_message_ignore
debug_message_2 = debug_message_write if debug_level &gt;= 2 else debug_message_ignore
# etc.


(You could avoid the repetition here by writing to the globals() dictionary in a loop.)

And then in the code you would call

debug_message_3("Opening {}", filename)


or whatever. But this is more complicated than your original approach and I doubt that the minuscule saving in time is worth it.
Your code would be easier to read if you wrote debug_message instead of dbgmsg and debug_level instead of dbglvl. There is no need for you to save on vowels: there is no danger of running out.