python-logging-loki icon indicating copy to clipboard operation
python-logging-loki copied to clipboard

Use 'level' instead of 'severity'

Open MartijnVanAndel opened this issue 4 years ago • 4 comments

Loki and Grafana use level as label to show the logging severity/verbose/levelname. It would be useful to replace the current severity label with level, as all queries and visualizations in Grafana would automatically pick up the log level. I can create a pull request if you like, but let me know if the current has a special purpose.

MartijnVanAndel avatar Feb 15 '21 22:02 MartijnVanAndel

That would be a very simple and useful change.

Grafana Loki output with and without level=error: image

Apollo3zehn avatar Aug 11 '21 13:08 Apollo3zehn

It seems the level tag can be easily changed with the following code:

import logging

from logging_loki import LokiHandler, emitter

emitter.LokiEmitter.level_tag = "level"

handler = LokiHandler(
    url="<url>", 
    tags={"app": "<appname>"},
    version="1",
)

logger = logging.getLogger("<loggername>")
logger.addHandler(handler)
logger.error(
    "Something happened", 
    extra={"tags": {"service": "my-service"}},
)

Apollo3zehn avatar Aug 11 '21 14:08 Apollo3zehn

Hi, the logger only supports error I can send logs to Loki but I perform logger.info he does not write it to Loki

edeno1 avatar Jul 11 '22 14:07 edeno1

Hi @edeno1

If it is still a problem for you, this article solves the problem:

In short, you need to set the default level to a lower level, for example to DEBUG. This is done with logger.setLevel(logging.DEBUG)

Jacob-EWE avatar Oct 05 '23 11:10 Jacob-EWE