<jupyter_start><jupyter_text>Logging example<jupyter_code>from xopt.log import configure_logger
# Notebook names are this:
__name__
# Put this in the top of any .py file or notebook
import logging
logger = logging.getLogger(__name__)
# Put this at the top-level application. For texting, just use the notebook name. 
configure_logger(logger_name=__name__, file = 'log.txt', level='INFO')
logger.debug('some debug message') # This will write to log.txt
logger.info('some info message')
!cat log.txt
 # Change level to debug
configure_logger(logger_name=__name__, file = 'log.txt', level='DEBUG')
logger.debug('another debug message') 
logger.info('another info message')
!cat log.txt # Now we see debug messages
 # With no file=, the default will go to stdout (here)
configure_logger(logger_name=__name__, level='INFO')
logger.debug('yet another debug message') 
logger.info('yet another info message')
!cat log.txt # Notice this function onlu
# configure_logger only manages one handler. For more, look at the xopt.log code
logger.handlers<jupyter_output><empty_output><jupyter_text>Adding logging to .py filesImporting .py files outside of a package have named based on their filename.<jupyter_code>TEXT="""

import logging
logger = logging.getLogger(__name__)


def f():
    print('__name__ =', __name__)
    logger.info('this is f')
"""
with open('myf.py', 'w') as f:
    f.write(TEXT)
from myf import f
f() # Notice no logger message. 
configure_logger(logger_name='myf', level='INFO')
f() # Now we see the logger message.<jupyter_output>__name__ = myf
this is f<jupyter_text>Cleanup<jupyter_code>!rm log.txt
!rm myf.py<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11010284331518451, 'fraction_numerical': 0.0, 'mean_word_length': 5.778688524590164, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '155132', 'license': 'Apache-2.0', 'n_tokens_mistral': 530, 'n_tokens_neox': 522, 'n_words': 173, 'path': 'examples/utilities/logger_example.ipynb', 'repo_name': 'ChristopherMayes/Xopt', 'size': '1653'}