pythonloggingabseil

How to set logging file / directory in Abseil Python?


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'


Solution

  • 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.