In Java world, there are different logging frameworks available, e.g: slf4j, log4j, commons logging as well as java standard log, the reason why we have so many log frameworks is because the log module in jdk is not good enough initially. in Python world it's totally different story, most of the time we should use python standard logging module, in grpc-mate, we follow the standard way to do logging as well.
log is very important for a running server, developer rely on the log to know what's going on when the server is running, in grpc-mate, I use a yaml file to config the log like below
version: 1
formatters:
simple:
format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
__main__:
level: DEBUG
handlers: [console]
propagate: no
service.greeter_servicer:
level: DEBUG
handlers: [console]
propagate: no
root:
level: INFO
handlers: [console]
propagate: yes
we could format the log message format to show datetime followed by logger name , then log leven and the actual log message, we could refer what format available in logrecord-attributes, we could also define the handlers, in this example, I will just out put the log into stdout, it's important we know where the log message comes from, so we could also define differnet loggers and customize the log level for each logger, finally we should define a root logger, if we do not define specific logger, it will reuse the root logger config.
after define the log config, we could load the into runtime, I achieve this by putting logic below in the grpc server module, so it's configged before we start grpc server
# Create a custom logger
with open('logging.yaml', 'r') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config)
now we could define logger for each module, it's recommended to define a logger for each module logger = logging.getLogger(__name__)
, in grpc-mate I define logger in each servicer
when we start the grpc server and try to call the greeter service, the server will output log like below
2019-11-13 19:21:44,320 - __main__ - DEBUG - grpc server started at port 8080
2019-11-13 19:21:49,430 - service.greeter_servicer - DEBUG - get request local