Logging
- delfick_project.logging.setup_logging(log=None, level=20, program='', syslog_address='', tcp_address='', udp_address='', only_message=False, json_to_console=False, logging_handler_file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>)
Setup the logging handlers
Note
if you configure the logs to go to the console (and not as json strings), then they will use colors if you have ‘rainbow_logging_handler==2.2.2’ installed in your python environment.
- log
The log to add the handler to.
If this is a string we do logging.getLogger(log)
If this is None, we do logging.getLogger(“”)
Otherwise we use as is
- level
The level we set the logging to
- program
The program to give to the logs.
If syslog is specified, then we give syslog this as the program.
If tcp_address, udp_address or json_to_console is specified, then we create a field in the json called program with this value.
- syslog_address, tcp_address, udp_address
If none of these is specified, then we log to the console.
Otherwise we use the address to converse with a remote server.
Only one will be used.
If syslog is specified that is used, otherwise if udp is specified that is used, otherwise tcp.
- json_to_console
Defaults to False. When True and we haven’t specified syslog/tcp/udp address then write json lines to the console.
- only_message
Whether to only print out the message when going to the console
- logging_handler_file
The file to print to when going to the console
- class delfick_project.logging.LogContext(initial=None, extra=None)
An object to represent logging context
One of these is provided as
delfick_project.logging.lc
You use it by doing something like:
from delfick_project.logging import lc import logging log = logging.getLogger("myproject") log.info(lc("Some logs", var1="one")) ctx = lc.using(var2="two") log.info(ctx("message", anotherarg=1))
Then as long as you’ve used
setup_logging()
the logger will understand the result of calling the logging context and display it nicely.For printing to the console it’ll display the keyword argument as tab separated
key=value
pairs whereas all other outputs will get the log as a json object.- __call__(*args, **kwargs)
Return a dictionary of
{"msg": " ".join(args), **kwargs}
- using(**kwargs)
Return a new logging context with these extra context
- unsafe_add_context(key, value)
Mutate the current logging context