madzak / python-json-logger

Json Formatter for the standard python logger
BSD 2-Clause "Simplified" License
1.7k stars 231 forks source link

Type Annotations #95

Closed MartinThoma closed 3 years ago

MartinThoma commented 4 years ago

I'm currently annotating a code base and wonder what the types of add_fields(self, log_record, record, message_dict) are. My current best guess:

from typing import Any, Dict
import logging

def add_fields(self, log_record, record:logging.LogRecord, message_dict: Dict[str, Any]) -> None: ...

What is log_record? Maybe a Dict?

On a more general note: What do you think about adding type annotations? As you still support Python 3.4 and 3.5, they should be added as type stubs. If you want, I can help you get started :-)

the-wondersmith commented 3 years ago

I'll happily pitch in, if any help is needed on this.

@MartinThoma you should be able to get the types by doing something like:

def add_fields(self, *args, **kwargs):
    for pos, arg in enumerate(args):
        print(f"Arg {pos}:\t{type(arg)} -> {arg}"
    for kw, arg in kwargs.items():
        print(f"Kwarg {kw}:\t{type(arg)} -> {arg}") 

Then of course just fire up a logging instance and log something. It's probably gonna pitch a fit so it'll probably be messy in the console, but it should also tell you what you'd like to know.

MartinThoma commented 3 years ago

Thank you for the support. I no longer have access to that code, so I cannot tell what the solution was/is.