Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A logger.metric() method #1292

Open
mrkmcnamee opened this issue Feb 5, 2025 · 2 comments
Open

A logger.metric() method #1292

mrkmcnamee opened this issue Feb 5, 2025 · 2 comments

Comments

@mrkmcnamee
Copy link

mrkmcnamee commented Feb 5, 2025

Hi Delgan!

I have a suggestion for a logger.metric() method. Currently we use logger.info() to publish metrics as part of the "extra" data. This works fine. However, as the code is maintained over time, other developers may modify or delete these logger calls, breaking the the monitoring tools that depend on them. We could of course add warning comments to the code, but it would be nice to be able to distinguish between plain old log messages and messages that are emitted specifically for metric monitoring.

At a minimum the logger.metric() method would just be an alias for logger.info(), but it would be a clear signal to developers that external tools could be relying on it. It could also be useful to have a distinct level name, i.e. METRIC, which would be a signal in the other direction to the Operations people looking for metrics to monitor in the log message flow. Now that I write that, it seems that a metric method could be used as a handshake between developers and operations, i.e. here is what you can reliably monitor and we promise to try to not break it.

Cheers,
Kevin

@Delgan
Copy link
Owner

Delgan commented Feb 12, 2025

Hi @mrkmcnamee. :)

That's an interesting idea. I've encountered similar concerns at work, where log messages evolve into product requirements and so must remain unchanged.

However, thinking about it further, I feel this falls outside the scope of a logging library like Loguru. Metrics are a different type of observability data.
I'm not sure how existing telemetry frameworks handle this issue, but it seems best to use them since they specialize in sending metrics and likely offer more powerful and appropriate tools. I'm also not aware of any logging frameworks that provide this kind of feature.

That being said, if you prefer to use the logger directly for simplicity, I’d suggest creating a custom "METRIC" level along with a metric() method: Using logging function based on custom added levels.

@mrkmcnamee
Copy link
Author

mrkmcnamee commented Feb 13, 2025

Hi @Delgan !

I hadn't thought about there being a framework for this, but of course, and I immediately found one called PyMetrics which would probably fit the bill.

Still, the logger.metric() method could be a "starting out" recipe until there's time for implementing something more advanced. Here it is:

from functools import partialmethod
from loguru import logger

logger.level("METRIC", no=25, color="<magenta>", icon="📊")
logger.__class__.metric = partialmethod(logger.__class__.log, "METRIC")

logger.metric("mycount", count=123)

Thanks,
Kevin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants