How to set logging file / directory in Abseil Python?

934 views Asked by At

I have a Python script using https://abseil.io/docs/python/guides/logging.

Documentation does not specify how to set output file like in PEP 282.

I thought that Abseil Logging is built on top of the standard Python logging and I could just set the logging.basicConfig(filename='myapp.log', level=logging.INFO)

But I get:

AttributeError: module 'absl.logging' has no attribute 'basicConfig'

2

There are 2 answers

0
kris On BEST ANSWER

You can set it via the command line:

python --log_dir=/path/to/my/logs

See the (limited) flag documentation by running --helpfull and looking at the absl.logging module.

You can also change it in code (somewhat convolutedly) by calling:

from absl import logging

# ...

logging.get_absl_handler().python_handler.use_absl_log_file(log_dir='/whatever')
print('Logging to: %s' % logging.get_log_file_name())

This will print something like /whatever/py_<myprog>.<hostname>.log.INFO.<ts>. Note that this also creates a friendlier symlink for your latest run that's just /whatever/py_<myprog>.INFO.

1
J_H On

For PEP-282 formatted output, use:

logging.basicConfig(filename='myapp.log', level=logging.INFO)