googleapis / googleapis/google-cloud-python
Add StructuredLogHandler adapter
- Linguagem predominante
- Python
- Estrelas
- 5.4k
- Forks
- 1.8k
- Merge médio
- 2d 23h
- PRs com merge (30d)
- 123
Descrição
Thanks for stopping by to let us know something could be better!
**Is your feature request related to a problem? Please describe.**
In my project hosted in Google Cloud, I use `StructuredLogHandler` and quite often I need to pass some predefined context to log labels for easier system monitoring.
Let's say it could be some service configuration values, code versions, and many more.
Basically, I need to pass such context data every logger call: `logger.info('my message', extra={'labels': ...}}` which is hard to maintain.
**Describe the solution you'd like**
For this case, we have `logging.LoggerAdapter` in the Python standard library.
And I propose adding a specific adapter to support the Google Cloud format for this purpose.
I have implemented it already in my project, and think it could be useful for everyone.
With that adapter, I need to configure my logging context once and use my logger in an easier manner.
Let's look at the example:
```
import logging
from google.cloud.logging_v2.handlers.structured_log import AppendLabelLoggingAdapter
from google.cloud.logging_v2.handlers.structured_log import StructuredLogHandler
logging.root.setLevel(logging.INFO)
logging.root.handlers = [StructuredLogHandler()]
first_adapter = AppendLabelLoggingAdapter(logging.root, {'a': 5, 'b': 6})
first_adapter.info('first info')
{
"message": "first info",
"severity": "INFO",
"logging.googleapis.com/labels": {"python_logger": "root", "a": 5, "b": 6}
[...]
}
# Could be stacked
second_adapter=AppendLabelLoggingAdapter(first_adapter, {'hello': 'world'})
second_adapter.info('second info')
{
"message": "second info",
"severity": "INFO",
"logging.googleapis.com/labels": {"python_logger": "root", "hello": "world", "a": 5, "b": 6}
[...]
}
```
Guia de contribuição
Direção de pesquisa
Comece em google.cloud.logging_v2.handlers.structured_log, onde StructuredLogHandler é exposto, e compare o comportamento solicitado com logging.LoggerAdapter da biblioteca padrão do Python. A alteração estará concluída quando um adapter puder adicionar labels configurados a cada chamada de log, preservar o contexto de cada chamada e oferecer suporte ao empilhamento conforme mostrado no exemplo.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- google-cloud, python
- Domínio
- cloud, observability
- Tipo de issue
- Funcionalidade
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 48/100