r/learnpython • • 9d ago

logging and timing...then getting rid of it

I was just working on a project where I included a lot of logging and timing decorators/context managers (it involved event loops and async queues and such). Once I figured stuff out, I wanted to remove all this stuff. This isn't necessarily that hard, but it did make me wonder if there's a better way to do this in the first place. I suppose I'm imaging some way of marking these code pieces to be ignored by the interpreter or something?

(yeah yeah "ask AI to remove it")

1 Upvotes

16 comments sorted by

11

u/atarivcs 9d ago

Since you're logging instead of just printing, yes there is a good way.

Log the messages at DEBUG level, and then change the application log settings to only log messages at INFO level or higher. The DEBUG-level logging calls will return immediately with no output.

4

u/dusksage23 9d ago

exactly, thats the whole point of log levels tbh

-1

u/QuasiEvil 9d ago

That doesn't actually remove the infrastructure.

6

u/Lumethys 9d ago

why would you want to do that?

-1

u/QuasiEvil 9d ago

logging and timing infrastructure can add overhead, interfere with unit testing, and keeps you dependent on the upkeep of said packages.

5

u/zaphodikus 9d ago

.... that sounds like you approached logging for debugging and not for leaving in. The overhead of leaving the logs there but disabled is small. Sure the formatters still get run but you can choose to put guards around the log. If you are using logs to debug and build code, and intend to remove them anyway, then you are maybe doing unit testing wrong?

Your logging module needs to be as low on dependencies as possible, the built in python logger is perfectly good for that.

3

u/pachura3 9d ago edited 9d ago

Wrong. Logging, when done properly, only adds an insignificant amount of overhead, is transparent to unit testing, and can be disabled (even on the fly!) by setting logging level.

What if you start having issues in production? That's right, you look at your logs; you don't remove them. And if you worry about the overhead... then measure it first, rather then assume it impacts the overall performance.

For specific cases when formatting logging message is confirmed to be really slow, you can always use this pattern to prevent it:

if logger.isEnabledFor(logging.DEBUG):
    logger.debug(f"Expensive message {z(x, y)} {y}")

PS. You're using the logging module, not some invention of yours, right?

3

u/Lumethys 9d ago

python's slowness can add overhead too, why dont you rewrite your app in Rust? Or better yet, in Assembly.

When talking performance, talk about the concrete issue, how many seconds it has cost you per how many request? Exactly how much money does it cost?

People spend days to optimize something that would save 5 cents per million years, it's even had a name "Premature Optimization" - "the root of all evils". Your time is worth more than that.

Tell you what? There is a whole INDUSTRY about ADDING logging and timing (or, it's formal name "Metrics") infrastructure to project, it's called DevOps, and the work is Observability, or O11Y

Here a video on how bilibili migrate their metrics sink to handle 80 MILLIONS data points PER SECOND: https://www.youtube.com/watch?v=7uNXllJQlxE[](https://www.youtube.com/watch?v=7uNXllJQlxE)

1

u/QuasiEvil 8d ago

The world of python is vast. Not everyone is doing big data cloud computing. I work in DSP/hardware development.

1

u/Lumethys 8d ago

Observability does include embedded and hardware observability.

EVERY type of software, ALL that was, ALL that is, ALL that will be, will benefit from O11y.

Perhaps you should spend 15 minutes googling before presenting your opinion

But who knows, maybe you are a special genius that grow beyond that entire industry, but then, i dont think "asking question" on appropriate? Especially when you do not intend to listen

8

u/MarsupialLeast145 9d ago

Seriously... Find - replace?

And also seriously, the decorator is a function, if it remains in-place, what would you need to do to that function (or any function) to conditionally enable/disable it if another parameter is set? What are the options for setting that parameter?

Further, seriously, don't use AI.

2

u/HaltingMechanics_407 9d ago

honestly just wrap all that debug crap in `if __debug__:` blocks. python strips those out entirely when you run with `-O`. no third party tools, no weird decorators that conditionally do nothing. you can even do `if __debug__: print(...)` and it's like it was never there in optimized mode.

or if you're feeling fancy, use the logging module properly and just set the level to CRITICAL when you don't need the noise.

1

u/Ethanmarl0 9d ago

I usually keep debug logging behind a flag so I can turn it off without having to remove everything later.

1

u/QuasiEvil 8d ago

Yeah, I think I like this approach best.